Skip to main content

HashedWheelTimer

Struct HashedWheelTimer 

Source
pub struct HashedWheelTimer<T> { /* private fields */ }
Expand description

Single-level hashed wheel timer indexed by event ID.

This timer is deterministic:

  • Events are grouped by (tick, delta_tick) waves.
  • Events in the same wave are popped in ascending event_id.

on_tick passed to schedule is clamped to curr_tick, so scheduling into the past places the event on the current tick.

Implementations§

Source§

impl<T> HashedWheelTimer<T>

Source

pub fn new(buckets_num: usize) -> Self

Creates a timer with a fixed number of buckets.

buckets_num must be greater than 0.

Source

pub fn count_all(&self) -> usize

Returns the total number of scheduled events.

Source

pub fn count_in_bucket( &self, bucket_index: usize, ) -> Result<usize, TimeWheelError>

Returns number of events currently stored in a bucket.

This is a bucket-level view, not limited to curr_tick.

Source

pub fn is_empty(&self) -> bool

Returns true when no events are scheduled.

Source

pub fn is_empty_bucket( &self, bucket_index: usize, ) -> Result<bool, TimeWheelError>

Returns true when a bucket contains no events.

Source

pub fn has_events_in_current_tick(&self) -> bool

Returns true if the current tick still has at least one wave to pop.

Source

pub fn curr_tick(&self) -> u64

Returns current absolute tick.

Source

pub fn curr_bucket(&self) -> usize

Returns current bucket index.

Source

pub fn curr_delta_tick(&self) -> u64

Returns current wave (delta_tick) inside curr_tick.

Source

pub fn curr_seq_id(&self) -> EventId

Returns the latest assigned sequence ID.

Source

pub fn schedule(&mut self, on_tick: u64, data: T) -> ScheduleResult

Schedules a new event and returns its ID.

If on_tick < curr_tick, the event is scheduled on curr_tick.

Source

pub fn contains(&self, id: EventId) -> bool

Returns true if an event with id exists.

Source

pub fn get(&self, id: EventId) -> Option<&Event<T>>

Returns event by ID.

Source

pub fn remove(&mut self, id: EventId) -> Option<Event<T>>

Removes and returns an event by ID.

Source

pub fn update( &mut self, id: EventId, on_tick: u64, data: T, ) -> Option<UpdateResult>

Replaces event payload and scheduled tick by ID.

Source

pub fn reschedule(&mut self, id: EventId, on_tick: u64) -> Option<UpdateResult>

Moves existing event to another tick preserving payload.

Source

pub fn pop_events(&mut self) -> Vec<Event<T>>

Pops a single wave of events for current tick.

Returns empty vector when current tick has no remaining events.

Source

pub fn step(&mut self)

Advances timer by one tick and moves to next bucket.

Source

pub fn reset(&mut self)

Clears all events and resets timer state.

Trait Implementations§

Source§

impl<T: Debug> Debug for HashedWheelTimer<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for HashedWheelTimer<T>

§

impl<T> RefUnwindSafe for HashedWheelTimer<T>
where T: RefUnwindSafe,

§

impl<T> Send for HashedWheelTimer<T>
where T: Send,

§

impl<T> Sync for HashedWheelTimer<T>
where T: Sync,

§

impl<T> Unpin for HashedWheelTimer<T>
where T: Unpin,

§

impl<T> UnwindSafe for HashedWheelTimer<T>
where T: 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.

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.