Trait async_timer::timer::Timer[][src]

pub trait Timer: Send + Sync + Unpin + Future<Output = ()> {
    fn new(timeout: Duration) -> Self;
fn is_ticking(&self) -> bool;
fn is_expired(&self) -> bool;
fn restart(&mut self, timeout: Duration);
fn restart_ctx(&mut self, timeout: Duration, waker: &Waker);
fn cancel(&mut self); }
Expand description

Timer

Common implementations:

  • Windows uses thread pooled timer
  • Apple systems uses dispatch source API
  • Posix compatible timer_create, available on major Posix-compliant systems. Depends on availability of siginfo_t::si_value method.
  • Wasm uses Web API SetTimeout
  • Dummy timer is used when no implementation is available. Panics when used.

Usage

use async_timer::timer::{Timer, new_timer};

use core::time;
use core::pin::Pin;

async fn do_something() {
    let mut work = new_timer(time::Duration::from_secs(2));
    assert!(!work.is_ticking()); //Timer starts only on initial poll
    assert!(!work.is_expired());
    Pin::new(&mut work).await; //Remember await consumes future, and we'd prefer to avoid that in order to re-use timer
    assert!(work.is_expired());

}

Required methods

Creates new instance

Returns whether timer is ongoing.

Note that if it returns false it doesn’t mean that is_expired will return true as initially timer may not be armed.

Returns whether timer has expired.

Restarts timer with new timeout value.

Restarts timer with new timeout value and waker.

Cancels timer, if it is still ongoing.

Implementors