diff --git a/rclrs/src/publisher.rs b/rclrs/src/publisher.rs index b1cdd93b9..5a97a43f3 100644 --- a/rclrs/src/publisher.rs +++ b/rclrs/src/publisher.rs @@ -146,6 +146,20 @@ where } } + /// Returns the number of subscriptions of the publisher. + pub fn get_subscription_count(&self) -> Result { + let mut subscription_count = 0; + // SAFETY: No preconditions for the function called. + unsafe { + rcl_publisher_get_subscription_count( + &*self.handle.rcl_publisher.lock().unwrap(), + &mut subscription_count, + ) + .ok()? + }; + Ok(subscription_count) + } + /// Publishes a message. /// /// The [`MessageCow`] trait is implemented by any @@ -327,6 +341,10 @@ mod tests { expected_publishers_info ); + // Test get_subscription_count() + assert_eq!(node_1_empty_publisher.get_subscription_count(), Ok(0)); + assert_eq!(node_2_default_publisher.get_subscription_count(), Ok(0)); + Ok(()) } }