@@ -2,10 +2,12 @@ use rosidl_runtime_rs::{Message, Service as IdlService};
2
2
use std:: { any:: Any , sync:: { Arc , Mutex , Weak } } ;
3
3
use futures:: channel:: oneshot;
4
4
use crate :: {
5
- WorkerCommands , NodeHandle , ToLogParams , Promise , log_fatal,
5
+ WorkerCommands , ToLogParams , Promise , log_fatal,
6
6
IntoWorkerSubscriptionCallback , IntoWorkerServiceCallback ,
7
7
WorkerSubscription , SubscriptionState , WorkerService , ServiceState ,
8
8
SubscriptionOptions , ServiceOptions , RclrsError , Node ,
9
+ IntoWorkerTimerRepeatingCallback , IntoWorkerTimerOneshotCallback ,
10
+ IntoTimerOptions , AnyTimerCallback , WorkerTimer , TimerState ,
9
11
} ;
10
12
11
13
/// A worker that carries a payload and synchronizes callbacks for subscriptions
@@ -33,7 +35,7 @@ pub type Worker<Payload> = Arc<WorkerState<Payload>>;
33
35
/// [1]: std::sync::Weak
34
36
pub struct WorkerState < Payload > {
35
37
/// The node that this worker is associated with
36
- node : Arc < NodeHandle > ,
38
+ node : Node ,
37
39
/// The commands to communicate with the runtime of the worker
38
40
commands : Arc < WorkerCommands > ,
39
41
_ignore : std:: marker:: PhantomData < Payload > ,
@@ -159,7 +161,7 @@ impl<Payload: 'static + Send + Sync> WorkerState<Payload> {
159
161
SubscriptionState :: < T , Worker < Payload > > :: create (
160
162
options,
161
163
callback. into_worker_subscription_callback ( ) ,
162
- & self . node ,
164
+ self . node . handle ( ) ,
163
165
& self . commands ,
164
166
)
165
167
}
@@ -176,16 +178,75 @@ impl<Payload: 'static + Send + Sync> WorkerState<Payload> {
176
178
ServiceState :: < T , Worker < Payload > > :: create (
177
179
options,
178
180
callback. into_worker_service_callback ( ) ,
179
- & self . node ,
181
+ self . node . handle ( ) ,
180
182
& self . commands ,
181
183
)
182
184
}
183
185
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
+
184
245
/// Used by [`Node`][crate::Node] to create a `WorkerState`. Users should
185
246
/// call [`Node::create_worker`][crate::NodeState::create_worker] instead of
186
247
/// this.
187
248
pub ( crate ) fn create (
188
- node : Arc < NodeHandle > ,
249
+ node : Node ,
189
250
commands : Arc < WorkerCommands > ,
190
251
) -> Arc < Self > {
191
252
Arc :: new ( Self { node, commands, _ignore : Default :: default ( ) } )
0 commit comments