Skip to main content

CallbackHandle

Struct CallbackHandle 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for CallbackHandle

Source§

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

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

impl Drop for CallbackHandle

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V