trait

IntoFuture: convert a value into an awaitable computation

An await operand is converted using IntoFuture. A builder can implement the conversion to turn its configured inputs into a future; Future implementations also have the conversion available. Construction work in a builder is distinct from polling. Expected stdout (not executed): Ready(13) Common mistake: Assuming an await operand must itself directly implement Future.

Formal Syntax & Structure

rust syntaxrust
trait IntoFuture { type Output; type IntoFuture: Future<Output = Self::Output>; fn into_future(self) -> Self::IntoFuture; }

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);    use std::future::{IntoFuture, Ready, ready};    struct Total(u32, u32);    impl IntoFuture for Total {        type Output = u32;        type IntoFuture = Ready<u32>;        fn into_future(self) -> Self::IntoFuture { ready(self.0 + self.1) }    }    let mut f = std::pin::pin!(async { Total(4, 9).await });    println!("{:?}", f.as_mut().poll(&mut cx));}
Rust Reference