Poll::Ready(value) reports that an asynchronous computation has finished and produced its output. An executor receives it when polling a Future. section 10.6
A polling cycle uses Poll::Ready alongside deferred completion:
- Return Poll::Ready with the timer’s String after the expiration time is reached. section 10.5
- Return Poll::Pending before completion, allowing the executor to schedule other tasks. section 10.5
- Resume polling after a Waker informs the async executor that progress may continue. section 10.6
Completing a custom timer
An AsyncTimer checks Instant::now() against its expiration time. Once the deadline arrives, poll returns Poll::Ready(String::from("Future 1 has completed")); otherwise it arranges a wake-up and returns Poll::Pending. section 10.5
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::thread::sleep;
use std::time::{Duration, Instant};
struct AsyncTimer { #1
expiration_time: Instant,
}
impl Future for AsyncTimer { #2
type Output = String; #3
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) ->
Poll<Self::Output> { #4
if Instant::now() >= self.expiration_time { #5
println!("Hello, it's time for Future 1");
Poll::Ready(String::from("Future 1 has completed"))
} else {
println!("Hello, it's not yet time for Future 1. Going to sleep");
let waker = cx.waker().clone();
let expiration_time = self.expiration_time;
std::thread::spawn(move || { #6
let current_time = Instant::now();
if current_time < expiration_time {
std::thread::sleep(expiration_time - current_time);
}
waker.wake(); #7
});
Poll::Pending
}
}
}
#[tokio::main]
async fn main() {
let h1 = tokio::spawn(async {
let future1 = AsyncTimer { #8
expiration_time: Instant::now() + Duration::from_millis(4–––),
};
println!("{:?}", future1.await);
});
let h2 = tokio::spawn(async {
let file2_contents = read_from_file2().await;
println!("{:?}", file2_contents);
});
let _ = tokio::join!(h1, h2);
}
// function that simulates reading from a file
fn read_from_file2() -> impl Future<Output = String> {
async {
sleep(Duration::new(2, –));
String::from("Future 2 has completed")
}
}
The implementation listing shows Poll::Ready in a Future implementation, alongside the timer state and poll signature. section 10.5
