Skip to main content

TimerDriver

Trait TimerDriver 

Source
pub trait TimerDriver:
    Send
    + Sync
    + 'static {
    // Required methods
    fn register(&self, id: TimerId, deadline: Instant, waker: Waker);
    fn cancel(&self, id: TimerId);

    // Provided methods
    fn is_live(&self) -> bool { ... }
    fn on_swept(&self) { ... }
}
Expand description

Seam for host-delegated timers: the CurrentThread flavor cannot park a helper thread on a threadless build, so it delegates sleep_until to the host event loop through this trait. The Node binding installs a setTimeout-based implementation via register_timer_driver at import – one per importing napi env (main thread AND workers).

Required Methods§

Source

fn register(&self, id: TimerId, deadline: Instant, waker: Waker)

Arm (or re-arm on re-poll) waker to fire at/after deadline. Called once per Sleep poll: implementations must treat a repeated id as a waker refresh, not a second timer.

Source

fn cancel(&self, id: TimerId)

Best-effort cancel (Drop of the Sleep future). A fire that already raced ahead is acceptable (the woken task observes the sleep completed).

Provided Methods§

Source

fn is_live(&self) -> bool

Whether this driver can still deliver wakes. false once the driver’s host is gone (the Node binding’s driver dies with its owning napi env: worker exit aborts the weak threadsafe function). Selection skips and evicts dead drivers – a timer armed on one would only ever busy-fail.

Source

fn on_swept(&self)

Called by TimerDriverRegistry AFTER it swept this driver out of the entries (its is_live turned false), with NO registry lock held. Implementations with pending-waker bookkeeping must wake everything armed on them here, so those sleeps re-poll onto the next live registrant: the is_live probe can be the FIRST layer to notice a dying host (before its env-cleanup hook or any call failure runs), and a silent retain would strand sleeps whose only wake source was the swept driver. Idempotent with the owner’s other eviction paths. Default no-op for drivers without waker state.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§