std-function
future::ready: immediately available value
Create a named future for an already computed value. The argument is evaluated before the future is created, so expensive synchronous work placed inside the argument is not deferred. Expected stdout (not executed): construct value created Ready(8) Common mistake: Expecting ready(expensive_call()) to defer the call until polling.
Formal Syntax & Structure
rust syntaxrust
std::future::ready(value) -> Ready<T>Working Example
Example Snippetrust
fn main() { use std::{future::Future, task::{Context, Poll, Wake, Waker}, sync::Arc}; struct Noop; impl Wake for Noop { fn wake(self: Arc<Self>) {} } let w = Waker::from(Arc::new(Noop)); let mut cx = Context::from_waker(&w); let f = std::future::ready({ println!("construct value"); 8 }); println!("created"); let mut f = std::pin::pin!(f); println!("{:?}", f.as_mut().poll(&mut cx));}