std-method
Waker::wake and wake_by_ref: ownership of notifications
The by-value variant consumes the handle; the borrowed variant leaves it available for later use. Both convey notification to the executor. A clone can be consumed without consuming the original handle. Expected stdout (not executed): true true Common mistake: Attempting to reuse the same owned handle after wake consumes it.
Formal Syntax & Structure
rust syntaxrust
waker.wake(self); waker.wake_by_ref(&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 another = w.clone(); another.wake(); println!("{}", signal.0.swap(false, Ordering::SeqCst)); w.wake_by_ref(); println!("{}", signal.0.load(Ordering::SeqCst));}