Expand description
Portable async time: tokio::time on native, [wasmtimer] in the browser.
Code that sleeps, ticks on an interval, or reads an Instant needs to work
on both targets. Native tokio’s clock is std::time::Instant::now(), which
panics on wasm32-unknown-unknown (no clock), and its timers need a runtime
time driver that wasm_bindgen_futures::spawn_local doesn’t provide. So in
the browser we route through wasmtimer, which implements the same API on
performance.now() + setTimeout. Use web_async::time in place of
tokio::time and the same code runs on both.
The surface mirrors tokio::time 1:1. The mockable test clock
(tokio::time::pause/advance) is intentionally not re-exported: it lives
behind tokio’s test-util feature, which must not leak into normal builds,
and it only ever runs in native tests anyway.
Structs§
- Duration
- A
Durationtype to represent a span of time, typically used for system timeouts. - Instant
- A measurement of a monotonically nondecreasing clock.
Opaque and useful only with
Duration. - Interval
- Interval returned by
intervalandinterval_at. - Sleep
- Future returned by
sleepandsleep_until. - Timeout
- Future returned by
timeoutandtimeout_at.
Enums§
- Missed
Tick Behavior - Defines the behavior of an
Intervalwhen it misses a tick.
Functions§
- interval
- Creates new
Intervalthat yields with interval ofperiod. The first tick completes immediately. The defaultMissedTickBehaviorisBurst, but this can be configured by callingset_missed_tick_behavior. - interval_
at - Creates new
Intervalthat yields with interval ofperiodwith the first tick completing atstart. The defaultMissedTickBehaviorisBurst, but this can be configured by callingset_missed_tick_behavior. - sleep
- Waits until
durationhas elapsed. - sleep_
until - Waits until
deadlineis reached. - timeout
- Requires a
Futureto complete before the specified duration has elapsed. - timeout_
at - Requires a
Futureto complete before the specified instant in time.