Skip to content

Commit bf00394

Browse files
committed
* Changed the timer callback type to an FnMut, added some helper docs
1 parent c44dfce commit bf00394

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

rclrs/src/executor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ mod tests {
9898
let context = Context::new([])?;
9999
let node = Node::new(&context, "test_spin_timer")?;
100100

101-
let callback_triggered = Arc::new(Mutex::new(false));
101+
let callback_triggered = Arc::new(Mutex::new(0));
102102
let callback_flag = Arc::clone(&callback_triggered);
103103

104104
let _timer = node.create_timer(Duration::from_secs(0), move |_| {
105-
*callback_flag.lock().unwrap() = true;
105+
*callback_flag.lock().unwrap() += 1;
106106
})?;
107107

108108
spin_once(node, Some(Duration::ZERO))?;
109109

110-
assert!(*callback_triggered.lock().unwrap());
110+
assert_eq!(*callback_triggered.lock().unwrap(), 1);
111111
Ok(())
112112
}
113113
}

rclrs/src/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl Node {
336336
callback: F,
337337
) -> Result<Arc<Mutex<Timer>>, RclrsError>
338338
where
339-
F: Fn(&mut Timer) + 'static + Send + Sync,
339+
F: FnMut(&mut Timer) + 'static + Send + Sync,
340340
{
341341
let timer = Arc::new(Mutex::new(Timer::new_with_context_handle(
342342
Arc::clone(&self.handle.context_handle),

rclrs/src/timer.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub trait TimerBase: Send + Sync {
7373
/// [3]: crate::Node::create_timer
7474
/// [4]: crate::Node
7575
pub struct Timer {
76-
callback: Arc<dyn Fn(&mut Timer) + Send + Sync>,
76+
callback: Arc<Mutex<dyn FnMut(&mut Timer) + Send + Sync>>,
7777
handle: TimerHandle,
7878
}
7979

@@ -87,7 +87,7 @@ impl Timer {
8787
callback: F,
8888
) -> Result<Self, RclrsError>
8989
where
90-
F: Fn(&mut Timer) + 'static + Send + Sync,
90+
F: FnMut(&mut Timer) + 'static + Send + Sync,
9191
{
9292
Timer::new_with_context_handle(Arc::clone(&context.handle), clock, period, callback)
9393
}
@@ -100,10 +100,10 @@ impl Timer {
100100
callback: F,
101101
) -> Result<Self, RclrsError>
102102
where
103-
F: Fn(&mut Timer) + 'static + Send + Sync,
103+
F: FnMut(&mut Timer) + 'static + Send + Sync,
104104
{
105105
// Move the callback to our reference counted container so rcl_callback can use it
106-
let callback = Arc::new(callback);
106+
let callback = Arc::new(Mutex::new(callback));
107107

108108
// SAFETY: Getting a zero-initialized value is always safe.
109109
let mut rcl_timer = unsafe { rcl_get_zero_initialized_timer() };
@@ -227,6 +227,11 @@ impl Timer {
227227
}
228228

229229
/// Set the period of the timer. Periods greater than i64::MAX nanoseconds will saturate to i64::MAX.
230+
///
231+
/// The updated period will not take affect until either [`reset`][1] is called
232+
/// or the timer next expires, whichever comes first.
233+
///
234+
/// [1]: crate::Timer::reset
230235
pub fn set_period(&self, period: Duration) {
231236
let timer = self.handle.lock();
232237
let new_period = i64::try_from(period.as_nanos()).unwrap_or(i64::MAX);
@@ -304,7 +309,7 @@ impl TimerBase for Timer {
304309
self.call_rcl()?;
305310

306311
let callback = self.callback.clone();
307-
callback(self);
312+
(*callback.lock().unwrap())(self);
308313

309314
Ok(())
310315
}

0 commit comments

Comments
 (0)