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>
impl<T> HashedWheelTimer<T>
Sourcepub fn new(buckets_num: usize) -> Self
pub fn new(buckets_num: usize) -> Self
Creates a timer with a fixed number of buckets.
buckets_num must be greater than 0.
Sourcepub fn count_in_bucket(
&self,
bucket_index: usize,
) -> Result<usize, TimeWheelError>
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.
Sourcepub fn is_empty_bucket(
&self,
bucket_index: usize,
) -> Result<bool, TimeWheelError>
pub fn is_empty_bucket( &self, bucket_index: usize, ) -> Result<bool, TimeWheelError>
Returns true when a bucket contains no events.
Sourcepub fn has_events_in_current_tick(&self) -> bool
pub fn has_events_in_current_tick(&self) -> bool
Returns true if the current tick still has at least one wave to pop.
Sourcepub fn curr_bucket(&self) -> usize
pub fn curr_bucket(&self) -> usize
Returns current bucket index.
Sourcepub fn curr_delta_tick(&self) -> u64
pub fn curr_delta_tick(&self) -> u64
Returns current wave (delta_tick) inside curr_tick.
Sourcepub fn curr_seq_id(&self) -> EventId
pub fn curr_seq_id(&self) -> EventId
Returns the latest assigned sequence ID.
Sourcepub fn schedule(&mut self, on_tick: u64, data: T) -> ScheduleResult
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.
Sourcepub fn update(
&mut self,
id: EventId,
on_tick: u64,
data: T,
) -> Option<UpdateResult>
pub fn update( &mut self, id: EventId, on_tick: u64, data: T, ) -> Option<UpdateResult>
Replaces event payload and scheduled tick by ID.
Sourcepub fn reschedule(&mut self, id: EventId, on_tick: u64) -> Option<UpdateResult>
pub fn reschedule(&mut self, id: EventId, on_tick: u64) -> Option<UpdateResult>
Moves existing event to another tick preserving payload.
Sourcepub fn pop_events(&mut self) -> Vec<Event<T>>
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.