From 8902b71ce526f4b4bf04714ab4efac673082be74 Mon Sep 17 00:00:00 2001 From: Yuma Hiramatsu Date: Sat, 28 Aug 2021 02:37:15 +0900 Subject: [PATCH] Refactoring --- rclrust/src/client.rs | 18 ++++++++++++------ rclrust/src/context.rs | 16 +++++++++++----- rclrust/src/init_options.rs | 1 + rclrust/src/node.rs | 16 +++++++++------- rclrust/src/node_options.rs | 1 + rclrust/src/publisher.rs | 13 +++++++++---- rclrust/src/service.rs | 11 ++++++----- rclrust/src/subscription.rs | 9 +++++---- rclrust/src/timer.rs | 3 ++- 9 files changed, 56 insertions(+), 32 deletions(-) diff --git a/rclrust/src/client.rs b/rclrust/src/client.rs index 56913523..ecca3563 100644 --- a/rclrust/src/client.rs +++ b/rclrust/src/client.rs @@ -62,10 +62,16 @@ impl RclClient { }) } + #[inline] pub const fn raw(&self) -> &rcl_sys::rcl_client_t { &self.r#impl } + #[inline] + fn raw_mut(&mut self) -> &mut rcl_sys::rcl_client_t { + &mut self.r#impl + } + fn send_request(&self, request: &Srv::Request) -> Result where Srv: ServiceT, @@ -73,7 +79,7 @@ impl RclClient { let mut sequence_number = 0; unsafe { rcl_sys::rcl_send_request( - &*self.r#impl, + self.raw(), &request.to_raw_ref() as *const _ as *const c_void, &mut sequence_number, ) @@ -93,7 +99,7 @@ impl RclClient { let mut response = ::Raw::default(); unsafe { rcl_sys::rcl_take_response( - &*self.r#impl, + self.raw(), request_header.as_mut_ptr(), &mut response as *mut _ as *mut c_void, ) @@ -106,7 +112,7 @@ impl RclClient { fn service_name(&self) -> String { unsafe { - let name = rcl_sys::rcl_client_get_service_name(&*self.r#impl); + let name = rcl_sys::rcl_client_get_service_name(self.raw()); String::from_c_char(name).unwrap() } } @@ -116,7 +122,7 @@ impl RclClient { unsafe { rcl_sys::rcl_service_server_is_available( self.node.lock().unwrap().raw(), - &*self.r#impl, + self.raw(), &mut is_available, ) .to_result() @@ -128,14 +134,14 @@ impl RclClient { } fn is_valid(&self) -> bool { - unsafe { rcl_sys::rcl_client_is_valid(&*self.r#impl) } + unsafe { rcl_sys::rcl_client_is_valid(self.raw()) } } } impl Drop for RclClient { fn drop(&mut self) { if let Err(e) = unsafe { - rcl_sys::rcl_client_fini(&mut *self.r#impl, self.node.lock().unwrap().raw_mut()) + rcl_sys::rcl_client_fini(self.raw_mut(), self.node.lock().unwrap().raw_mut()) .to_result() } { rclrust_error!( diff --git a/rclrust/src/context.rs b/rclrust/src/context.rs index e7a3fb66..5af7182b 100644 --- a/rclrust/src/context.rs +++ b/rclrust/src/context.rs @@ -60,18 +60,24 @@ impl RclContext { } } + #[inline] + const fn raw(&self) -> &rcl_sys::rcl_context_t { + &self.0 + } + + #[inline] pub fn raw_mut(&mut self) -> &mut rcl_sys::rcl_context_t { - self.0.as_mut() + &mut self.0 } pub(crate) fn is_valid(&mut self) -> bool { - unsafe { rcl_sys::rcl_context_is_valid(self.0.as_mut()) } + unsafe { rcl_sys::rcl_context_is_valid(self.raw_mut()) } } fn shutdown(&mut self) -> Result<()> { if self.is_valid() { unsafe { - rcl_sys::rcl_shutdown(self.0.as_mut()) + rcl_sys::rcl_shutdown(self.raw_mut()) .to_result() .with_context(|| "rcl_sys::rcl_shutdown in RclContext::shutdown")? } @@ -80,7 +86,7 @@ impl RclContext { } pub(crate) const fn global_arguments(&self) -> &rcl_sys::rcl_arguments_t { - &self.0.global_arguments + &self.raw().global_arguments } } @@ -93,7 +99,7 @@ impl Drop for RclContext { e ) } - if let Err(e) = unsafe { rcl_sys::rcl_context_fini(self.0.as_mut()).to_result() } { + if let Err(e) = unsafe { rcl_sys::rcl_context_fini(self.raw_mut()).to_result() } { rclrust_error!( Logger::new("rclrust"), "Failed to clean up rcl context handle: {}", diff --git a/rclrust/src/init_options.rs b/rclrust/src/init_options.rs index ffc682ac..df9281db 100644 --- a/rclrust/src/init_options.rs +++ b/rclrust/src/init_options.rs @@ -20,6 +20,7 @@ impl RclInitOptions { Ok(Self(options)) } + #[inline] pub const fn raw(&self) -> &rcl_sys::rcl_init_options_t { &self.0 } diff --git a/rclrust/src/node.rs b/rclrust/src/node.rs index e62b6111..2a825bf0 100644 --- a/rclrust/src/node.rs +++ b/rclrust/src/node.rs @@ -67,48 +67,50 @@ impl RclNode { }) } + #[inline] pub(crate) const fn raw(&self) -> &rcl_sys::rcl_node_t { &self.r#impl } - pub(crate) unsafe fn raw_mut(&mut self) -> &mut rcl_sys::rcl_node_t { + #[inline] + pub(crate) fn raw_mut(&mut self) -> &mut rcl_sys::rcl_node_t { &mut self.r#impl } fn is_valid(&self) -> bool { - unsafe { rcl_sys::rcl_node_is_valid(&*self.r#impl) } + unsafe { rcl_sys::rcl_node_is_valid(self.raw()) } } fn name(&self) -> String { unsafe { - let name = rcl_sys::rcl_node_get_name(&*self.r#impl); + let name = rcl_sys::rcl_node_get_name(self.raw()); String::from_c_char(name).unwrap() } } fn namespace(&self) -> String { unsafe { - let namespace = rcl_sys::rcl_node_get_namespace(&*self.r#impl); + let namespace = rcl_sys::rcl_node_get_namespace(self.raw()); String::from_c_char(namespace).unwrap() } } pub(crate) fn fully_qualified_name(&self) -> String { unsafe { - let name = rcl_sys::rcl_node_get_fully_qualified_name(&*self.r#impl); + let name = rcl_sys::rcl_node_get_fully_qualified_name(self.raw()); String::from_c_char(name).unwrap() } } fn logger_name(&self) -> String { unsafe { - let logger_name = rcl_sys::rcl_node_get_logger_name(&*self.r#impl); + let logger_name = rcl_sys::rcl_node_get_logger_name(self.raw()); String::from_c_char(logger_name).unwrap() } } pub fn get_options(&self) -> Option<&rcl_sys::rcl_node_options_t> { - unsafe { rcl_sys::rcl_node_get_options(&*self.r#impl).as_ref() } + unsafe { rcl_sys::rcl_node_get_options(self.raw()).as_ref() } } pub fn use_global_arguments(&self) -> Option { diff --git a/rclrust/src/node_options.rs b/rclrust/src/node_options.rs index 039bbc9f..a096a270 100644 --- a/rclrust/src/node_options.rs +++ b/rclrust/src/node_options.rs @@ -6,6 +6,7 @@ pub(crate) struct RclNodeOptions(rcl_sys::rcl_node_options_t); unsafe impl Send for RclNodeOptions {} impl RclNodeOptions { + #[inline] pub const fn raw(&self) -> &rcl_sys::rcl_node_options_t { &self.0 } diff --git a/rclrust/src/publisher.rs b/rclrust/src/publisher.rs index 0898d292..b4dc8e84 100644 --- a/rclrust/src/publisher.rs +++ b/rclrust/src/publisher.rs @@ -54,13 +54,18 @@ impl RclPublisher { }) } + #[inline] + const fn raw(&self) -> &rcl_sys::rcl_publisher_t { + &self.r#impl + } + fn publish(&self, message: &T) -> Result<()> where T: MessageT, { unsafe { rcl_sys::rcl_publish( - &*self.r#impl, + self.raw(), &message.to_raw_ref() as *const _ as *const c_void, std::ptr::null_mut(), ) @@ -73,19 +78,19 @@ impl RclPublisher { fn topic_name(&self) -> String { unsafe { - let name = rcl_sys::rcl_publisher_get_topic_name(&*self.r#impl); + let name = rcl_sys::rcl_publisher_get_topic_name(self.raw()); String::from_c_char(name).unwrap() } } fn is_valid(&self) -> bool { - unsafe { rcl_sys::rcl_publisher_is_valid(&*self.r#impl) } + unsafe { rcl_sys::rcl_publisher_is_valid(self.raw()) } } fn subscription_count(&self) -> Result { let mut size = 0; unsafe { - rcl_sys::rcl_publisher_get_subscription_count(&*self.r#impl, &mut size) + rcl_sys::rcl_publisher_get_subscription_count(self.raw(), &mut size) .to_result() .with_context(|| { "rcl_sys::rcl_publisher_get_subscription_count in RclPublisher::subscription_count" diff --git a/rclrust/src/service.rs b/rclrust/src/service.rs index 33764d31..a3b38dc7 100644 --- a/rclrust/src/service.rs +++ b/rclrust/src/service.rs @@ -22,7 +22,7 @@ use crate::{ }; #[derive(Debug)] -pub struct RclService { +pub(crate) struct RclService { r#impl: Box, node: Arc>, } @@ -57,6 +57,7 @@ impl RclService { }) } + #[inline] pub const fn raw(&self) -> &rcl_sys::rcl_service_t { &self.r#impl } @@ -71,7 +72,7 @@ impl RclService { let mut request = ::Raw::default(); unsafe { rcl_sys::rcl_take_request( - &*self.r#impl, + self.raw(), request_header.as_mut_ptr(), &mut request as *mut _ as *mut c_void, ) @@ -92,7 +93,7 @@ impl RclService { { unsafe { rcl_sys::rcl_send_response( - &*self.r#impl, + self.raw(), response_header, &response.to_raw_ref() as *const _ as *mut c_void, ) @@ -103,13 +104,13 @@ impl RclService { fn service_name(&self) -> String { unsafe { - let name = rcl_sys::rcl_service_get_service_name(&*self.r#impl); + let name = rcl_sys::rcl_service_get_service_name(self.raw()); String::from_c_char(name).unwrap() } } fn is_valid(&self) -> bool { - unsafe { rcl_sys::rcl_service_is_valid(&*self.r#impl) } + unsafe { rcl_sys::rcl_service_is_valid(self.raw()) } } } diff --git a/rclrust/src/subscription.rs b/rclrust/src/subscription.rs index 2094506e..7d139b7e 100644 --- a/rclrust/src/subscription.rs +++ b/rclrust/src/subscription.rs @@ -59,6 +59,7 @@ impl RclSubscription { }) } + #[inline] pub const fn raw(&self) -> &rcl_sys::rcl_subscription_t { &self.r#impl } @@ -70,7 +71,7 @@ impl RclSubscription { let mut message = T::Raw::default(); unsafe { rcl_sys::rcl_take( - &*self.r#impl, + self.raw(), &mut message as *mut _ as *mut c_void, std::ptr::null_mut(), std::ptr::null_mut(), @@ -84,19 +85,19 @@ impl RclSubscription { fn topic_name(&self) -> String { unsafe { - let topic_name = rcl_sys::rcl_subscription_get_topic_name(&*self.r#impl); + let topic_name = rcl_sys::rcl_subscription_get_topic_name(self.raw()); String::from_c_char(topic_name).unwrap() } } fn is_valid(&self) -> bool { - unsafe { rcl_sys::rcl_subscription_is_valid(&*self.r#impl) } + unsafe { rcl_sys::rcl_subscription_is_valid(self.raw()) } } fn publisher_count(&self) -> Result { let mut size = 0; unsafe { - rcl_sys::rcl_subscription_get_publisher_count(&*self.r#impl, &mut size) + rcl_sys::rcl_subscription_get_publisher_count(self.raw(), &mut size) .to_result() .with_context(|| { "rcl_sys::rcl_subscription_get_publisher_count in RclSubscription::publisher_count" diff --git a/rclrust/src/timer.rs b/rclrust/src/timer.rs index 0ee9c978..46820799 100644 --- a/rclrust/src/timer.rs +++ b/rclrust/src/timer.rs @@ -43,6 +43,7 @@ impl RclTimer { Ok(Self(timer)) } + #[inline] pub const fn raw(&self) -> &rcl_sys::rcl_timer_t { &self.0 } @@ -51,7 +52,7 @@ impl RclTimer { fn is_ready(&self) -> Result { let mut ready = false; unsafe { - rcl_sys::rcl_timer_is_ready(&*self.0, &mut ready) + rcl_sys::rcl_timer_is_ready(self.raw(), &mut ready) .to_result() .with_context(|| "rcl_sys::rcl_timer_is_ready in RclTimer::is_ready")?; }