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
impl TimerQueue
Sourcepub fn set_timer(
&mut self,
now_ms: u64,
timeout: Duration,
period: Option<Duration>,
) -> TimerId
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). Callclock.now_ms()immediately beforeset_timer.timeout— delay until first expiry.period—Nonefor a one-shot;Some(d)for a repeating timer.dmust convert to at least 1 ms (sub-millisecond periods are rejected with adebug_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.
Sourcepub fn kill_timer(&mut self, id: TimerId)
pub fn kill_timer(&mut self, id: TimerId)
Cancel a pending timer.
No-op if id is unknown or has already fired (one-shot).
Sourcepub fn collect_expired(&mut self, now_ms: u64) -> Vec<TimerId>
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_mssnapshot 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_msviacalc_next_expires_at. - One-shot timers are removed from the queue upon firing.
Sourcepub fn time_until_next(&self, now_ms: u64) -> Option<Duration>
pub fn time_until_next(&self, now_ms: u64) -> Option<Duration>
Milliseconds until the next timer expires.
Noneif 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.
Trait Implementations§
Source§impl Debug for TimerQueue
impl Debug for TimerQueue
Auto Trait Implementations§
impl Freeze for TimerQueue
impl RefUnwindSafe for TimerQueue
impl Send for TimerQueue
impl Sync for TimerQueue
impl Unpin for TimerQueue
impl UnsafeUnpin for TimerQueue
impl UnwindSafe for TimerQueue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.