
Writing a Future by Hand in Rust With Poll and Wakers
An async fn is a state machine behind the Future trait. Writing a future yourself, with poll and a waker, removes the mystery from await.
Tag
5 posts

An async fn is a state machine behind the Future trait. Writing a future yourself, with poll and a waker, removes the mystery from await.

Futures are lazy: calling an async fn without .await does nothing. The unused_must_use warning is the compiler catching a missing await, a forgotten spawn, or a dropped JoinHandle.

Pin<Box<dyn Future<Output = T>>> looks like noise until each word has a job. Learn why a future must be pinned, why it is boxed, why it is dyn, and how to write it with less ceremony.

A Stream is the async version of Iterator. See next().await, StreamExt adapters, creating streams from channels and iterators, and where Stream is headed.

An async fn compiles to a state machine that implements Future. See the generated shape, what poll does, and why a future does nothing until it is polled.