expression
A bounded single-task driver for a self-waking future
A scheduler decides when to drive a task after a wake notification. This toy driver supports only synchronous self-notifications from a fixed future; its finite iteration cap and failure on missing notification deliberately exclude general external I/O, timers, fairness and multi-task scheduling. Expected stdout (not executed): 3 Common mistake: Using this bounded demonstration as an executor for arbitrary futures.
Formal Syntax & Structure
rust syntaxrust
poll → Pending + notification → poll → ReadyWorking 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 mut n = 0; let mut f = std::pin::pin!(std::future::poll_fn(|cx| { n += 1; if n == 3 { Poll::Ready(n) } else { cx.waker().wake_by_ref(); Poll::Pending } })); let mut answer = None; for _ in 0..4 { match f.as_mut().poll(&mut cx) { Poll::Ready(v) => { answer = Some(v); break; } Poll::Pending => assert!(signal.0.swap(false, Ordering::SeqCst), "external events unsupported"), } } println!("{}", answer.expect("bounded fixture did not finish"));}