Enum async_timers::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
sourceimpl OneshotTimer
impl OneshotTimer
Trait Implementations
sourceimpl Debug for OneshotTimer
impl Debug for OneshotTimer
sourceimpl Default for OneshotTimer
impl Default for OneshotTimer
sourcefn default() -> OneshotTimer
fn default() -> OneshotTimer
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl RefUnwindSafe for OneshotTimer
impl Send for OneshotTimer
impl Sync for OneshotTimer
impl Unpin for OneshotTimer
impl UnwindSafe for OneshotTimer
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more