[][src]Trait async_timer::oneshot::Oneshot

pub trait Oneshot: Send + Sync + Unpin + Future<Output = ()> {
    fn new(timeout: Duration) -> Self;
fn cancel(&mut self);
fn restart(&mut self, timeout: &Duration, waker: &Waker); }

One-shot timer that expires once

Trait itself describes Future that resolves after timeout

Most common platforms are supplied via alias Timer

Common implementations:

  • Windows uses thread pooled timer
  • Apple systems uses dispatch source API
  • Posix compatible timer_create, currently available only on Linux systems
  • Wasm uses Web API SetTimeout
  • Dummy timer is used when no implementation is available. Panics when used.

Feature romio_on

  • Linux uses timerfd_create, replaces Posix tiemr when enabled.
  • Other unix systems uses kqueue, replaces Apple timer when enabled.
 use async_timer::oneshot::{Oneshot, Timer};
 use futures::executor::block_on;

 use std::time;

 let work = Timer::new(time::Duration::from_secs(2));

 let before = time::SystemTime::now();
 block_on(work);
 let after = time::SystemTime::now();
 let diff = after.duration_since(before).unwrap();

 assert_eq!(diff.as_secs(), 2);

Required methods

fn new(timeout: Duration) -> Self

Creates new instance without actually starting timer.

Timer should start only on first Future::poll

fn cancel(&mut self)

Cancels ongoing timer, if it is not expired yet.

fn restart(&mut self, timeout: &Duration, waker: &Waker)

Restarts timer with new timeout value.

If timer is already running, then over-write old value and replaces waker.

Loading content...

Implementors

impl Oneshot for PosixTimer[src]

impl Oneshot for NeverTimer[src]

Loading content...