Expand description
Waiting on a future from a thread, a waker that does nothing, and a waker that sets a flag.
This module is gated by the std cargo feature, the one feature of this
crate that links the standard library. It is for three callers:
- A blocking client built over an async one. A client that returns a
future is the one source of truth for a call’s behaviour; a blocking
variant of the same call is
block_onover that future, so the two cannot diverge. Thedeadlinebounds the whole wait. - A frame loop that polls a future once per frame. Such a loop needs a
Context, and aContextneeds aWaker, but the loop polls on its own schedule and has no use for a wake.noop_wakeris that waker: created once when the loop starts and cloned into eachContext. - A frame loop that polls again when the future asks for it. A future
that does a bounded amount of work per poll and wakes itself when work is
left — the generated
Servetakes at most 32 claims per poll — makes progress undernoop_wakeronly at the loop’s next frame, because that wake is discarded.flag_wakerrecords the wake in a flag the loop reads: poll, then poll again while the flag was set, up to the loop’s own limit of polls per frame.
All three are written without unsafe, which this crate forbids, over
std::task::Wake on an Arc. That is why the module is under std:
Waker::noop() needs Rust 1.85, above this crate’s minimum, and building a
Waker from a raw vtable needs unsafe. The module takes no dependency:
the wait is std::thread::park_timeout, whose unpark token cannot be lost
between a poll and the park that follows it.
The feature compiles for wasm32-unknown-unknown, because just wasm-check
requires every feature to, and block_on is not usable on that target:
Instant::now() panics there, and a park does not block the thread. A
frame loop on wasm polls with noop_waker or flag_waker and never
calls block_on. A no_std frame loop, where this module does not
exist, writes the same small waker over alloc::task::Wake on an Arc
when it has an allocator; one without an allocator needs a hand-written
RawWaker, which needs unsafe, as the paragraph above explains.
Nothing here names a port, an interface or a generated type; the module
knows only core::future::Future.
Structs§
- Wake
Flag - The read side of a
flag_waker: whether the waker was woken since the lasttake.
Functions§
- block_
on - Run
futto completion on the current thread, parking the thread between polls, and give up atdeadline. - flag_
waker - A waker whose wake sets a flag, for a frame loop that polls again in the same frame when the future asks for it.
- noop_
waker - A waker whose wake does nothing, for a loop that polls on its own schedule.