Skip to content

Commit bd2da54

Browse files
Private new takes Box<dyn Fn()> instead of generic for usability
1 parent 7722761 commit bd2da54

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

rclrs/src/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl Node {
203203
pub fn create_guard_condition(&mut self) -> Arc<GuardCondition> {
204204
let guard_condition = Arc::new(GuardCondition::new_with_rcl_context(
205205
&mut self.rcl_context_mtx.lock().unwrap(),
206-
None::<fn()>,
206+
None,
207207
));
208208
self.guard_conditions
209209
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);
@@ -225,7 +225,7 @@ impl Node {
225225
{
226226
let guard_condition = Arc::new(GuardCondition::new_with_rcl_context(
227227
&mut self.rcl_context_mtx.lock().unwrap(),
228-
Some(callback),
228+
Some(Box::new(callback) as Box<dyn Fn() + Send + Sync>),
229229
));
230230
self.guard_conditions
231231
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);

rclrs/src/wait/guard_condition.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,28 @@ unsafe impl Send for rcl_guard_condition_t {}
8080
impl GuardCondition {
8181
/// Creates a new guard condition with no callback.
8282
pub fn new(context: &Context) -> Self {
83-
Self::new_with_rcl_context(&mut context.rcl_context_mtx.lock().unwrap(), None::<fn()>)
83+
Self::new_with_rcl_context(&mut context.rcl_context_mtx.lock().unwrap(), None)
8484
}
8585

8686
/// Creates a new guard condition with a callback.
8787
pub fn new_with_callback<F>(context: &Context, callback: F) -> Self
8888
where
8989
F: Fn() + Send + Sync + 'static,
9090
{
91-
Self::new_with_rcl_context(&mut context.rcl_context_mtx.lock().unwrap(), Some(callback))
91+
Self::new_with_rcl_context(
92+
&mut context.rcl_context_mtx.lock().unwrap(),
93+
Some(Box::new(callback) as Box<dyn Fn() + Send + Sync>),
94+
)
9295
}
9396

9497
/// Creates a new guard condition by providing the rcl_context_t and an optional callback.
9598
/// Note this function enables calling `Node::create_guard_condition`[1] without providing the Context separately
9699
///
97100
/// [1]: Node::create_guard_condition
98-
pub(crate) fn new_with_rcl_context<F>(context: &mut rcl_context_t, callback: Option<F>) -> Self
99-
where
100-
F: Fn() + Send + Sync + 'static,
101-
{
101+
pub(crate) fn new_with_rcl_context(
102+
context: &mut rcl_context_t,
103+
callback: Option<Box<dyn Fn() + Send + Sync>>,
104+
) -> Self {
102105
// SAFETY: Getting a zero initialized value is always safe
103106
let mut guard_condition = unsafe { rcl_get_zero_initialized_guard_condition() };
104107
unsafe {
@@ -112,7 +115,7 @@ impl GuardCondition {
112115

113116
Self {
114117
rcl_guard_condition: Mutex::new(guard_condition),
115-
callback: callback.map(|f| Box::new(f) as Box<dyn Fn() + Send + Sync>),
118+
callback,
116119
in_use_by_wait_set: Arc::new(AtomicBool::new(false)),
117120
}
118121
}

0 commit comments

Comments
 (0)