pub struct PeriodicTimer { /* private fields */ }Expand description
A timer that periodically ticks.
A periodic timer can be created using the PeriodicTimer::new() constructor,
which requires a reference to a Clock.
§Precision
The timer uses the current thread’s scheduler to schedule its ticks. The precision of the timer is affected by the load on this thread. There are no guarantees about the precision of the timer other than that it will eventually tick. When the thread is healthy, the timer’s period should be close to the specified one.
Note: The periodic timer is not affected by adjustments to the system clock.
§Stream Behavior
PeriodicTimer implements Stream and will never complete. The stream produces
a tick every period indefinitely. Use stream combinators like StreamExt::take
to limit the number of ticks.
§Examples
§Create a periodic timer
use std::time::Duration;
use futures::StreamExt;
use tick::{Clock, PeriodicTimer, Stopwatch};
let timer = PeriodicTimer::new(clock, Duration::from_millis(1));
timer
.take(3)
.for_each(async |()| {
// Do something every 1ms
})
.await;§Create a periodic timer with initial delay
use std::time::Duration;
use futures::StreamExt;
use tick::{Clock, PeriodicTimer};
// Delay for 10ms before the timer starts ticking
clock.delay(Duration::from_millis(10)).await;
let timer = PeriodicTimer::new(clock, Duration::from_millis(1));
timer
.take(3)
.for_each(async |()| {
// Do something every 1ms
})
.await;Implementations§
Trait Implementations§
Source§impl Debug for PeriodicTimer
impl Debug for PeriodicTimer
Source§impl Drop for PeriodicTimer
impl Drop for PeriodicTimer
Source§impl Stream for PeriodicTimer
impl Stream for PeriodicTimer
Auto Trait Implementations§
impl Freeze for PeriodicTimer
impl !RefUnwindSafe for PeriodicTimer
impl Send for PeriodicTimer
impl Sync for PeriodicTimer
impl Unpin for PeriodicTimer
impl UnsafeUnpin for PeriodicTimer
impl !UnwindSafe for PeriodicTimer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more