Enum OneshotTimer

Source
pub enum OneshotTimer {
    Scheduled(Instant),
    Expired,
}
Expand description

OneshotTimer expires once after a given duration

OneshotTimer is used for tasks that need to be executed once after some delay. OneshotTimer is an extension and built on top of tokio::time::Sleep. In OneshotTimer::Scheduled state it will expire once and transition into OneshotTimer::Expired state.

use async_timers::OneshotTimer;
use tokio::time::{Duration, timeout};

#[tokio::main]
async fn main() {
    let mut timer = OneshotTimer::scheduled(Duration::from_millis(10));

    timer.tick().await;

    // approximately 10ms have elapsed.

    let result = timeout(Duration::from_millis(100), timer.tick()).await;
    assert!(result.is_err(), "Timeout should occur since timer is expired");

    timer.schedule(Duration::from_millis(30));

    let result = timeout(Duration::from_millis(100), timer.tick()).await;
    assert!(result.is_ok(), "Timeout should not occur since timer has been scheduled");
}

Variants§

§

Scheduled(Instant)

§

Expired

Implementations§

Source§

impl OneshotTimer

Source

pub fn scheduled(duration: Duration) -> Self

Create a timer scheduled to be expired after duration

Source

pub fn expired() -> Self

Create a timer that won’t expire

Source

pub fn schedule(&mut self, duration: Duration)

Schedule a new duration

Source

pub fn cancel(&mut self)

Cancel the timer

Source

pub async fn tick(&mut self)

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

Trait Implementations§

Source§

impl Debug for OneshotTimer

Source§

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

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

impl Default for OneshotTimer

Source§

fn default() -> OneshotTimer

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.