pub struct Timer { /* private fields */ }Expand description
Manages timers and triggers timeouts.
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn has_timeouts(&self) -> bool
pub fn has_timeouts(&self) -> bool
Check whether there are timeouts being tracked.
Sourcepub fn set_timeout(&mut self, timeout: Duration, after: Timestamp)
pub fn set_timeout(&mut self, timeout: Duration, after: Timestamp)
Register a new timeout relative to a certain point in time.
Sourcepub fn next_expiring_from(&self, time: impl Into<Timestamp>) -> Option<Duration>
pub fn next_expiring_from(&self, time: impl Into<Timestamp>) -> Option<Duration>
Get the first timeout expiring right at or after certain moment of time.
Returns None if there are no timeouts.
use reactor::{Timer, Timestamp};
let mut tm = Timer::new();
let now = Timestamp::now();
tm.set_timeout(Duration::from_secs(16), now);
tm.set_timeout(Duration::from_secs(8), now);
tm.set_timeout(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_expiring_from(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_expiring_from(now).unwrap() <= Duration::from_secs(7));Sourcepub fn remove_expired_by(&mut self, time: Timestamp) -> usize
pub fn remove_expired_by(&mut self, time: Timestamp) -> usize
Removes timeouts which expire by a certain moment of time (inclusive), returning total number of timeouts which were removed.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Timer
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