Skip to content

Commit fe7ca50

Browse files
authored
Remove PublisherHandle (#199)
1 parent b7cedd9 commit fe7ca50

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

rclrs/src/node/publisher.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,14 @@ use std::ffi::CString;
88
use std::marker::PhantomData;
99
use std::sync::Arc;
1010

11-
use parking_lot::{Mutex, MutexGuard};
11+
use parking_lot::Mutex;
1212

1313
use rosidl_runtime_rs::{Message, RmwMessage};
1414

1515
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
1616
// they are running in. Therefore, this type can be safely sent to another thread.
1717
unsafe impl Send for rcl_publisher_t {}
1818

19-
pub(crate) struct PublisherHandle {
20-
rcl_publisher_mtx: Mutex<rcl_publisher_t>,
21-
rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
22-
}
23-
24-
impl PublisherHandle {
25-
fn lock(&self) -> MutexGuard<rcl_publisher_t> {
26-
self.rcl_publisher_mtx.lock()
27-
}
28-
}
29-
30-
impl Drop for PublisherHandle {
31-
fn drop(&mut self) {
32-
let rcl_publisher = self.rcl_publisher_mtx.get_mut();
33-
let rcl_node = &mut *self.rcl_node_mtx.lock();
34-
// SAFETY: No preconditions for this function (besides the arguments being valid).
35-
unsafe {
36-
rcl_publisher_fini(rcl_publisher as *mut _, rcl_node as *mut _);
37-
}
38-
}
39-
}
40-
4119
/// Struct for sending messages of type `T`.
4220
///
4321
/// Multiple publishers can be created for the same topic, in different nodes or the same node.
@@ -52,10 +30,26 @@ pub struct Publisher<T>
5230
where
5331
T: Message,
5432
{
55-
pub(crate) handle: Arc<PublisherHandle>,
33+
rcl_publisher_mtx: Mutex<rcl_publisher_t>,
34+
rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
5635
message: PhantomData<T>,
5736
}
5837

38+
impl<T> Drop for Publisher<T>
39+
where
40+
T: Message,
41+
{
42+
fn drop(&mut self) {
43+
unsafe {
44+
// SAFETY: No preconditions for this function (besides the arguments being valid).
45+
rcl_publisher_fini(
46+
self.rcl_publisher_mtx.get_mut(),
47+
&mut *self.rcl_node_mtx.lock(),
48+
);
49+
}
50+
}
51+
}
52+
5953
impl<T> Publisher<T>
6054
where
6155
T: Message,
@@ -96,13 +90,9 @@ where
9690
.ok()?;
9791
}
9892

99-
let handle = Arc::new(PublisherHandle {
100-
rcl_publisher_mtx: Mutex::new(rcl_publisher),
101-
rcl_node_mtx: node.rcl_node_mtx.clone(),
102-
});
103-
10493
Ok(Self {
105-
handle,
94+
rcl_publisher_mtx: Mutex::new(rcl_publisher),
95+
rcl_node_mtx: Arc::clone(&node.rcl_node_mtx),
10696
message: PhantomData,
10797
})
10898
}
@@ -125,7 +115,7 @@ where
125115
/// [1]: https://github.com/ros2/ros2/issues/255
126116
pub fn publish<'a, M: MessageCow<'a, T>>(&self, message: M) -> Result<(), RclrsError> {
127117
let rmw_message = T::into_rmw_message(message.into_cow());
128-
let rcl_publisher = &mut *self.handle.lock();
118+
let rcl_publisher = &mut *self.rcl_publisher_mtx.lock();
129119
let ret = unsafe {
130120
// SAFETY: The message type is guaranteed to match the publisher type by the type system.
131121
// The message does not need to be valid beyond the duration of this function call.

0 commit comments

Comments
 (0)