@@ -2,10 +2,12 @@ use rosidl_runtime_rs::{Message, Service as IdlService};
22use std:: { any:: Any , sync:: { Arc , Mutex , Weak } } ;
33use futures:: channel:: oneshot;
44use crate :: {
5- WorkerCommands , NodeHandle , ToLogParams , Promise , log_fatal,
5+ WorkerCommands , ToLogParams , Promise , log_fatal,
66 IntoWorkerSubscriptionCallback , IntoWorkerServiceCallback ,
77 WorkerSubscription , SubscriptionState , WorkerService , ServiceState ,
88 SubscriptionOptions , ServiceOptions , RclrsError , Node ,
9+ IntoWorkerTimerRepeatingCallback , IntoWorkerTimerOneshotCallback ,
10+ IntoTimerOptions , AnyTimerCallback , WorkerTimer , TimerState ,
911} ;
1012
1113/// A worker that carries a payload and synchronizes callbacks for subscriptions
@@ -33,7 +35,7 @@ pub type Worker<Payload> = Arc<WorkerState<Payload>>;
3335/// [1]: std::sync::Weak
3436pub struct WorkerState < Payload > {
3537 /// The node that this worker is associated with
36- node : Arc < NodeHandle > ,
38+ node : Node ,
3739 /// The commands to communicate with the runtime of the worker
3840 commands : Arc < WorkerCommands > ,
3941 _ignore : std:: marker:: PhantomData < Payload > ,
@@ -159,7 +161,7 @@ impl<Payload: 'static + Send + Sync> WorkerState<Payload> {
159161 SubscriptionState :: < T , Worker < Payload > > :: create (
160162 options,
161163 callback. into_worker_subscription_callback ( ) ,
162- & self . node ,
164+ self . node . handle ( ) ,
163165 & self . commands ,
164166 )
165167 }
@@ -176,16 +178,75 @@ impl<Payload: 'static + Send + Sync> WorkerState<Payload> {
176178 ServiceState :: < T , Worker < Payload > > :: create (
177179 options,
178180 callback. into_worker_service_callback ( ) ,
179- & self . node ,
181+ self . node . handle ( ) ,
180182 & self . commands ,
181183 )
182184 }
183185
186+ /// Create a [`WorkerTimer`] with a repeating callback.
187+ ///
188+ /// See also:
189+ /// * [`Self::create_timer_oneshot`]
190+ /// * [`Self::create_timer_inert`]
191+ pub fn create_timer_repeating < ' a , Args > (
192+ & self ,
193+ options : impl IntoTimerOptions < ' a > ,
194+ callback : impl IntoWorkerTimerRepeatingCallback < Worker < Payload > , Args > ,
195+ ) -> Result < WorkerTimer < Payload > , RclrsError > {
196+ self . create_timer ( options, callback. into_worker_timer_repeating_callback ( ) )
197+ }
198+
199+ /// Create a [`WorkerTimer`] whose callback will be triggered once after the
200+ /// period of the timer has elapsed. After that you will need to use
201+ /// [`WorkerTimer::set_worker_oneshot`] or [`WorkerTimer::set_worker_repeating`]
202+ /// or else nothing will happen the following times that the `Timer` elapses.
203+ ///
204+ /// See also:
205+ /// * [`Self::create_timer_repeating`]
206+ /// * [`Self::create_time_inert`]
207+ pub fn create_timer_oneshot < ' a , Args > (
208+ & self ,
209+ options : impl IntoTimerOptions < ' a > ,
210+ callback : impl IntoWorkerTimerOneshotCallback < Worker < Payload > , Args > ,
211+ ) -> Result < WorkerTimer < Payload > , RclrsError > {
212+ self . create_timer ( options, callback. into_worker_timer_oneshot_callback ( ) )
213+ }
214+
215+ /// Create a [`WorkerTimer`] without a callback. Nothing will happen when this
216+ /// `WorkerTimer` elapses until you use [`WorkerTimer::set_worker_repeating`]
217+ /// or [`WorkerTimer::set_worker_oneshot`].
218+ ///
219+ /// See also:
220+ /// * [`Self::create_timer_repeating`]
221+ /// * [`Self::create_timer_oneshot`]
222+ pub fn create_timer_inert < ' a > (
223+ & self ,
224+ options : impl IntoTimerOptions < ' a > ,
225+ ) -> Result < WorkerTimer < Payload > , RclrsError > {
226+ self . create_timer ( options, AnyTimerCallback :: Inert )
227+ }
228+
229+ fn create_timer < ' a > (
230+ & self ,
231+ options : impl IntoTimerOptions < ' a > ,
232+ callback : AnyTimerCallback < Worker < Payload > > ,
233+ ) -> Result < WorkerTimer < Payload > , RclrsError > {
234+ let options = options. into_timer_options ( ) ;
235+ let clock = options. clock . as_clock ( & * self . node ) ;
236+ TimerState :: create (
237+ options. period ,
238+ clock,
239+ callback,
240+ & self . commands ,
241+ & self . node . handle ( ) . context_handle ,
242+ )
243+ }
244+
184245 /// Used by [`Node`][crate::Node] to create a `WorkerState`. Users should
185246 /// call [`Node::create_worker`][crate::NodeState::create_worker] instead of
186247 /// this.
187248 pub ( crate ) fn create (
188- node : Arc < NodeHandle > ,
249+ node : Node ,
189250 commands : Arc < WorkerCommands > ,
190251 ) -> Arc < Self > {
191252 Arc :: new ( Self { node, commands, _ignore : Default :: default ( ) } )
0 commit comments