Skip to content

Commit 25eabf7

Browse files
nnmmesteve
andauthored
Declare fn new in waitables as pub(crate) (#227)
* Always add subscription/client/service to the node's list of waitables * Revert changes. Declare waitables new function with pub(crate) visibility * Document why ::new needs pub(crate) visibility * Fix clippy issues Co-authored-by: Esteve Fernandez <esteve@apache.org>
1 parent faa0284 commit 25eabf7

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

rclrs/src/node/client.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type RequestValue<Response> = Box<dyn FnOnce(Response) + 'static + Send>;
5656
type RequestId = i64;
5757

5858
/// Main class responsible for sending requests to a ROS service.
59+
///
60+
/// The only available way to instantiate clients is via [`Node::create_client`], this is to
61+
/// ensure that [`Node`]s can track all the clients that have been created.
5962
pub struct Client<T>
6063
where
6164
T: rosidl_runtime_rs::Service,
@@ -70,7 +73,9 @@ where
7073
T: rosidl_runtime_rs::Service,
7174
{
7275
/// Creates a new client.
73-
pub fn new(node: &Node, topic: &str) -> Result<Self, RclrsError>
76+
pub(crate) fn new(node: &Node, topic: &str) -> Result<Self, RclrsError>
77+
// This uses pub(crate) visibility to avoid instantiating this struct outside
78+
// [`Node::create_client`], see the struct's documentation for the rationale
7479
where
7580
T: rosidl_runtime_rs::Service,
7681
{

rclrs/src/node/service.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ type ServiceCallback<Request, Response> =
5555
Box<dyn Fn(&rmw_request_id_t, Request) -> Response + 'static + Send>;
5656

5757
/// Main class responsible for responding to requests sent by ROS clients.
58+
///
59+
/// The only available way to instantiate services is via [`Node::create_service`], this is to
60+
/// ensure that [`Node`]s can track all the services that have been created.
5861
pub struct Service<T>
5962
where
6063
T: rosidl_runtime_rs::Service,
@@ -69,7 +72,9 @@ where
6972
T: rosidl_runtime_rs::Service,
7073
{
7174
/// Creates a new service.
72-
pub fn new<F>(node: &Node, topic: &str, callback: F) -> Result<Self, RclrsError>
75+
pub(crate) fn new<F>(node: &Node, topic: &str, callback: F) -> Result<Self, RclrsError>
76+
// This uses pub(crate) visibility to avoid instantiating this struct outside
77+
// [`Node::create_service`], see the struct's documentation for the rationale
7378
where
7479
T: rosidl_runtime_rs::Service,
7580
F: Fn(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,

rclrs/src/node/subscription.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub trait SubscriptionBase: Send + Sync {
5959
/// When a subscription is created, it may take some time to get "matched" with a corresponding
6060
/// publisher.
6161
///
62+
/// The only available way to instantiate subscriptions is via [`Node::create_subscription`], this
63+
/// is to ensure that [`Node`]s can track all the subscriptions that have been created.
64+
///
6265
/// [1]: crate::spin_once
6366
/// [2]: crate::spin
6467
pub struct Subscription<T>
@@ -76,12 +79,14 @@ where
7679
T: Message,
7780
{
7881
/// Creates a new subscription.
79-
pub fn new<F>(
82+
pub(crate) fn new<F>(
8083
node: &Node,
8184
topic: &str,
8285
qos: QoSProfile,
8386
callback: F,
8487
) -> Result<Self, RclrsError>
88+
// This uses pub(crate) visibility to avoid instantiating this struct outside
89+
// [`Node::create_subscription`], see the struct's documentation for the rationale
8590
where
8691
T: Message,
8792
F: FnMut(T) + 'static + Send,
@@ -213,15 +218,14 @@ where
213218
#[cfg(test)]
214219
mod tests {
215220
use super::*;
216-
use crate::{create_node, Context, Subscription, QOS_PROFILE_DEFAULT};
221+
use crate::{create_node, Context, QOS_PROFILE_DEFAULT};
217222

218223
#[test]
219224
fn test_instantiate_subscriber() -> Result<(), RclrsError> {
220225
let context =
221226
Context::new(vec![]).expect("Context instantiation is expected to be a success");
222-
let node = create_node(&context, "test_new_subscriber")?;
223-
let _subscriber = Subscription::<std_msgs::msg::String>::new(
224-
&node,
227+
let mut node = create_node(&context, "test_new_subscriber")?;
228+
let _subscriber = node.create_subscription::<std_msgs::msg::String, _>(
225229
"test",
226230
QOS_PROFILE_DEFAULT,
227231
move |_: std_msgs::msg::String| {},

0 commit comments

Comments
 (0)