@@ -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
7884pub 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