pub struct Scheduler<E> { /* private fields */ }Expand description
A scheduler for future events.
The scheduler does not dispatch events itself, rather it provides the facilities needed for the caller to efficiently dispatch events in as few wakeups as possible.
Implementations§
Source§impl<E> Scheduler<E>
impl<E> Scheduler<E>
Sourcepub fn add(&mut self, when: TimeRange, what: E) -> Handle<E>
pub fn add(&mut self, when: TimeRange, what: E) -> Handle<E>
Schedule an event to occur at a future point in time.
Returns a Handle which may be used to cancel or reschedule the event. The caller need
not retain the Handle if cancellation and rescheduling are not required.
Sourcepub fn dispatch(&mut self, now: Instant) -> impl Iterator<Item = E> + use<E>
pub fn dispatch(&mut self, now: Instant) -> impl Iterator<Item = E> + use<E>
Removes events that are due to happen at or before now from the scheduler’s queue,
returning an iterator over the removed events.
If the iterator is dropped before being fully consumed, it drops the remaining removed events.
Sourcepub fn next_dispatch_range(&self) -> Option<TimeRange>
pub fn next_dispatch_range(&self) -> Option<TimeRange>
Returns the next time range in which Scheduler::dispatch should next be called to
dispatch events.
Scheduler::dispatch should be called at some point in the returned TimeRange to
dispatch events on time.
Calling dispatch closer to the end of the range is more efficient and results in more
events being available. Calling dispatch before the returned range is inefficient
but otherwise harmless.
The returned range may lie entirely in the past, if overdue events exists. The caller is
expected to call Scheduler::dispatch as soon as possible in that case.
This method is intended to be used to plumb this Scheduler’s event dispatch into another Scheduler.
Sourcepub fn next_dispatch(&self) -> Option<Instant>
pub fn next_dispatch(&self) -> Option<Instant>
Returns the next time at which Scheduler::dispatch should next be called to dispatch
events.
This is the same as Scheduler::next_dispatch_range, but only returns the Instant
corresponding to the end of the feasible time range.
Calling dispatch before the returned time is inefficient but otherwise harmless.
The returned time may be in the past, if overdue events exists. The caller is
expected to call Scheduler::dispatch as soon as possible in that case.
This method is intended to be used to plumb this Scheduler into an external timer facility
(e.g. std::thread::sleep, tokio::time::sleep), to trigger event dispatching at the
appropriate time.