Skip to content

Commit

Permalink
add destroy publisher functions
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dahl committed Oct 30, 2024
1 parent 2236b2a commit 1f8e739
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions r2r/examples/publishers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
count += 1;
}

// destroy publisher before the node
node.destroy_publisher(publisher);

println!("All done!");

Ok(())
Expand Down
24 changes: 24 additions & 0 deletions r2r/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,30 @@ impl Node {
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.
///
/// This handles wakeups of all subscribes, services, etc on the
Expand Down
4 changes: 2 additions & 2 deletions r2r/src/publishers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub struct Publisher<T>
where
T: WrappedTypesupport,
{
handle: Weak<Publisher_>,
pub(crate) handle: Weak<Publisher_>,
type_: PhantomData<T>,
}

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

Expand Down

0 comments on commit 1f8e739

Please sign in to comment.