std-type

Waker: signal that another poll may be useful

A Waker is a notification handle rather than an execution result. It may be shared across threads and can notify an executor that a task should be considered for polling. Notifications can be coalesced; do not infer one poll per wake. This fixture uses a boolean notification flag, not a runtime. Expected stdout (not executed): true false Common mistake: Assuming every wake immediately runs the future on the calling thread.

Formal Syntax & Structure

rust syntaxrust
Waker::from(Arc<W>); waker.wake(); waker.wake_by_ref();

Working Example

Example Snippetrust
fn main() {    use std::{future::Future, task::{Context, Poll, Wake, Waker}, sync::{Arc, atomic::{AtomicBool, Ordering}}};    struct Signal(AtomicBool);    impl Wake for Signal {        fn wake(self: Arc<Self>) { self.0.store(true, Ordering::SeqCst); }        fn wake_by_ref(self: &Arc<Self>) { self.0.store(true, Ordering::SeqCst); }    }    let signal = Arc::new(Signal(AtomicBool::new(false)));    let w = Waker::from(Arc::clone(&signal));    let mut cx = Context::from_waker(&w);    w.wake_by_ref();    w.wake_by_ref();    println!("{}", signal.0.swap(false, Ordering::SeqCst));    println!("{}", signal.0.load(Ordering::SeqCst));}
Rust Reference