pub trait Timer:
Send
+ Sync
+ Unpin
+ Future<Output = ()> {
// Required methods
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 ofsiginfo_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§
sourcefn is_ticking(&self) -> bool
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 may not be armed.
sourcefn is_expired(&self) -> bool
fn is_expired(&self) -> bool
Returns whether timer has expired.
sourcefn restart_ctx(&mut self, timeout: Duration, waker: &Waker)
fn restart_ctx(&mut self, timeout: Duration, waker: &Waker)
Restarts timer with new timeout value and waker.
Object Safety§
This trait is not object safe.