Skip to content

Commit 4956c31

Browse files
authored
Add WaitSet::new_for_node() (#276)
This function is important for users who directly use wait sets, since the live_subscriptions() etc. functions are not public.
1 parent 3fb8ea5 commit 4956c31

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

rclrs/src/lib.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,7 @@ pub use wait::*;
4646
///
4747
/// [1]: crate::RclReturnCode
4848
pub fn spin_once(node: &Node, timeout: Option<Duration>) -> Result<(), RclrsError> {
49-
let live_subscriptions = node.live_subscriptions();
50-
let live_clients = node.live_clients();
51-
let live_guard_conditions = node.live_guard_conditions();
52-
let live_services = node.live_services();
53-
let ctx = Context {
54-
rcl_context_mtx: node.rcl_context_mtx.clone(),
55-
};
56-
let mut wait_set = WaitSet::new(
57-
live_subscriptions.len(),
58-
live_guard_conditions.len(),
59-
0,
60-
live_clients.len(),
61-
live_services.len(),
62-
0,
63-
&ctx,
64-
)?;
65-
66-
for live_subscription in &live_subscriptions {
67-
wait_set.add_subscription(live_subscription.clone())?;
68-
}
69-
70-
for live_client in &live_clients {
71-
wait_set.add_client(live_client.clone())?;
72-
}
73-
74-
for live_guard_condition in &live_guard_conditions {
75-
wait_set.add_guard_condition(live_guard_condition.clone())?;
76-
}
77-
78-
for live_service in &live_services {
79-
wait_set.add_service(live_service.clone())?;
80-
}
81-
49+
let mut wait_set = WaitSet::new_for_node(node)?;
8250
let ready_entities = wait_set.wait(timeout)?;
8351

8452
for ready_subscription in ready_entities.subscriptions {

rclrs/src/wait.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::vec::Vec;
2121

2222
use crate::error::{to_rclrs_result, RclReturnCode, RclrsError, ToResult};
2323
use crate::rcl_bindings::*;
24-
use crate::{ClientBase, Context, ServiceBase, SubscriptionBase};
24+
use crate::{ClientBase, Context, Node, ServiceBase, SubscriptionBase};
2525

2626
mod exclusivity_guard;
2727
mod 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

Comments
 (0)