1
1
use crate :: {
2
- clock:: Clock ,
3
- context:: ContextHandle ,
4
- error:: RclrsError ,
5
- rcl_bindings:: * ,
6
- ToResult , log_error, ToLogParams , ENTITY_LIFECYCLE_MUTEX ,
2
+ clock:: Clock , context:: ContextHandle , error:: RclrsError , log_error, rcl_bindings:: * ,
3
+ ToLogParams , ToResult , ENTITY_LIFECYCLE_MUTEX ,
7
4
} ;
8
5
// TODO: fix me when the callback type is properly defined.
9
6
// use std::fmt::Debug;
@@ -58,7 +55,8 @@ impl Timer {
58
55
unsafe {
59
56
let rcl_timer = self . handle . rcl_timer . lock ( ) . unwrap ( ) ;
60
57
rcl_timer_get_period ( & * rcl_timer, & mut timer_period_ns)
61
- } . ok ( ) ?;
58
+ }
59
+ . ok ( ) ?;
62
60
63
61
rcl_duration ( timer_period_ns)
64
62
}
@@ -76,7 +74,8 @@ impl Timer {
76
74
unsafe {
77
75
let rcl_timer = self . handle . rcl_timer . lock ( ) . unwrap ( ) ;
78
76
rcl_timer_is_canceled ( & * rcl_timer, & mut is_canceled)
79
- } . ok ( ) ?;
77
+ }
78
+ . ok ( ) ?;
80
79
Ok ( is_canceled)
81
80
}
82
81
@@ -86,7 +85,8 @@ impl Timer {
86
85
unsafe {
87
86
let rcl_timer = self . handle . rcl_timer . lock ( ) . unwrap ( ) ;
88
87
rcl_timer_get_time_since_last_call ( & * rcl_timer, & mut time_value_ns)
89
- } . ok ( ) ?;
88
+ }
89
+ . ok ( ) ?;
90
90
91
91
rcl_duration ( time_value_ns)
92
92
}
@@ -97,7 +97,8 @@ impl Timer {
97
97
unsafe {
98
98
let rcl_timer = self . handle . rcl_timer . lock ( ) . unwrap ( ) ;
99
99
rcl_timer_get_time_until_next_call ( & * rcl_timer, & mut time_value_ns)
100
- } . ok ( ) ?;
100
+ }
101
+ . ok ( ) ?;
101
102
102
103
rcl_duration ( time_value_ns)
103
104
}
@@ -137,7 +138,10 @@ impl Timer {
137
138
/// See also:
138
139
/// * [`Self::set_oneshot`]
139
140
/// * [`Self::remove_callback`]
140
- pub fn set_repeating < Args > ( & self , f : impl TimerCallRepeating < Args > ) -> Option < AnyTimerCallback > {
141
+ pub fn set_repeating < Args > (
142
+ & self ,
143
+ f : impl TimerCallRepeating < Args > ,
144
+ ) -> Option < AnyTimerCallback > {
141
145
self . set_callback ( f. into_repeating_timer_callback ( ) )
142
146
}
143
147
@@ -188,7 +192,7 @@ impl Timer {
188
192
189
193
let rcl_timer = Arc :: new ( Mutex :: new (
190
194
// SAFETY: Zero-initializing a timer is always safe
191
- unsafe { rcl_get_zero_initialized_timer ( ) }
195
+ unsafe { rcl_get_zero_initialized_timer ( ) } ,
192
196
) ) ;
193
197
194
198
unsafe {
@@ -209,7 +213,8 @@ impl Timer {
209
213
rcl_timer_callback,
210
214
allocator,
211
215
)
212
- } . ok ( ) ?;
216
+ }
217
+ . ok ( ) ?;
213
218
214
219
let timer = Timer {
215
220
handle : TimerHandle { rcl_timer, clock } ,
@@ -249,10 +254,7 @@ impl Timer {
249
254
}
250
255
251
256
if let Err ( err) = self . rcl_call ( ) {
252
- log_error ! (
253
- "timer" ,
254
- "Unable to call timer: {err:?}" ,
255
- ) ;
257
+ log_error ! ( "timer" , "Unable to call timer: {err:?}" , ) ;
256
258
}
257
259
258
260
Ok ( ( ) )
@@ -310,7 +312,10 @@ unsafe impl Send for rcl_timer_t {}
310
312
#[ cfg( test) ]
311
313
mod tests {
312
314
use crate :: * ;
313
- use std:: { thread, time, sync:: atomic:: { AtomicBool , Ordering } } ;
315
+ use std:: {
316
+ sync:: atomic:: { AtomicBool , Ordering } ,
317
+ thread, time,
318
+ } ;
314
319
315
320
#[ test]
316
321
fn traits ( ) {
@@ -327,7 +332,7 @@ mod tests {
327
332
& context. handle ,
328
333
Duration :: from_millis ( 1 ) ,
329
334
Clock :: system ( ) ,
330
- ( || { } ) . into_repeating_timer_callback ( ) ,
335
+ ( || { } ) . into_repeating_timer_callback ( ) ,
331
336
) ;
332
337
assert ! ( result. is_ok( ) ) ;
333
338
}
@@ -339,7 +344,7 @@ mod tests {
339
344
& context. handle ,
340
345
Duration :: from_millis ( 1 ) ,
341
346
Clock :: steady ( ) ,
342
- ( || { } ) . into_repeating_timer_callback ( ) ,
347
+ ( || { } ) . into_repeating_timer_callback ( ) ,
343
348
) ;
344
349
assert ! ( result. is_ok( ) ) ;
345
350
}
@@ -360,7 +365,7 @@ mod tests {
360
365
& context. handle ,
361
366
Duration :: from_millis ( 1 ) ,
362
367
clock,
363
- ( || { } ) . into_repeating_timer_callback ( ) ,
368
+ ( || { } ) . into_repeating_timer_callback ( ) ,
364
369
) ;
365
370
assert ! ( result. is_ok( ) ) ;
366
371
}
@@ -374,7 +379,7 @@ mod tests {
374
379
& context. handle ,
375
380
period,
376
381
Clock :: steady ( ) ,
377
- ( || { } ) . into_repeating_timer_callback ( ) ,
382
+ ( || { } ) . into_repeating_timer_callback ( ) ,
378
383
) ;
379
384
380
385
let timer = result. unwrap ( ) ;
@@ -390,7 +395,7 @@ mod tests {
390
395
& context. handle ,
391
396
Duration :: from_millis ( 1 ) ,
392
397
Clock :: steady ( ) ,
393
- ( || { } ) . into_repeating_timer_callback ( ) ,
398
+ ( || { } ) . into_repeating_timer_callback ( ) ,
394
399
) ;
395
400
396
401
let timer = result. unwrap ( ) ;
@@ -407,7 +412,7 @@ mod tests {
407
412
& context. handle ,
408
413
Duration :: from_millis ( 2 ) ,
409
414
Clock :: steady ( ) ,
410
- ( || { } ) . into_repeating_timer_callback ( ) ,
415
+ ( || { } ) . into_repeating_timer_callback ( ) ,
411
416
) ;
412
417
let timer = result. unwrap ( ) ;
413
418
@@ -432,7 +437,7 @@ mod tests {
432
437
& context. handle ,
433
438
period,
434
439
Clock :: steady ( ) ,
435
- ( || { } ) . into_repeating_timer_callback ( ) ,
440
+ ( || { } ) . into_repeating_timer_callback ( ) ,
436
441
) ;
437
442
let timer = result. unwrap ( ) ;
438
443
@@ -453,8 +458,9 @@ mod tests {
453
458
& context. handle ,
454
459
period,
455
460
Clock :: steady ( ) ,
456
- ( || { } ) . into_repeating_timer_callback ( ) ,
457
- ) . unwrap ( ) ;
461
+ ( || { } ) . into_repeating_timer_callback ( ) ,
462
+ )
463
+ . unwrap ( ) ;
458
464
459
465
// The unwrap will panic if the remaining time is negative
460
466
timer. time_until_next_call ( ) . unwrap ( ) ;
@@ -463,7 +469,10 @@ mod tests {
463
469
thread:: sleep ( Duration :: from_millis ( 3 ) ) ;
464
470
465
471
// Now the time until next call should give an error
466
- assert ! ( matches!( timer. time_until_next_call( ) , Err ( RclrsError :: NegativeDuration ( _) ) ) ) ;
472
+ assert ! ( matches!(
473
+ timer. time_until_next_call( ) ,
474
+ Err ( RclrsError :: NegativeDuration ( _) )
475
+ ) ) ;
467
476
468
477
// Reset the timer so its interval begins again
469
478
assert ! ( timer. reset( ) . is_ok( ) ) ;
@@ -479,8 +488,9 @@ mod tests {
479
488
& context. handle ,
480
489
Duration :: from_millis ( 1 ) ,
481
490
Clock :: steady ( ) ,
482
- ( || { } ) . into_repeating_timer_callback ( ) ,
483
- ) . unwrap ( ) ;
491
+ ( || { } ) . into_repeating_timer_callback ( ) ,
492
+ )
493
+ . unwrap ( ) ;
484
494
485
495
// The unwrap will panic if the remaining time is negative
486
496
timer. time_until_next_call ( ) . unwrap ( ) ;
@@ -489,7 +499,10 @@ mod tests {
489
499
thread:: sleep ( time:: Duration :: from_micros ( 1500 ) ) ;
490
500
491
501
// Now the time until the next call should give an error
492
- assert ! ( matches!( timer. time_until_next_call( ) , Err ( RclrsError :: NegativeDuration ( _) ) ) ) ;
502
+ assert ! ( matches!(
503
+ timer. time_until_next_call( ) ,
504
+ Err ( RclrsError :: NegativeDuration ( _) )
505
+ ) ) ;
493
506
494
507
// The unwrap will panic if anything went wrong with the call
495
508
timer. rcl_call ( ) . unwrap ( ) ;
@@ -505,8 +518,9 @@ mod tests {
505
518
& context. handle ,
506
519
Duration :: from_millis ( 1 ) ,
507
520
Clock :: steady ( ) ,
508
- ( || { } ) . into_repeating_timer_callback ( ) ,
509
- ) . unwrap ( ) ;
521
+ ( || { } ) . into_repeating_timer_callback ( ) ,
522
+ )
523
+ . unwrap ( ) ;
510
524
511
525
assert ! ( !timer. is_ready( ) . unwrap( ) ) ;
512
526
@@ -580,8 +594,11 @@ mod tests {
580
594
executed : Arc < AtomicBool > ,
581
595
) -> AnyTimerCallback {
582
596
( move |t : Time | {
583
- assert ! ( t. compare_with( & initial_time, |t, initial| t >= initial) . unwrap( ) ) ;
597
+ assert ! ( t
598
+ . compare_with( & initial_time, |t, initial| t >= initial)
599
+ . unwrap( ) ) ;
584
600
executed. store ( true , Ordering :: Release ) ;
585
- } ) . into_oneshot_timer_callback ( )
601
+ } )
602
+ . into_oneshot_timer_callback ( )
586
603
}
587
604
}
0 commit comments