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

Create started timer with the given period

Create stopped timer

Start the timer with given period

Stop the timer

Returns a Future that will expire based on timer’s state

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.