Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add destroy publisher functions #110

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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