@@ -19,11 +19,11 @@ use std::{
19
19
use rosidl_runtime_rs:: Message ;
20
20
21
21
use crate :: {
22
- rcl_bindings:: * , Client , ClientBase , ClientOptions , Clock , ContextHandle , GuardCondition ,
23
- LogParams , Logger , ParameterBuilder , ParameterInterface , ParameterVariant , Parameters ,
24
- Publisher , PublisherOptions , RclrsError , Service , ServiceBase , ServiceOptions , Subscription ,
25
- SubscriptionBase , SubscriptionCallback , SubscriptionOptions , TimeSource , ToLogParams ,
26
- ENTITY_LIFECYCLE_MUTEX ,
22
+ rcl_bindings:: * , Client , ClientBase , ClientOptions , ClientState , Clock , ContextHandle ,
23
+ GuardCondition , LogParams , Logger , ParameterBuilder , ParameterInterface , ParameterVariant ,
24
+ Parameters , Publisher , PublisherOptions , PublisherState , RclrsError , Service , ServiceBase ,
25
+ ServiceOptions , ServiceState , Subscription , SubscriptionBase , SubscriptionCallback ,
26
+ SubscriptionOptions , SubscriptionState , TimeSource , ToLogParams , ENTITY_LIFECYCLE_MUTEX ,
27
27
} ;
28
28
29
29
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
@@ -59,7 +59,7 @@ unsafe impl Send for rcl_node_t {}
59
59
/// In that sense, the parameters to the node creation functions are only the _default_ namespace and
60
60
/// name.
61
61
/// See also the [official tutorial][1] on the command line arguments for ROS nodes, and the
62
- /// [`Node ::namespace()`][3] and [`Node ::name()`][4 ] functions for examples.
62
+ /// [`NodeState ::namespace()`] and [`NodeState ::name()`] functions for examples.
63
63
///
64
64
/// ## Rules for valid names
65
65
/// The rules for valid node names and node namespaces are explained in
@@ -72,7 +72,17 @@ unsafe impl Send for rcl_node_t {}
72
72
/// [5]: crate::NodeOptions::new
73
73
/// [6]: crate::NodeOptions::namespace
74
74
/// [7]: crate::Executor::create_node
75
- pub struct Node {
75
+ pub type Node = Arc < NodeState > ;
76
+
77
+ /// The inner state of a [`Node`].
78
+ ///
79
+ /// This is public so that you can choose to put it inside a [`Weak`] if you
80
+ /// want to be able to refer to a [`Node`] in a non-owning way. It is generally
81
+ /// recommended to manage the [`NodeState`] inside of an [`Arc`], and [`Node`]
82
+ /// is provided as convenience alias for that.
83
+ ///
84
+ /// The public API of the [`Node`] type is implemented via `NodeState`.
85
+ pub struct NodeState {
76
86
pub ( crate ) clients_mtx : Mutex < Vec < Weak < dyn ClientBase > > > ,
77
87
pub ( crate ) guard_conditions_mtx : Mutex < Vec < Weak < GuardCondition > > > ,
78
88
pub ( crate ) services_mtx : Mutex < Vec < Weak < dyn ServiceBase > > > ,
@@ -128,23 +138,23 @@ impl Drop for NodeHandle {
128
138
}
129
139
}
130
140
131
- impl Eq for Node { }
141
+ impl Eq for NodeState { }
132
142
133
- impl PartialEq for Node {
143
+ impl PartialEq for NodeState {
134
144
fn eq ( & self , other : & Self ) -> bool {
135
145
Arc :: ptr_eq ( & self . handle , & other. handle )
136
146
}
137
147
}
138
148
139
- impl fmt:: Debug for Node {
149
+ impl fmt:: Debug for NodeState {
140
150
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> Result < ( ) , fmt:: Error > {
141
151
f. debug_struct ( "Node" )
142
152
. field ( "fully_qualified_name" , & self . fully_qualified_name ( ) )
143
153
. finish ( )
144
154
}
145
155
}
146
156
147
- impl Node {
157
+ impl NodeState {
148
158
/// Returns the clock associated with this node.
149
159
pub fn get_clock ( & self ) -> Clock {
150
160
self . time_source . get_clock ( )
@@ -202,7 +212,7 @@ impl Node {
202
212
/// Returns the fully qualified name of the node.
203
213
///
204
214
/// The fully qualified name of the node is the node namespace combined with the node name.
205
- /// It is subject to the remappings shown in [`Node ::name()`] and [`Node ::namespace()`].
215
+ /// It is subject to the remappings shown in [`NodeState ::name()`] and [`NodeState ::namespace()`].
206
216
///
207
217
/// # Example
208
218
/// ```
@@ -266,11 +276,11 @@ impl Node {
266
276
pub fn create_client < ' a , T > (
267
277
self : & Arc < Self > ,
268
278
options : impl Into < ClientOptions < ' a > > ,
269
- ) -> Result < Arc < Client < T > > , RclrsError >
279
+ ) -> Result < Client < T > , RclrsError >
270
280
where
271
281
T : rosidl_runtime_rs:: Service ,
272
282
{
273
- let client = Arc :: new ( Client :: < T > :: new ( self , options) ?) ;
283
+ let client = Arc :: new ( ClientState :: < T > :: new ( self , options) ?) ;
274
284
{ self . clients_mtx . lock ( ) . unwrap ( ) } . push ( Arc :: downgrade ( & client) as Weak < dyn ClientBase > ) ;
275
285
Ok ( client)
276
286
}
@@ -349,11 +359,11 @@ impl Node {
349
359
pub fn create_publisher < ' a , T > (
350
360
& self ,
351
361
options : impl Into < PublisherOptions < ' a > > ,
352
- ) -> Result < Arc < Publisher < T > > , RclrsError >
362
+ ) -> Result < Publisher < T > , RclrsError >
353
363
where
354
364
T : Message ,
355
365
{
356
- let publisher = Arc :: new ( Publisher :: < T > :: new ( Arc :: clone ( & self . handle ) , options) ?) ;
366
+ let publisher = Arc :: new ( PublisherState :: < T > :: new ( Arc :: clone ( & self . handle ) , options) ?) ;
357
367
Ok ( publisher)
358
368
}
359
369
@@ -403,12 +413,12 @@ impl Node {
403
413
self : & Arc < Self > ,
404
414
options : impl Into < ServiceOptions < ' a > > ,
405
415
callback : F ,
406
- ) -> Result < Arc < Service < T > > , RclrsError >
416
+ ) -> Result < Service < T > , RclrsError >
407
417
where
408
418
T : rosidl_runtime_rs:: Service ,
409
419
F : Fn ( & rmw_request_id_t , T :: Request ) -> T :: Response + ' static + Send ,
410
420
{
411
- let service = Arc :: new ( Service :: < T > :: new ( self , options, callback) ?) ;
421
+ let service = Arc :: new ( ServiceState :: < T > :: new ( self , options, callback) ?) ;
412
422
{ self . services_mtx . lock ( ) . unwrap ( ) }
413
423
. push ( Arc :: downgrade ( & service) as Weak < dyn ServiceBase > ) ;
414
424
Ok ( service)
@@ -459,11 +469,11 @@ impl Node {
459
469
self : & Arc < Self > ,
460
470
options : impl Into < SubscriptionOptions < ' a > > ,
461
471
callback : impl SubscriptionCallback < T , Args > ,
462
- ) -> Result < Arc < Subscription < T > > , RclrsError >
472
+ ) -> Result < Subscription < T > , RclrsError >
463
473
where
464
474
T : Message ,
465
475
{
466
- let subscription = Arc :: new ( Subscription :: < T > :: new ( self , options, callback) ?) ;
476
+ let subscription = Arc :: new ( SubscriptionState :: < T > :: new ( self , options, callback) ?) ;
467
477
{ self . subscriptions_mtx . lock ( ) }
468
478
. unwrap ( )
469
479
. push ( Arc :: downgrade ( & subscription) as Weak < dyn SubscriptionBase > ) ;
@@ -583,7 +593,7 @@ impl Node {
583
593
}
584
594
}
585
595
586
- impl < ' a > ToLogParams < ' a > for & ' a Node {
596
+ impl < ' a > ToLogParams < ' a > for & ' a NodeState {
587
597
fn to_log_params ( self ) -> LogParams < ' a > {
588
598
self . logger ( ) . to_log_params ( )
589
599
}
0 commit comments