[−][src]Trait async_timer::oneshot::Oneshot
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 is_ticking(&self) -> bool
Returns whether timer is ongoing.
Note that if it returns false
it doesn't mean that is_expired
will return true
as initially timer is not armed.
fn is_expired(&self) -> bool
Returns whether timer has expired.
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.