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));