diff --git a/r2r/examples/publishers.rs b/r2r/examples/publishers.rs index 3a9aeece7..535007d67 100644 --- a/r2r/examples/publishers.rs +++ b/r2r/examples/publishers.rs @@ -31,6 +31,9 @@ fn main() -> Result<(), Box> { count += 1; } + // destroy publisher before the node + node.destroy_publisher(publisher); + println!("All done!"); Ok(()) diff --git a/r2r/src/nodes.rs b/r2r/src/nodes.rs index b9bdb395b..c797f1df6 100644 --- a/r2r/src/nodes.rs +++ b/r2r/src/nodes.rs @@ -905,6 +905,30 @@ impl Node { Ok(p) } + /// Destroy a ROS publisher. + pub fn destroy_publisher(&mut self, p: Publisher) { + if let Some(handle) = p.handle.upgrade() { + // Remove handle from list of publishers. + self.pubs.iter().position(|p| Arc::ptr_eq(p, &handle)) + .map(|i| self.pubs.swap_remove(i)); + + let handle = wait_until_unwrapped(handle); + handle.destroy(self.node_handle.as_mut()); + } + } + + /// Destroy a ROS publisher. + pub fn destroy_publisher_untyped(&mut self, p: PublisherUntyped) { + if let Some(handle) = p.handle.upgrade() { + // Remove handle from list of publishers. + self.pubs.iter().position(|p| Arc::ptr_eq(p, &handle)) + .map(|i| self.pubs.swap_remove(i)); + + let handle = wait_until_unwrapped(handle); + handle.destroy(self.node_handle.as_mut()); + } + } + /// Spin the ROS node. /// /// This handles wakeups of all subscribes, services, etc on the diff --git a/r2r/src/publishers.rs b/r2r/src/publishers.rs index afe71be6c..cb3bd3bd7 100644 --- a/r2r/src/publishers.rs +++ b/r2r/src/publishers.rs @@ -104,7 +104,7 @@ pub struct Publisher where T: WrappedTypesupport, { - handle: Weak, + pub(crate) handle: Weak, type_: PhantomData, } @@ -116,7 +116,7 @@ unsafe impl Send for PublisherUntyped {} /// move between threads. #[derive(Debug, Clone)] pub struct PublisherUntyped { - handle: Weak, + pub(crate) handle: Weak, type_: String, }