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