Skip to main content

Module time

Module time 

Source
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 Duration type to represent a span of time, typically used for system timeouts.
Elapsed
Errors returned by Timeout.
Instant
A measurement of a monotonically nondecreasing clock. Opaque and useful only with Duration.
Interval
Interval returned by interval and interval_at.
Sleep
Future returned by sleep and sleep_until.
SystemTime
A measurement of the system clock, useful for talking to external entities like the file system or other processes.
SystemTimeError
An error returned from the duration_since and elapsed methods on SystemTime, used to learn how far in the opposite direction a system time lies.
Timeout
Future returned by timeout and timeout_at.

Enums§

MissedTickBehavior
Defines the behavior of an Interval when it misses a tick.

Constants§

UNIX_EPOCH
An anchor in time which can be used to create new SystemTime instances or learn about where in time a SystemTime lies.

Functions§

interval
Creates new Interval that yields with interval of period. The first tick completes immediately. The default MissedTickBehavior is Burst, but this can be configured by calling set_missed_tick_behavior.
interval_at
Creates new Interval that yields with interval of period with the first tick completing at start. The default MissedTickBehavior is Burst, but this can be configured by calling set_missed_tick_behavior.
sleep
Waits until duration has elapsed.
sleep_until
Waits until deadline is reached.
timeout
Requires a Future to complete before the specified duration has elapsed.
timeout_at
Requires a Future to complete before the specified instant in time.