pub struct TimerQueue<K, I = Instant> { /* private fields */ }Expand description
Pending timers, earliest first.
Cancellation does not remove from the middle of the heap. Each key carries a generation counter; setting or clearing bumps it, and an entry whose generation is stale is discarded when it surfaces. Cancellation is common — every response cancels something — so making it O(1) and paying at pop time is the right trade.
Implementations§
Source§impl<K: Clone + Eq + Hash, I: Ord + Copy + Add<Duration, Output = I>> TimerQueue<K, I>
impl<K: Clone + Eq + Hash, I: Ord + Copy + Add<Duration, Output = I>> TimerQueue<K, I>
Sourcepub fn set(&mut self, key: K, now: I, after: Duration)
pub fn set(&mut self, key: K, now: I, after: Duration)
Schedule a timer for after from now, replacing any previous instance of the same key.
now is the caller’s, not this queue’s. That is the whole difference between a queue a
tokio driver can use and one that any driver can: a caller on virtual time hands its own
clock in, and nothing here has an opinion about what an instant means.
Sourcepub fn forget(&mut self, key: &K)
pub fn forget(&mut self, key: &K)
Forget one timer key and its generation counter.
The heap entry, if any, becomes stale and is discarded when it reaches the front. This is
the constant-time termination path for callers that can enumerate their small timer set;
Self::forget_matching remains the general full-map operation.
Sourcepub fn clear_matching(&mut self, matches: impl Fn(&K) -> bool)
pub fn clear_matching(&mut self, matches: impl Fn(&K) -> bool)
Cancel every timer whose key matches.
The general form of “cancel everything belonging to this transaction”, which is what termination means — expressed as a predicate because the queue does not know what part of a key identifies a transaction.
Sourcepub fn next_deadline(&mut self) -> Option<I>
pub fn next_deadline(&mut self) -> Option<I>
When the next live timer is due, if any.
Discards stale entries as it looks, so a queue full of cancelled timers does not keep waking the loop.
Sourcepub fn take_due(&mut self, now: I) -> Vec<K>
pub fn take_due(&mut self, now: I) -> Vec<K>
Take every timer due at or before now, earliest first.
Sourcepub fn forget_matching(&mut self, matches: impl Fn(&K) -> bool)
pub fn forget_matching(&mut self, matches: impl Fn(&K) -> bool)
Forget every key that matches, generation counters and all.
Trait Implementations§
Auto Trait Implementations§
impl<K, I> Freeze for TimerQueue<K, I>
impl<K, I> RefUnwindSafe for TimerQueue<K, I>where
K: RefUnwindSafe,
I: RefUnwindSafe,
impl<K, I> Send for TimerQueue<K, I>
impl<K, I> Sync for TimerQueue<K, I>
impl<K, I> Unpin for TimerQueue<K, I>
impl<K, I> UnsafeUnpin for TimerQueue<K, I>
impl<K, I> UnwindSafe for TimerQueue<K, I>where
K: UnwindSafe,
I: UnwindSafe,
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more