Skip to content

Commit c1cdfa8

Browse files
committed
* Added subtrait for TimerCallback to address clippy error
1 parent 6cbc29c commit c1cdfa8

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

rclrs/src/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
rcl_bindings::*, Client, ClientBase, Clock, Context, ContextHandle, GuardCondition,
1818
ParameterBuilder, ParameterInterface, ParameterVariant, Parameters, Publisher, QoSProfile,
1919
RclrsError, Service, ServiceBase, Subscription, SubscriptionBase, SubscriptionCallback,
20-
TimeSource, Timer, TimerBase, ENTITY_LIFECYCLE_MUTEX,
20+
TimeSource, Timer, TimerBase, TimerCallback, ENTITY_LIFECYCLE_MUTEX,
2121
};
2222

2323
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
@@ -336,7 +336,7 @@ impl Node {
336336
callback: F,
337337
) -> Result<Arc<Mutex<Timer>>, RclrsError>
338338
where
339-
F: FnMut(&mut Timer) + 'static + Send + Sync,
339+
F: TimerCallback + 'static,
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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ use std::{
1515
time::Duration,
1616
};
1717

18+
/// A trait for the callback function of a timer.
19+
pub trait TimerCallback: FnMut(&mut Timer) + Send + Sync {}
20+
21+
// Blanket implementation of TimerCallback for all types that implement the necessary traits.
22+
impl<T: FnMut(&mut Timer) + Send + Sync> TimerCallback for T {}
23+
1824
// SAFETY: The functions accessing this type, including drop(), shouldn't care
1925
// about the thread they are running in (partly because they're protected by mutex).
2026
// Therefore, this type can be safely sent to another thread.
@@ -76,7 +82,7 @@ pub trait TimerBase: Send + Sync {
7682
/// [5]: crate::Timer::execute
7783
/// [6]: crate::WaitSet
7884
pub struct Timer {
79-
callback: Arc<Mutex<dyn FnMut(&mut Timer) + Send + Sync>>,
85+
callback: Arc<Mutex<dyn TimerCallback>>,
8086
handle: TimerHandle,
8187
}
8288

@@ -96,7 +102,7 @@ impl Timer {
96102
callback: F,
97103
) -> Result<Self, RclrsError>
98104
where
99-
F: FnMut(&mut Timer) + 'static + Send + Sync,
105+
F: TimerCallback + 'static,
100106
{
101107
Timer::new_with_context_handle(Arc::clone(&context.handle), clock, period, callback)
102108
}
@@ -109,7 +115,7 @@ impl Timer {
109115
callback: F,
110116
) -> Result<Self, RclrsError>
111117
where
112-
F: FnMut(&mut Timer) + 'static + Send + Sync,
118+
F: TimerCallback + 'static,
113119
{
114120
let callback = Arc::new(Mutex::new(callback));
115121

0 commit comments

Comments
 (0)