@@ -22,11 +22,11 @@ use std::sync::{atomic::AtomicBool, Arc, Mutex};
22
22
/// let atomic_bool = Arc::new(std::sync::atomic::AtomicBool::new(false));
23
23
/// let atomic_bool_for_closure = Arc::clone(&atomic_bool);
24
24
///
25
- /// let gc = Arc::new(GuardCondition::new (
25
+ /// let gc = Arc::new(GuardCondition::new_with_callback (
26
26
/// &context,
27
- /// Some(Box::new( move || {
27
+ /// move || {
28
28
/// atomic_bool_for_closure.store(true, Ordering::Relaxed);
29
- /// })) ,
29
+ /// },
30
30
/// ));
31
31
///
32
32
/// let mut ws = WaitSet::new(0, 1, 0, 0, 0, 0, &context)?;
@@ -78,8 +78,24 @@ impl Eq for GuardCondition {}
78
78
unsafe impl Send for rcl_guard_condition_t { }
79
79
80
80
impl GuardCondition {
81
- /// Creates a new guard condition.
82
- pub fn new < F > ( context : & Context , callback : Option < F > ) -> Self
81
+ /// Creates a new guard condition with no callback.
82
+ pub fn new ( context : & Context ) -> Self {
83
+ Self :: new_with_rcl_context ( & mut context. rcl_context_mtx . lock ( ) . unwrap ( ) , None :: < fn ( ) > )
84
+ }
85
+
86
+ /// Creates a new guard condition with a callback.
87
+ pub fn new_with_callback < F > ( context : & Context , callback : F ) -> Self
88
+ where
89
+ F : Fn ( ) + Send + Sync + ' static ,
90
+ {
91
+ Self :: new_with_rcl_context ( & mut context. rcl_context_mtx . lock ( ) . unwrap ( ) , Some ( callback) )
92
+ }
93
+
94
+ /// Creates a new guard condition by providing the rcl_context_t and an optional callback.
95
+ /// Note this function enables calling `Node::create_guard_condition`[1] without providing the Context separately
96
+ ///
97
+ /// [1]: Node::create_guard_condition
98
+ pub ( crate ) fn new_with_rcl_context < F > ( context : & mut rcl_context_t , callback : Option < F > ) -> Self
83
99
where
84
100
F : Fn ( ) + Send + Sync + ' static ,
85
101
{
@@ -89,7 +105,7 @@ impl GuardCondition {
89
105
// SAFETY: The context must be valid, and the guard condition must be zero-initialized
90
106
rcl_guard_condition_init (
91
107
& mut guard_condition,
92
- & mut * context. rcl_context_mtx . lock ( ) . unwrap ( ) ,
108
+ context,
93
109
rcl_guard_condition_get_default_options ( ) ,
94
110
) ;
95
111
}
@@ -127,12 +143,9 @@ mod tests {
127
143
let atomic_bool = Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
128
144
let atomic_bool_for_closure = Arc :: clone ( & atomic_bool) ;
129
145
130
- let guard_condition = GuardCondition :: new (
131
- & context,
132
- Some ( Box :: new ( move || {
133
- atomic_bool_for_closure. store ( true , Ordering :: Relaxed ) ;
134
- } ) ) ,
135
- ) ;
146
+ let guard_condition = GuardCondition :: new_with_callback ( & context, move || {
147
+ atomic_bool_for_closure. store ( true , Ordering :: Relaxed ) ;
148
+ } ) ;
136
149
137
150
guard_condition. trigger ( ) ?;
138
151
@@ -148,12 +161,9 @@ mod tests {
148
161
let atomic_bool = Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
149
162
let atomic_bool_for_closure = Arc :: clone ( & atomic_bool) ;
150
163
151
- let guard_condition = Arc :: new ( GuardCondition :: new (
152
- & context,
153
- Some ( Box :: new ( move || {
154
- atomic_bool_for_closure. store ( true , Ordering :: Relaxed ) ;
155
- } ) ) ,
156
- ) ) ;
164
+ let guard_condition = Arc :: new ( GuardCondition :: new_with_callback ( & context, move || {
165
+ atomic_bool_for_closure. store ( true , Ordering :: Relaxed ) ;
166
+ } ) ) ;
157
167
158
168
let mut wait_set = WaitSet :: new ( 0 , 1 , 0 , 0 , 0 , 0 , & context) ?;
159
169
wait_set. add_guard_condition ( Arc :: clone ( & guard_condition) ) ?;
0 commit comments