The wake() method on a Rust Waker tells the asynchronous executor that its associated task should be scheduled for polling again. A task that returns Poll::Pending stores a Waker handle in its Context rather than being polled continuously. section 10.4
In a custom Future implementation, the wake-and-poll cycle works like this:
- Implement poll with a Pin<&mut Self> and mutable Context reference. section 10.4
- Return Poll::Pending when the asynchronous computation is not ready. section 10.6
- Call cx.waker().wake_by_ref() to wake the associated task; the executor then invokes poll again. section 10.4
- Return Poll::Ready when the computation finishes; the Tokio runtime does not poll the completed future again. section 10.4
Self-waking custom future
A custom ReadFileFuture can print a message, call cx.waker().wake_by_ref(), and return Poll::Pending. This causes repeated polling until the implementation returns Poll::Ready with file contents. section 10.4
Trace the custom future's waker flow from poll to wake and back to another poll. section 10.4
