pub struct CallbackHandle { /* private fields */ }Expand description
A reusable, local one-shot callback timer.
Each construction or reset arms one invocation of the retained callback.
The callback may capture Rc state; this handle is neither Send nor Sync
and cannot be cloned. Keep the handle alive until the callback should run.
Dropping it cancels pending delivery, including an expired callback that has
not started, and releases its captures. An active invocation retains its
captures until it finishes.
Expiration and cancellation retain the callback for reuse. The callback
implements Fn, so use interior mutability for state that changes between
invocations.
Callbacks run outside wheel access and may create, reset, cancel or drop timers, including themselves. They must be short, synchronous and non-blocking. Callbacks due on the same tick have no guaranteed order.
Callback panics propagate through the runtime. They may stop the shared
driver and disarm other timers; remaining callbacks need not be delivered.
Runtime shutdown suppresses pending delivery. Outside of a native
Runtime, a surviving handle retains its callback
and can be explicitly reset in a new runtime on the same thread. Starting
a new runtime does not automatically rearm the handle.
Inside a native runtime the callback is registered with the wheel of that runtime. Once the runtime is dropped, or while it is not active on the thread, the handle is stale: it reports the timer as elapsed and ignores resets.
Implementations§
Source§impl CallbackHandle
impl CallbackHandle
Sourcepub fn new(millis: u64, callback: impl Fn() + 'static) -> CallbackHandle
pub fn new(millis: u64, callback: impl Fn() + 'static) -> CallbackHandle
Arms a callback after millis, using the wheel’s rounding and limits.
The base granularity is approximately 16 ms by default, see
Builder::timer_resolution;
longer delays use coarser rounding, and delays beyond the wheel’s
capacity (about 24 days at 16 ms) are clamped. Callback delivery may be
delayed further while the runtime is busy.
A zero delay waits for a subsequent timer tick; the callback is never invoked inline.
Sourcepub fn reset(&self, millis: u64)
pub fn reset(&self, millis: u64)
Arms another invocation, invalidating any previous pending delivery.
This also suppresses an expired invocation that has been queued but has not started. It can be called before or after expiration or cancellation.
A zero delay waits for a subsequent tick. Resetting from within the callback arms a new invocation, which survives completion of the old one.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Disarms the timer and suppresses pending delivery, retaining the callback.
This also suppresses an expired invocation that has not started. Calling this method on an already disarmed timer is harmless.
An invocation that has already started cannot be interrupted.
Sourcepub fn is_elapsed(&self) -> bool
pub fn is_elapsed(&self) -> bool
Returns true when disarmed, including after cancellation or shutdown.
Expiration marks the timer elapsed before callback delivery. This query does not indicate whether the callback has run.