add destroy publisher functions

This commit is contained in:
Martin Dahl 2024-10-30 20:52:59 +01:00
parent 2236b2ac12
commit 1f8e739803
3 changed files with 29 additions and 2 deletions

View File

@ -31,6 +31,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
count += 1; count += 1;
} }
// destroy publisher before the node
node.destroy_publisher(publisher);
println!("All done!"); println!("All done!");
Ok(()) Ok(())

View File

@ -905,6 +905,30 @@ impl Node {
Ok(p) Ok(p)
} }
/// Destroy a ROS publisher.
pub fn destroy_publisher<T: WrappedTypesupport>(&mut self, p: Publisher<T>) {
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. /// Spin the ROS node.
/// ///
/// This handles wakeups of all subscribes, services, etc on the /// This handles wakeups of all subscribes, services, etc on the

View File

@ -104,7 +104,7 @@ pub struct Publisher<T>
where where
T: WrappedTypesupport, T: WrappedTypesupport,
{ {
handle: Weak<Publisher_>, pub(crate) handle: Weak<Publisher_>,
type_: PhantomData<T>, type_: PhantomData<T>,
} }
@ -116,7 +116,7 @@ unsafe impl Send for PublisherUntyped {}
/// move between threads. /// move between threads.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct PublisherUntyped { pub struct PublisherUntyped {
handle: Weak<Publisher_>, pub(crate) handle: Weak<Publisher_>,
type_: String, type_: String,
} }