pub struct Ticker { /* private fields */ }
Expand description
Asynchronous stream that yields every Duration, indefinitely.
This stream will tick at uniform intervals, even if blocking work is performed between ticks.
For instance, consider the following code fragment.
use embassy_time::{Duration, Timer};
#[embassy_executor::task]
async fn ticker_example_0() {
loop {
foo();
Timer::after(Duration::from_secs(1)).await;
}
}
This fragment will not call foo
every second.
Instead, it will call it every second + the time it took to previously call foo
.
Example using ticker, which will consistently call foo
once a second.
use embassy_time::{Duration, Ticker};
#[embassy_executor::task]
async fn ticker_example_1() {
let mut ticker = Ticker::every(Duration::from_secs(1));
loop {
foo();
ticker.next().await;
}
}
§Cancel safety
It is safe to cancel waiting for the next tick, meaning no tick is lost if the Future is dropped.
Implementations§
Source§impl Ticker
impl Ticker
Sourcepub fn every(duration: Duration) -> Self
pub fn every(duration: Duration) -> Self
Creates a new ticker that ticks at the specified duration interval.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the ticker back to its original state. This causes the ticker to go back to zero, even if the current tick isn’t over yet.
Sourcepub fn reset_at(&mut self, deadline: Instant)
pub fn reset_at(&mut self, deadline: Instant)
Reset the ticker at the deadline. If the deadline is in the past, the ticker will fire instantly.
Sourcepub fn reset_after(&mut self, after: Duration)
pub fn reset_after(&mut self, after: Duration)
Resets the ticker, after the specified duration has passed. If the specified duration is zero, the next tick will be after the duration of the ticker.
Trait Implementations§
Source§impl FusedStream for Ticker
impl FusedStream for Ticker
Source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the stream should no longer be polled.