trait

Wake: build a safe notification adapter

Implement Wake to supply an Arc-backed notification action on supported targets. The callback should arrange scheduling rather than performing the entire computation itself. The adapter in this example records a flag only and has no task queue. Expected stdout (not executed): true Common mistake: Implementing raw waker pointer machinery for a simple Arc-backed adapter.

Formal Syntax & Structure

rust syntaxrust
impl Wake for T { fn wake(self: Arc<Self>) { ... } }

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);    let owned = w.clone();    owned.wake();    println!("{}", signal.0.load(Ordering::SeqCst));}
Rust Reference