@@ -15,6 +15,12 @@ use std::{
15
15
time:: Duration ,
16
16
} ;
17
17
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
+
18
24
// SAFETY: The functions accessing this type, including drop(), shouldn't care
19
25
// about the thread they are running in (partly because they're protected by mutex).
20
26
// Therefore, this type can be safely sent to another thread.
@@ -76,7 +82,7 @@ pub trait TimerBase: Send + Sync {
76
82
/// [5]: crate::Timer::execute
77
83
/// [6]: crate::WaitSet
78
84
pub struct Timer {
79
- callback : Arc < Mutex < dyn FnMut ( & mut Timer ) + Send + Sync > > ,
85
+ callback : Arc < Mutex < dyn TimerCallback > > ,
80
86
handle : TimerHandle ,
81
87
}
82
88
@@ -96,7 +102,7 @@ impl Timer {
96
102
callback : F ,
97
103
) -> Result < Self , RclrsError >
98
104
where
99
- F : FnMut ( & mut Timer ) + ' static + Send + Sync ,
105
+ F : TimerCallback + ' static ,
100
106
{
101
107
Timer :: new_with_context_handle ( Arc :: clone ( & context. handle ) , clock, period, callback)
102
108
}
@@ -109,7 +115,7 @@ impl Timer {
109
115
callback : F ,
110
116
) -> Result < Self , RclrsError >
111
117
where
112
- F : FnMut ( & mut Timer ) + ' static + Send + Sync ,
118
+ F : TimerCallback + ' static ,
113
119
{
114
120
let callback = Arc :: new ( Mutex :: new ( callback) ) ;
115
121
0 commit comments