@@ -22,11 +22,11 @@ use std::sync::{atomic::AtomicBool, Arc, Mutex};
2222/// let atomic_bool = Arc::new(std::sync::atomic::AtomicBool::new(false));
2323/// let atomic_bool_for_closure = Arc::clone(&atomic_bool);
2424///
25- /// let gc = Arc::new(GuardCondition::new (
25+ /// let gc = Arc::new(GuardCondition::new_with_callback (
2626/// &context,
27- /// Some(Box::new( move || {
27+ /// move || {
2828/// atomic_bool_for_closure.store(true, Ordering::Relaxed);
29- /// })) ,
29+ /// },
3030/// ));
3131///
3232/// let mut ws = WaitSet::new(0, 1, 0, 0, 0, 0, &context)?;
@@ -78,8 +78,24 @@ impl Eq for GuardCondition {}
7878unsafe impl Send for rcl_guard_condition_t { }
7979
8080impl 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
8399 where
84100 F : Fn ( ) + Send + Sync + ' static ,
85101 {
@@ -89,7 +105,7 @@ impl GuardCondition {
89105 // SAFETY: The context must be valid, and the guard condition must be zero-initialized
90106 rcl_guard_condition_init (
91107 & mut guard_condition,
92- & mut * context. rcl_context_mtx . lock ( ) . unwrap ( ) ,
108+ context,
93109 rcl_guard_condition_get_default_options ( ) ,
94110 ) ;
95111 }
@@ -127,12 +143,9 @@ mod tests {
127143 let atomic_bool = Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
128144 let atomic_bool_for_closure = Arc :: clone ( & atomic_bool) ;
129145
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+ } ) ;
136149
137150 guard_condition. trigger ( ) ?;
138151
@@ -148,12 +161,9 @@ mod tests {
148161 let atomic_bool = Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
149162 let atomic_bool_for_closure = Arc :: clone ( & atomic_bool) ;
150163
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+ } ) ) ;
157167
158168 let mut wait_set = WaitSet :: new ( 0 , 1 , 0 , 0 , 0 , 0 , & context) ?;
159169 wait_set. add_guard_condition ( Arc :: clone ( & guard_condition) ) ?;
0 commit comments