std-type

Context: notification information for the current task

A caller supplies a task context at poll time. A future waiting for a resource can clone the current waker to notify the task later. Do not retain the borrowed Context beyond the call. For a real resource, update a cached waker when the task changes. Expected stdout (not executed): true Common mistake: Storing a reference to the short-lived Context in an asynchronous resource.

Formal Syntax & Structure

rust syntaxrust
Context::from_waker(&waker) -> Context; cx.waker() -> &Waker

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);    cx.waker().wake_by_ref();    println!("{}", signal.0.load(Ordering::SeqCst));}
Rust Reference