Skip to main content

TimerQueue

Struct TimerQueue 

Source
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>

Source

pub fn new() -> Self

An empty queue.

Source

pub fn len(&self) -> usize

How many entries are held, including stale ones not yet discarded.

Source

pub fn is_empty(&self) -> bool

Whether nothing is scheduled.

Source

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.

Source

pub fn clear(&mut self, key: &K)

Cancel a timer.

Source

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.

Source

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.

Source

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.

Source

pub fn take_due(&mut self, now: I) -> Vec<K>

Take every timer due at or before now, earliest first.

Source

pub fn forget_matching(&mut self, matches: impl Fn(&K) -> bool)

Forget every key that matches, generation counters and all.

Trait Implementations§

Source§

impl<K: Debug, I: Debug> Debug for TimerQueue<K, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Eq, I: Ord> Default for TimerQueue<K, I>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, I> Freeze for TimerQueue<K, I>

§

impl<K, I> RefUnwindSafe for TimerQueue<K, I>

§

impl<K, I> Send for TimerQueue<K, I>
where K: Send, I: Send,

§

impl<K, I> Sync for TimerQueue<K, I>
where K: Sync, I: Sync,

§

impl<K, I> Unpin for TimerQueue<K, I>
where K: Unpin, I: Unpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,