Skip to main content

TimerWheel

Struct TimerWheel 

Source
pub struct TimerWheel<T: 'static, S: SlabStore<Item = WheelEntry<T>> = Slab<WheelEntry<T>>> { /* private fields */ }
Expand description

A multi-level, no-cascade timer wheel.

Generic over:

  • T — the user payload stored with each timer.
  • S — the slab storage backend. Defaults to unbounded::Slab.

§Thread Safety

Send but !Sync. Can be moved to a thread at setup but must not be shared. All internal raw pointers point into owned allocations (slab chunks, level slot arrays) — moving the wheel moves the heap data with it.

Implementations§

Source§

impl<T: 'static> TimerWheel<T>

Source

pub fn unbounded(chunk_capacity: usize, now: Instant) -> Self

Creates an unbounded timer wheel with default configuration.

For custom configuration, use WheelBuilder.

Source§

impl<T: 'static> TimerWheel<T, Slab<WheelEntry<T>>>

Source

pub fn bounded(capacity: usize, now: Instant) -> Self

Creates a bounded timer wheel with default configuration.

For custom configuration, use WheelBuilder.

Source§

impl<T: 'static, S: SlabStore<Item = WheelEntry<T>>> TimerWheel<T, S>

Source

pub fn schedule(&mut self, deadline: Instant, value: T) -> TimerHandle<T>

Schedules a timer and returns a handle for cancellation.

The handle must be consumed via cancel or free. Dropping it is a programming error.

§Panics

Panics if the backing slab is at capacity (bounded slabs only). This is a capacity planning error — size your wheel for peak load.

Source

pub fn schedule_forget(&mut self, deadline: Instant, value: T)

Schedules a fire-and-forget timer (no handle returned).

The timer will fire during poll and the value will be collected. Cannot be cancelled.

§Panics

Panics if the backing slab is at capacity (bounded slabs only). This is a capacity planning error — size your wheel for peak load.

Source§

impl<T: 'static, S: BoundedStore<Item = WheelEntry<T>>> TimerWheel<T, S>

Source

pub fn try_schedule( &mut self, deadline: Instant, value: T, ) -> Result<TimerHandle<T>, Full<T>>

Attempts to schedule a timer, returning a handle on success.

Returns Err(Full(value)) if the slab is at capacity. Use this when you need graceful error handling. For the common case where capacity exhaustion is fatal, use schedule.

Source

pub fn try_schedule_forget( &mut self, deadline: Instant, value: T, ) -> Result<(), Full<T>>

Attempts to schedule a fire-and-forget timer.

Returns Err(Full(value)) if the slab is at capacity. Use this when you need graceful error handling. For the common case where capacity exhaustion is fatal, use schedule_forget.

Source§

impl<T: 'static, S: SlabStore<Item = WheelEntry<T>>> TimerWheel<T, S>

Source

pub fn cancel(&mut self, handle: TimerHandle<T>) -> Option<T>

Cancels a timer and returns its value.

  • If the timer is still active: unlinks from the wheel, extracts value, frees the slab entry. Returns Some(T).
  • If the timer already fired (zombie handle): frees the slab entry. Returns None.

Consumes the handle (no Drop runs).

Source

pub fn free(&mut self, handle: TimerHandle<T>)

Releases a timer handle without cancelling.

  • If the timer is still active: converts to fire-and-forget (refs 2→1). Timer stays in the wheel and will fire normally during poll.
  • If the timer already fired (zombie): frees the slab entry (refs 1→0).

Consumes the handle (no Drop runs).

Source

pub fn reschedule( &mut self, handle: TimerHandle<T>, new_deadline: Instant, ) -> TimerHandle<T>

Reschedules an active timer to a new deadline.

Moves the entry from its current slot to the correct slot for new_deadline without extracting or reconstructing the value.

§Panics

Panics if the timer has already fired (zombie handle). Only active timers (refs == 2) can be rescheduled.

Consumes and returns a new handle (same entry, new position).

Source

pub fn poll(&mut self, now: Instant, buf: &mut Vec<T>) -> usize

Fires all expired timers, collecting their values into buf.

Returns the number of timers fired.

Source

pub fn poll_with_limit( &mut self, now: Instant, limit: usize, buf: &mut Vec<T>, ) -> usize

Fires expired timers up to limit, collecting values into buf.

Resumable: if the limit is hit, the next call continues where this one left off (as long as now hasn’t changed).

Returns the number of timers fired in this call.

Source

pub fn next_deadline(&self) -> Option<Instant>

Returns the Instant of the next timer that will fire, or None if empty.

Walks only active (non-empty) slots. O(active_slots) in the worst case, but typically very fast because most slots are empty.

Source

pub fn len(&self) -> usize

Returns the number of timers currently in the wheel.

Source

pub fn is_empty(&self) -> bool

Returns true if the wheel contains no timers.

Trait Implementations§

Source§

impl<T: 'static, S: SlabStore<Item = WheelEntry<T>>> Drop for TimerWheel<T, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send + 'static, S: SlabStore<Item = WheelEntry<T>>> Send for TimerWheel<T, S>

Auto Trait Implementations§

§

impl<T, S> Freeze for TimerWheel<T, S>
where S: Freeze,

§

impl<T, S = Slab<WheelEntry<T>>> !RefUnwindSafe for TimerWheel<T, S>

§

impl<T, S = Slab<WheelEntry<T>>> !Sync for TimerWheel<T, S>

§

impl<T, S> Unpin for TimerWheel<T, S>
where S: Unpin,

§

impl<T, S> UnsafeUnpin for TimerWheel<T, S>
where S: UnsafeUnpin,

§

impl<T, S = Slab<WheelEntry<T>>> !UnwindSafe for TimerWheel<T, S>

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.

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, 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.