@@ -21,7 +21,7 @@ use std::vec::Vec;
2121
2222use crate :: error:: { to_rclrs_result, RclReturnCode , RclrsError , ToResult } ;
2323use crate :: rcl_bindings:: * ;
24- use crate :: { ClientBase , Context , ServiceBase , SubscriptionBase } ;
24+ use crate :: { ClientBase , Context , Node , ServiceBase , SubscriptionBase } ;
2525
2626mod exclusivity_guard;
2727mod guard_condition;
@@ -117,6 +117,45 @@ impl WaitSet {
117117 } )
118118 }
119119
120+ /// Creates a new wait set and adds all waitable entities in the node to it.
121+ ///
122+ /// The wait set is sized to fit the node exactly, so there is no capacity for adding other entities.
123+ pub fn new_for_node ( node : & Node ) -> Result < Self , RclrsError > {
124+ let live_subscriptions = node. live_subscriptions ( ) ;
125+ let live_clients = node. live_clients ( ) ;
126+ let live_guard_conditions = node. live_guard_conditions ( ) ;
127+ let live_services = node. live_services ( ) ;
128+ let ctx = Context {
129+ rcl_context_mtx : node. rcl_context_mtx . clone ( ) ,
130+ } ;
131+ let mut wait_set = WaitSet :: new (
132+ live_subscriptions. len ( ) ,
133+ live_guard_conditions. len ( ) ,
134+ 0 ,
135+ live_clients. len ( ) ,
136+ live_services. len ( ) ,
137+ 0 ,
138+ & ctx,
139+ ) ?;
140+
141+ for live_subscription in & live_subscriptions {
142+ wait_set. add_subscription ( live_subscription. clone ( ) ) ?;
143+ }
144+
145+ for live_client in & live_clients {
146+ wait_set. add_client ( live_client. clone ( ) ) ?;
147+ }
148+
149+ for live_guard_condition in & live_guard_conditions {
150+ wait_set. add_guard_condition ( live_guard_condition. clone ( ) ) ?;
151+ }
152+
153+ for live_service in & live_services {
154+ wait_set. add_service ( live_service. clone ( ) ) ?;
155+ }
156+ Ok ( wait_set)
157+ }
158+
120159 /// Removes all entities from the wait set.
121160 ///
122161 /// This effectively resets the wait set to the state it was in after being created by
0 commit comments