Skip to content

Commit 7741dfc

Browse files
committed
Disambiguate 'handle' as a variable name
1 parent 318bebf commit 7741dfc

File tree

7 files changed

+73
-74
lines changed

7 files changed

+73
-74
lines changed

rclrs/src/context.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Drop for rcl_context_t {
1616
// line arguments.
1717
// SAFETY: No preconditions for this function.
1818
if rcl_context_is_valid(self) {
19-
// SAFETY: These functions have no preconditions besides a valid handle
19+
// SAFETY: These functions have no preconditions besides a valid rcl_context
2020
rcl_shutdown(self);
2121
rcl_context_fini(self);
2222
}
@@ -41,7 +41,7 @@ unsafe impl Send for rcl_context_t {}
4141
/// - the allocator used (left as the default by `rclrs`)
4242
///
4343
pub struct Context {
44-
pub(crate) handle: Arc<Mutex<rcl_context_t>>,
44+
pub(crate) rcl_context_mtx: Arc<Mutex<rcl_context_t>>,
4545
}
4646

4747
impl Context {
@@ -77,31 +77,31 @@ impl Context {
7777
// SAFETY: No preconditions for this function.
7878
let allocator = rcutils_get_default_allocator();
7979
// SAFETY: Getting a zero-initialized value is always safe.
80-
let mut init_options = rcl_get_zero_initialized_init_options();
80+
let mut rcl_init_options = rcl_get_zero_initialized_init_options();
8181
// SAFETY: Passing in a zero-initialized value is expected.
8282
// In the case where this returns not ok, there's nothing to clean up.
83-
rcl_init_options_init(&mut init_options, allocator).ok()?;
83+
rcl_init_options_init(&mut rcl_init_options, allocator).ok()?;
8484
// SAFETY: This function does not store the ephemeral init_options and c_args
85-
// pointers. Passing in a zero-initialized handle is expected.
85+
// pointers. Passing in a zero-initialized rcl_context is expected.
8686
let ret = rcl_init(
8787
c_args.len() as i32,
8888
if c_args.is_empty() {
8989
std::ptr::null()
9090
} else {
9191
c_args.as_ptr()
9292
},
93-
&init_options,
93+
&rcl_init_options,
9494
&mut rcl_context,
9595
)
9696
.ok();
9797
// SAFETY: It's safe to pass in an initialized object.
9898
// Early return will not leak memory, because this is the last fini function.
99-
rcl_init_options_fini(&mut init_options).ok()?;
99+
rcl_init_options_fini(&mut rcl_init_options).ok()?;
100100
// Move the check after the last fini()
101101
ret?;
102102
}
103103
Ok(Self {
104-
handle: Arc::new(Mutex::new(rcl_context)),
104+
rcl_context_mtx: Arc::new(Mutex::new(rcl_context)),
105105
})
106106
}
107107

@@ -153,8 +153,8 @@ impl Context {
153153
pub fn ok(&self) -> bool {
154154
// This will currently always return true, but once we have a signal handler, the signal
155155
// handler could call `rcl_shutdown()`, hence making the context invalid.
156-
let handle = &mut *self.handle.lock();
156+
let rcl_context = &mut *self.rcl_context_mtx.lock();
157157
// SAFETY: No preconditions for this function.
158-
unsafe { rcl_context_is_valid(handle) }
158+
unsafe { rcl_context_is_valid(rcl_context) }
159159
}
160160
}

rclrs/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::time::Duration;
3434
pub fn spin_once(node: &Node, timeout: Option<Duration>) -> Result<(), RclrsError> {
3535
let live_subscriptions = node.live_subscriptions();
3636
let ctx = Context {
37-
handle: node.context.clone(),
37+
rcl_context_mtx: node.rcl_context_mtx.clone(),
3838
};
3939
let mut wait_set = WaitSet::new(live_subscriptions.len(), &ctx)?;
4040

@@ -57,10 +57,10 @@ pub fn spin(node: &Node) -> Result<(), RclrsError> {
5757
// The context_is_valid functions exists only to abstract away ROS distro differences
5858
#[cfg(ros_distro = "foxy")]
5959
// SAFETY: No preconditions for this function.
60-
let context_is_valid = || unsafe { rcl_context_is_valid(&mut *node.context.lock()) };
60+
let context_is_valid = || unsafe { rcl_context_is_valid(&mut *node.rcl_context_mtx.lock()) };
6161
#[cfg(not(ros_distro = "foxy"))]
6262
// SAFETY: No preconditions for this function.
63-
let context_is_valid = || unsafe { rcl_context_is_valid(&*node.context.lock()) };
63+
let context_is_valid = || unsafe { rcl_context_is_valid(&*node.rcl_context_mtx.lock()) };
6464

6565
while context_is_valid() {
6666
match spin_once(node, None) {

rclrs/src/node.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ unsafe impl Send for rcl_node_t {}
6666
/// [3]: crate::NodeBuilder::new
6767
/// [4]: crate::NodeBuilder::namespace
6868
pub struct Node {
69-
handle: Arc<Mutex<rcl_node_t>>,
70-
pub(crate) context: Arc<Mutex<rcl_context_t>>,
69+
rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
70+
pub(crate) rcl_context_mtx: Arc<Mutex<rcl_context_t>>,
7171
pub(crate) subscriptions: Vec<Weak<dyn SubscriptionBase>>,
7272
}
7373

7474
impl Eq for Node {}
7575

7676
impl PartialEq for Node {
7777
fn eq(&self, other: &Self) -> bool {
78-
Arc::ptr_eq(&self.handle, &other.handle)
78+
Arc::ptr_eq(&self.rcl_node_mtx, &other.rcl_node_mtx)
7979
}
8080
}
8181

@@ -171,8 +171,8 @@ impl Node {
171171
getter: unsafe extern "C" fn(*const rcl_node_t) -> *const c_char,
172172
) -> String {
173173
let char_ptr = unsafe {
174-
// SAFETY: The node handle is valid.
175-
getter(&*self.handle.lock())
174+
// SAFETY: The rcl_node is valid.
175+
getter(&*self.rcl_node_mtx.lock())
176176
};
177177
debug_assert!(!char_ptr.is_null());
178178
let cstr = unsafe {
@@ -249,11 +249,11 @@ impl Node {
249249
// add description about this function is for getting actual domain_id
250250
// and about override of domain_id via node option
251251
pub fn domain_id(&self) -> usize {
252-
let handle = &*self.handle.lock();
252+
let rcl_node = &*self.rcl_node_mtx.lock();
253253
let mut domain_id: usize = 0;
254254
let ret = unsafe {
255255
// SAFETY: No preconditions for this function.
256-
rcl_node_get_domain_id(handle, &mut domain_id)
256+
rcl_node_get_domain_id(rcl_node, &mut domain_id)
257257
};
258258

259259
debug_assert_eq!(ret, 0);

rclrs/src/node/builder.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl NodeBuilder {
8181
/// [3]: NodeBuilder::build
8282
pub fn new(context: &Context, name: &str) -> NodeBuilder {
8383
NodeBuilder {
84-
context: context.handle.clone(),
84+
context: context.rcl_context_mtx.clone(),
8585
name: name.to_string(),
8686
namespace: "/".to_string(),
8787
}
@@ -162,33 +162,33 @@ impl NodeBuilder {
162162
})?;
163163

164164
// SAFETY: No preconditions for this function.
165-
let mut node_handle = unsafe { rcl_get_zero_initialized_node() };
165+
let mut rcl_node = unsafe { rcl_get_zero_initialized_node() };
166166

167167
unsafe {
168168
// SAFETY: No preconditions for this function.
169-
let context_handle = &mut *self.context.lock();
169+
let rcl_context = &mut *self.context.lock();
170170
// SAFETY: No preconditions for this function.
171171
let node_options = rcl_node_get_default_options();
172172

173-
// SAFETY: The node handle is zero-initialized as expected by this function.
173+
// SAFETY: The rcl_node is zero-initialized as expected by this function.
174174
// The strings and node options are copied by this function, so we don't need
175175
// to keep them alive.
176-
// The context handle has to be kept alive because it is co-owned by the node.
176+
// The rcl_context has to be kept alive because it is co-owned by the node.
177177
rcl_node_init(
178-
&mut node_handle,
178+
&mut rcl_node,
179179
node_name.as_ptr(),
180180
node_namespace.as_ptr(),
181-
context_handle,
181+
rcl_context,
182182
&node_options,
183183
)
184184
.ok()?;
185185
};
186186

187-
let handle = Arc::new(Mutex::new(node_handle));
187+
let rcl_node_mtx = Arc::new(Mutex::new(rcl_node));
188188

189189
Ok(Node {
190-
handle,
191-
context: self.context.clone(),
190+
rcl_node_mtx,
191+
rcl_context_mtx: self.context.clone(),
192192
subscriptions: std::vec![],
193193
})
194194
}

rclrs/src/node/publisher.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ use rosidl_runtime_rs::{Message, RmwMessage};
1717
unsafe impl Send for rcl_publisher_t {}
1818

1919
pub(crate) struct PublisherHandle {
20-
handle: Mutex<rcl_publisher_t>,
21-
node_handle: Arc<Mutex<rcl_node_t>>,
20+
rcl_publisher_mtx: Mutex<rcl_publisher_t>,
21+
rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
2222
}
2323

2424
impl PublisherHandle {
2525
fn lock(&self) -> MutexGuard<rcl_publisher_t> {
26-
self.handle.lock()
26+
self.rcl_publisher_mtx.lock()
2727
}
2828
}
2929

3030
impl Drop for PublisherHandle {
3131
fn drop(&mut self) {
32-
let handle = self.handle.get_mut();
33-
let node_handle = &mut *self.node_handle.lock();
32+
let rcl_publisher = self.rcl_publisher_mtx.get_mut();
33+
let rcl_node = &mut *self.rcl_node_mtx.lock();
3434
// SAFETY: No preconditions for this function (besides the arguments being valid).
3535
unsafe {
36-
rcl_publisher_fini(handle as *mut _, node_handle as *mut _);
36+
rcl_publisher_fini(rcl_publisher as *mut _, rcl_node as *mut _);
3737
}
3838
}
3939
}
@@ -68,27 +68,27 @@ where
6868
T: Message,
6969
{
7070
// SAFETY: Getting a zero-initialized value is always safe.
71-
let mut publisher_handle = unsafe { rcl_get_zero_initialized_publisher() };
71+
let mut rcl_publisher = unsafe { rcl_get_zero_initialized_publisher() };
7272
let type_support =
7373
<T as Message>::RmwMsg::get_type_support() as *const rosidl_message_type_support_t;
7474
let topic_c_string = CString::new(topic).map_err(|err| RclrsError::StringContainsNul {
7575
err,
7676
s: topic.into(),
7777
})?;
78-
let node_handle = &mut *node.handle.lock();
78+
let rcl_node = &mut *node.rcl_node_mtx.lock();
7979

8080
// SAFETY: No preconditions for this function.
8181
let mut publisher_options = unsafe { rcl_publisher_get_default_options() };
8282
publisher_options.qos = qos.into();
8383
unsafe {
84-
// SAFETY: The publisher handle is zero-initialized as expected by this function.
85-
// The node handle is kept alive because it is co-owned by the subscription.
84+
// SAFETY: The rcl_publisher is zero-initialized as expected by this function.
85+
// The rcl_node is kept alive because it is co-owned by the subscription.
8686
// The topic name and the options are copied by this function, so they can be dropped
8787
// afterwards.
8888
// TODO: type support?
8989
rcl_publisher_init(
90-
&mut publisher_handle,
91-
node_handle,
90+
&mut rcl_publisher,
91+
rcl_node,
9292
type_support,
9393
topic_c_string.as_ptr(),
9494
&publisher_options,
@@ -97,8 +97,8 @@ where
9797
}
9898

9999
let handle = Arc::new(PublisherHandle {
100-
handle: Mutex::new(publisher_handle),
101-
node_handle: node.handle.clone(),
100+
rcl_publisher_mtx: Mutex::new(rcl_publisher),
101+
rcl_node_mtx: node.rcl_node_mtx.clone(),
102102
});
103103

104104
Ok(Self {
@@ -125,13 +125,13 @@ where
125125
/// [1]: https://github.com/ros2/ros2/issues/255
126126
pub fn publish<'a, M: MessageCow<'a, T>>(&self, message: M) -> Result<(), RclrsError> {
127127
let rmw_message = T::into_rmw_message(message.into_cow());
128-
let handle = &mut *self.handle.lock();
128+
let rcl_publisher = &mut *self.handle.lock();
129129
let ret = unsafe {
130130
// SAFETY: The message type is guaranteed to match the publisher type by the type system.
131131
// The message does not need to be valid beyond the duration of this function call.
132132
// The third argument is explictly allowed to be NULL.
133133
rcl_publish(
134-
handle,
134+
rcl_publisher,
135135
rmw_message.as_ref() as *const <T as Message>::RmwMsg as *mut _,
136136
std::ptr::null_mut(),
137137
)

rclrs/src/node/subscription.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::qos::QoSProfile;
33
use crate::Node;
44
use crate::{rcl_bindings::*, RclrsError};
55

6-
use std::borrow::Borrow;
76
use std::boxed::Box;
87
use std::ffi::CString;
98
use std::marker::PhantomData;
@@ -19,23 +18,23 @@ unsafe impl Send for rcl_subscription_t {}
1918

2019
/// Internal struct used by subscriptions.
2120
pub struct SubscriptionHandle {
22-
handle: Mutex<rcl_subscription_t>,
23-
node_handle: Arc<Mutex<rcl_node_t>>,
21+
rcl_subscription_mtx: Mutex<rcl_subscription_t>,
22+
rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
2423
}
2524

2625
impl SubscriptionHandle {
2726
pub(crate) fn lock(&self) -> MutexGuard<rcl_subscription_t> {
28-
self.handle.lock()
27+
self.rcl_subscription_mtx.lock()
2928
}
3029
}
3130

3231
impl Drop for SubscriptionHandle {
3332
fn drop(&mut self) {
34-
let handle = self.handle.get_mut();
35-
let node_handle = &mut *self.node_handle.lock();
33+
let rcl_subscription = self.rcl_subscription_mtx.get_mut();
34+
let rcl_node = &mut *self.rcl_node_mtx.lock();
3635
// SAFETY: No preconditions for this function (besides the arguments being valid).
3736
unsafe {
38-
rcl_subscription_fini(handle, node_handle);
37+
rcl_subscription_fini(rcl_subscription, rcl_node);
3938
}
4039
}
4140
}
@@ -85,27 +84,27 @@ where
8584
F: FnMut(T) + 'static + Send,
8685
{
8786
// SAFETY: Getting a zero-initialized value is always safe.
88-
let mut subscription_handle = unsafe { rcl_get_zero_initialized_subscription() };
87+
let mut rcl_subscription = unsafe { rcl_get_zero_initialized_subscription() };
8988
let type_support =
9089
<T as Message>::RmwMsg::get_type_support() as *const rosidl_message_type_support_t;
9190
let topic_c_string = CString::new(topic).map_err(|err| RclrsError::StringContainsNul {
9291
err,
9392
s: topic.into(),
9493
})?;
95-
let node_handle = &mut *node.handle.lock();
94+
let rcl_node = &mut *node.rcl_node_mtx.lock();
9695

9796
// SAFETY: No preconditions for this function.
9897
let mut subscription_options = unsafe { rcl_subscription_get_default_options() };
9998
subscription_options.qos = qos.into();
10099
unsafe {
101-
// SAFETY: The subscription handle is zero-initialized as expected by this function.
102-
// The node handle is kept alive because it is co-owned by the subscription.
100+
// SAFETY: The rcl_subscription is zero-initialized as expected by this function.
101+
// The rcl_node is kept alive because it is co-owned by the subscription.
103102
// The topic name and the options are copied by this function, so they can be dropped
104103
// afterwards.
105104
// TODO: type support?
106105
rcl_subscription_init(
107-
&mut subscription_handle,
108-
node_handle,
106+
&mut rcl_subscription,
107+
rcl_node,
109108
type_support,
110109
topic_c_string.as_ptr(),
111110
&subscription_options,
@@ -114,8 +113,8 @@ where
114113
}
115114

116115
let handle = Arc::new(SubscriptionHandle {
117-
handle: Mutex::new(subscription_handle),
118-
node_handle: node.handle.clone(),
116+
rcl_subscription_mtx: Mutex::new(rcl_subscription),
117+
rcl_node_mtx: node.rcl_node_mtx.clone(),
119118
});
120119

121120
Ok(Self {
@@ -149,13 +148,13 @@ where
149148
// ```
150149
pub fn take(&self) -> Result<T, RclrsError> {
151150
let mut rmw_message = <T as Message>::RmwMsg::default();
152-
let handle = &mut *self.handle.lock();
151+
let rcl_subscription = &mut *self.handle.lock();
153152
let ret = unsafe {
154153
// SAFETY: The first two pointers are valid/initialized, and do not need to be valid
155154
// beyond the function call.
156155
// The latter two pointers are explicitly allowed to be NULL.
157156
rcl_take(
158-
handle,
157+
rcl_subscription,
159158
&mut rmw_message as *mut <T as Message>::RmwMsg as *mut _,
160159
std::ptr::null_mut(),
161160
std::ptr::null_mut(),
@@ -171,7 +170,7 @@ where
171170
T: Message,
172171
{
173172
fn handle(&self) -> &SubscriptionHandle {
174-
self.handle.borrow()
173+
&self.handle
175174
}
176175

177176
fn execute(&self) -> Result<(), RclrsError> {

0 commit comments

Comments
 (0)