]>
git.proxmox.com Git - rustc.git/blob - library/core/src/future/into_future.rs
1 use crate::future
::Future
;
3 /// Conversion into a `Future`.
5 /// By implementing `IntoFuture` for a type, you define how it will be
6 /// converted to a future.
8 /// # `.await` desugaring
10 /// The `.await` keyword desugars into a call to `IntoFuture::into_future`
11 /// first before polling the future to completion. `IntoFuture` is implemented
12 /// for all `T: Future` which means the `into_future` method will be available
16 /// #![feature(into_future)]
18 /// use std::future::IntoFuture;
20 /// # async fn foo() {
21 /// let v = async { "meow" };
22 /// let mut fut = v.into_future();
23 /// assert_eq!("meow", fut.await);
29 /// When implementing futures manually there will often be a choice between
30 /// implementing `Future` or `IntoFuture` for a type. Implementing `Future` is a
31 /// good choice in most cases. But implementing `IntoFuture` is most useful when
32 /// implementing "async builder" types, which allow their values to be modified
33 /// multiple times before being `.await`ed.
36 /// #![feature(into_future)]
38 /// use std::future::{ready, Ready, IntoFuture};
40 /// /// Eventually multiply two numbers
41 /// pub struct Multiply {
47 /// /// Construct a new instance of `Multiply`.
48 /// pub fn new(num: u16, factor: u16) -> Self {
49 /// Self { num, factor }
52 /// /// Set the number to multiply by the factor.
53 /// pub fn number(mut self, num: u16) -> Self {
58 /// /// Set the factor to multiply the number with.
59 /// pub fn factor(mut self, factor: u16) -> Self {
60 /// self.factor = factor;
65 /// impl IntoFuture for Multiply {
66 /// type Output = u16;
67 /// type IntoFuture = Ready<Self::Output>;
69 /// fn into_future(self) -> Self::IntoFuture {
70 /// ready(self.num * self.factor)
74 /// // NOTE: Rust does not yet have an `async fn main` function, that functionality
75 /// // currently only exists in the ecosystem.
77 /// let num = Multiply::new(0, 0) // initialize the builder to number: 0, factor: 0
78 /// .number(2) // change the number to 2
79 /// .factor(2) // change the factor to 2
80 /// .await; // convert to future and .await
82 /// assert_eq!(num, 4);
86 /// # Usage in trait bounds
88 /// Using `IntoFuture` in trait bounds allows a function to be generic over both
89 /// `Future` and `IntoFuture`. This is convenient for users of the function, so
90 /// when they are using it they don't have to make an extra call to
91 /// `IntoFuture::into_future` to obtain an instance of `Future`:
94 /// #![feature(into_future)]
96 /// use std::future::IntoFuture;
98 /// /// Convert the output of a future to a string.
99 /// async fn fut_to_string<Fut>(fut: Fut) -> String
102 /// Fut::Output: std::fmt::Debug,
104 /// format!("{:?}", fut.await)
107 #[unstable(feature = "into_future", issue = "67644")]
108 pub trait IntoFuture
{
109 /// The output that the future will produce on completion.
110 #[unstable(feature = "into_future", issue = "67644")]
113 /// Which kind of future are we turning this into?
114 #[unstable(feature = "into_future", issue = "67644")]
115 type IntoFuture
: Future
<Output
= Self::Output
>;
117 /// Creates a future from a value.
124 /// #![feature(into_future)]
126 /// use std::future::IntoFuture;
128 /// # async fn foo() {
129 /// let v = async { "meow" };
130 /// let mut fut = v.into_future();
131 /// assert_eq!("meow", fut.await);
134 #[unstable(feature = "into_future", issue = "67644")]
135 #[lang = "into_future"]
136 fn into_future(self) -> Self::IntoFuture
;
139 #[unstable(feature = "into_future", issue = "67644")]
140 impl<F
: Future
> IntoFuture
for F
{
141 type Output
= F
::Output
;
144 fn into_future(self) -> Self::IntoFuture
{