Skip to main content

TimerQueue

Struct TimerQueue 

Source
pub struct TimerQueue { /* private fields */ }
Expand description

A queue of pending one-shot and periodic timers.

collect_expired returns the due timers as a Vec<TimerId> for the caller to dispatch, and the current time now_ms is passed in by the caller rather than stored here (see the module docs).

Durations are stored internally as u64 milliseconds (truncated from Duration). Sub-millisecond periods truncate to 0 and are rejected.

§Turbo Vision heritage

Ports TTimerQueue (ttimerqu.cpp, system.h).

Implementations§

Source§

impl TimerQueue

Source

pub fn new() -> Self

Create an empty timer queue.

Source

pub fn set_timer( &mut self, now_ms: u64, timeout: Duration, period: Option<Duration>, ) -> TimerId

Arm a new timer.

  • now_ms — the current clock value at the moment of arming. Must be supplied by the caller (the clock lives in the event loop, not here). Call clock.now_ms() immediately before set_timer.
  • timeout — delay until first expiry.
  • period — None for a one-shot; Some(d) for a repeating timer. d must convert to at least 1 ms (sub-millisecond periods are rejected with a debug_assert; in release they silently become one-shot-like one-ms timers — document this in your integration code).

Returns a TimerId that can be passed to kill_timer.

Source

pub fn kill_timer(&mut self, id: TimerId)

Cancel a pending timer.

No-op if id is unknown or has already fired (one-shot).

Source

pub fn collect_expired(&mut self, now_ms: u64) -> Vec<TimerId>

Fire all timers due at now_ms and return their ids (in unspecified order).

Instead of calling a user-supplied callback mid-iteration, it collects due ids into a Vec and returns them. The caller then dispatches each id (posts a command, calls a callback, etc.).

Invariants:

  • A single now_ms snapshot is used for the whole pass.
  • A periodic timer fires at most once per call, even if overdue by several periods — it is rescheduled forward past now_ms via calc_next_expires_at.
  • One-shot timers are removed from the queue upon firing.
Source

pub fn time_until_next(&self, now_ms: u64) -> Option<Duration>

Milliseconds until the next timer expires.

  • None if the queue is empty.
  • Some(Duration::ZERO) if any timer is already due.
  • Some(remaining) otherwise (the minimum over all timers).

Returning Option<Duration> avoids any artificial cap on the wait time.

Source

pub fn is_empty(&self) -> bool

Whether the queue has no pending timers.

Source

pub fn len(&self) -> usize

Number of pending timers.

Trait Implementations§

Source§

impl Debug for TimerQueue

Source§

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

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

impl Default for TimerQueue

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> CustomValue for T
where T: Any + Debug,

Source§

fn as_any_rc(self: Rc<T>) -> Rc<dyn Any>

Upcast Rc<Self> to Rc<dyn Any> so FieldValue::as_custom / FieldValue::value_as can downcast.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.