Skip to content

Commit 94f76e5

Browse files
Add Publisher::get_subscription_count (#457)
Co-authored-by: Romain Reignier <[email protected]>
1 parent f10fc41 commit 94f76e5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

rclrs/src/publisher.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ where
146146
}
147147
}
148148

149+
/// Returns the number of subscriptions of the publisher.
150+
pub fn get_subscription_count(&self) -> Result<usize, RclrsError> {
151+
let mut subscription_count = 0;
152+
// SAFETY: No preconditions for the function called.
153+
unsafe {
154+
rcl_publisher_get_subscription_count(
155+
&*self.handle.rcl_publisher.lock().unwrap(),
156+
&mut subscription_count,
157+
)
158+
.ok()?
159+
};
160+
Ok(subscription_count)
161+
}
162+
149163
/// Publishes a message.
150164
///
151165
/// The [`MessageCow`] trait is implemented by any
@@ -327,6 +341,29 @@ mod tests {
327341
expected_publishers_info
328342
);
329343

344+
// Test get_subscription_count()
345+
assert_eq!(node_1_empty_publisher.get_subscription_count(), Ok(0));
346+
assert_eq!(node_1_basic_types_publisher.get_subscription_count(), Ok(0));
347+
assert_eq!(node_2_default_publisher.get_subscription_count(), Ok(0));
348+
let _node_1_empty_subscriber = graph.node1.create_subscription(
349+
"graph_test_topic_1",
350+
QOS_PROFILE_SYSTEM_DEFAULT,
351+
|_msg: msg::Empty| {},
352+
);
353+
let _node_1_basic_types_subscriber = graph.node1.create_subscription(
354+
"graph_test_topic_2",
355+
QOS_PROFILE_SYSTEM_DEFAULT,
356+
|_msg: msg::BasicTypes| {},
357+
);
358+
let _node_2_default_subscriber = graph.node2.create_subscription(
359+
"graph_test_topic_3",
360+
QOS_PROFILE_SYSTEM_DEFAULT,
361+
|_msg: msg::Defaults| {},
362+
);
363+
assert_eq!(node_1_empty_publisher.get_subscription_count(), Ok(1));
364+
assert_eq!(node_1_basic_types_publisher.get_subscription_count(), Ok(1));
365+
assert_eq!(node_2_default_publisher.get_subscription_count(), Ok(1));
366+
330367
Ok(())
331368
}
332369
}

0 commit comments

Comments
 (0)