@@ -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
6467pub 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) ]
214219mod 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