@@ -59,6 +59,9 @@ pub trait SubscriptionBase: Send + Sync {
59
59
/// When a subscription is created, it may take some time to get "matched" with a corresponding
60
60
/// publisher.
61
61
///
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
+ ///
62
65
/// [1]: crate::spin_once
63
66
/// [2]: crate::spin
64
67
pub struct Subscription < T >
@@ -76,12 +79,14 @@ where
76
79
T : Message ,
77
80
{
78
81
/// Creates a new subscription.
79
- pub fn new < F > (
82
+ pub ( crate ) fn new < F > (
80
83
node : & Node ,
81
84
topic : & str ,
82
85
qos : QoSProfile ,
83
86
callback : F ,
84
87
) -> 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
85
90
where
86
91
T : Message ,
87
92
F : FnMut ( T ) + ' static + Send ,
@@ -213,15 +218,14 @@ where
213
218
#[ cfg( test) ]
214
219
mod tests {
215
220
use super :: * ;
216
- use crate :: { create_node, Context , Subscription , QOS_PROFILE_DEFAULT } ;
221
+ use crate :: { create_node, Context , QOS_PROFILE_DEFAULT } ;
217
222
218
223
#[ test]
219
224
fn test_instantiate_subscriber ( ) -> Result < ( ) , RclrsError > {
220
225
let context =
221
226
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 , _ > (
225
229
"test" ,
226
230
QOS_PROFILE_DEFAULT ,
227
231
move |_: std_msgs:: msg:: String | { } ,
0 commit comments