Trait IntoFuture
trait IntoFuture
Conversion into a Future.
By implementing IntoFuture for a type, you define how it will be
converted to a future.
.await desugaring
The .await keyword desugars into a call to IntoFuture::into_future
first before polling the future to completion. IntoFuture is implemented
for all T: Future which means the into_future method will be available
on all futures.
use std::future::IntoFuture;
# async fn foo() {
let v = async { "meow" };
let mut fut = v.into_future();
assert_eq!("meow", fut.await);
# }
Async builders
When implementing futures manually there will often be a choice between
implementing Future or IntoFuture for a type. Implementing Future is a
good choice in most cases. But implementing IntoFuture is most useful when
implementing "async builder" types, which allow their values to be modified
multiple times before being .awaited.
use ;
/// Eventually multiply two numbers
// NOTE: Rust does not yet have an `async fn main` function, that functionality
// currently only exists in the ecosystem.
async
Usage in trait bounds
Using IntoFuture in trait bounds allows a function to be generic over both
Future and IntoFuture. This is convenient for users of the function, so
when they are using it they don't have to make an extra call to
IntoFuture::into_future to obtain an instance of Future:
use IntoFuture;
/// Converts the output of a future to a string.
async
Associated Types
type OutputThe output that the future will produce on completion.
type IntoFuture: TraitBound { trait_: Path { path: "Future", id: Id(215), args: Some(AngleBracketed { args: [], constraints: [AssocItemConstraint { name: "Output", args: None, binding: Equality(Type(QualifiedPath { name: "Output", args: None, self_type: Generic("Self"), trait_: Some(Path { path: "", id: Id(217), args: None }) })) }] }) }, generic_params: [], modifier: None }Which kind of future are we turning this into?
Required Methods
fn into_future(self: Self) -> <Self as >::IntoFutureCreates a future from a value.
Examples
Basic usage:
use std::future::IntoFuture; # async fn foo() { let v = async { "meow" }; let mut fut = v.into_future(); assert_eq!("meow", fut.await); # }