pub struct Timer { /* private fields */ }Expand description
Manages timers and triggers timeouts.
Implementations§
source§impl Timer
impl Timer
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new timeout manager.
Takes a threshold below which two timeouts cannot overlap.
sourcepub fn set_timer(&mut self, span: Duration, after: Timestamp)
pub fn set_timer(&mut self, span: Duration, after: Timestamp)
Register a new timeout with an associated key and wake-up time from a UNIX time epoch.
sourcepub fn next(&self, after: impl Into<Timestamp>) -> Option<Duration>
pub fn next(&self, after: impl Into<Timestamp>) -> Option<Duration>
Get the minimum time duration we should wait for at least one timeout
to be reached. Returns None if there are no timeouts.
use reactor::{Timer, Timestamp};
let mut tm = Timer::new();
let now = Timestamp::now();
tm.set_timer(Duration::from_secs(16), now);
tm.set_timer(Duration::from_secs(8), now);
tm.set_timer(Duration::from_secs(64), now);
let mut now = Timestamp::now();
// We need to wait 8 secs to trigger the next timeout (1).
assert!(tm.next(now) <= Some(Duration::from_secs(8)));
// ... sleep for a sec ...
now += Duration::from_secs(1);
// Now we don't need to wait as long!
assert!(tm.next(now).unwrap() <= Duration::from_secs(7));Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Timer
impl Send for Timer
impl Sync for Timer
impl Unpin for Timer
impl UnwindSafe for Timer
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