std::task::Waker
Rust wakers are std::task::Waker
s. A Waker wraps a
std::task::RawWaker
which provides the waking functionality.
Reasons behind the “delegation”:
- RawWakers are unsafe to use. By wrapping RawWakers in Wakers, it possible to avoid the unsafe part.
std::task::LocalWaker
(nightly) is the thread unsafe version of Waker, meaning that a LocalWaker have to be accessed from the same thread where it was created. The wrapping enables code sharing between Waker and LocalWaker.
std::task::Context
Polling a Future is to call its method poll, passing a Waker which is
wrapped in a std::task::Context. While “currently, Context only serves to
provide access to a &Waker”, it’s possible to add more fields to Context when
there is need to pass additional data to poll()
. The API was designed this
way for future expanding in a backwards-compatible way.
std::task::RawWaker
RawWakers function like Trait Objects. The RawWakerVTable provides a
common behavior, and the *const ()
pointer stores arbitrary data that the
common behavior applies on.
While inconvenient, this “vtable strategy” has its benefits:
- Breaks away from the requirements that dyn compatibility (formerly “object safety”) enforces.
- Enables reducing allocations.