Enum async_timers::PeriodicTimer
source · [−]pub enum PeriodicTimer {
Started(Interval),
Stopped,
}
Expand description
PeriodicTimer expires on given interval
PeriodicTimer is an extension and built on top of tokio::time::Interval
.
It can be in two states: PeriodicTimer::Started
and PeriodicTimer::Stopped
.
When in PeriodicTimer::Started
state the timer will expire every interval duration but
when in PeriodicTimer::Stopped
it won’t expire until the timer is started again.
use async_timers::PeriodicTimer;
use tokio::time::{Duration, timeout};
#[tokio::main]
async fn main() {
let mut timer = PeriodicTimer::started(Duration::from_millis(10));
timer.tick().await;
timer.tick().await;
timer.tick().await;
// approximately 30ms have elapsed.
let result = timeout(Duration::from_millis(100), timer.tick()).await;
assert!(result.is_ok(), "Timeout should not occur since timer is running");
timer.stop();
let result = timeout(Duration::from_millis(100), timer.tick()).await;
assert!(result.is_err(), "Timeout should occur since timer is stopped");
}
Variants
Started(Interval)
Stopped
Implementations
sourceimpl PeriodicTimer
impl PeriodicTimer
Trait Implementations
sourceimpl Debug for PeriodicTimer
impl Debug for PeriodicTimer
sourceimpl Default for PeriodicTimer
impl Default for PeriodicTimer
sourcefn default() -> PeriodicTimer
fn default() -> PeriodicTimer
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl !RefUnwindSafe for PeriodicTimer
impl Send for PeriodicTimer
impl Sync for PeriodicTimer
impl Unpin for PeriodicTimer
impl !UnwindSafe for PeriodicTimer
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more