Enum 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§

Source§

impl PeriodicTimer

Source

pub fn started(period: Duration) -> Self

Create started timer with the given period

Source

pub fn stopped() -> Self

Create stopped timer

Source

pub fn start(&mut self, period: Duration)

Start the timer with given period

Source

pub fn stop(&mut self)

Stop the timer

Source

pub async fn tick(&mut self) -> Instant

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

Trait Implementations§

Source§

impl Debug for PeriodicTimer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PeriodicTimer

Source§

fn default() -> PeriodicTimer

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.