pub enum PeriodicTimer {
Started(Interval),
Stopped,
}
Expand description
PeriodicTimer expires on given interval
PeriodicTimer is an extension and built on top of tokio::time::Interval
.
It can be in two states: PeriodicTimer::Started
and PeriodicTimer::Stopped
.
When in PeriodicTimer::Started
state the timer will expire every interval duration but
when in PeriodicTimer::Stopped
it won’t expire until the timer is started again.
use async_timers::PeriodicTimer;
use tokio::time::{Duration, timeout};
#[tokio::main]
async fn main() {
let mut timer = PeriodicTimer::started(Duration::from_millis(10));
timer.tick().await;
timer.tick().await;
timer.tick().await;
// approximately 30ms have elapsed.
let result = timeout(Duration::from_millis(100), timer.tick()).await;
assert!(result.is_ok(), "Timeout should not occur since timer is running");
timer.stop();
let result = timeout(Duration::from_millis(100), timer.tick()).await;
assert!(result.is_err(), "Timeout should occur since timer is stopped");
}
Variants§
Implementations§
Source§impl PeriodicTimer
impl PeriodicTimer
Trait Implementations§
Source§impl Debug for PeriodicTimer
impl Debug for PeriodicTimer
Source§impl Default for PeriodicTimer
impl Default for PeriodicTimer
Source§fn default() -> PeriodicTimer
fn default() -> PeriodicTimer
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for PeriodicTimer
impl !RefUnwindSafe for PeriodicTimer
impl Send for PeriodicTimer
impl Sync for PeriodicTimer
impl Unpin for PeriodicTimer
impl !UnwindSafe for PeriodicTimer
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