]>
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 /// use std::future::IntoFuture;
18 /// # async fn foo() {
19 /// let v = async { "meow" };
20 /// let mut fut = v.into_future();
21 /// assert_eq!("meow", fut.await);
27 /// When implementing futures manually there will often be a choice between
28 /// implementing `Future` or `IntoFuture` for a type. Implementing `Future` is a
29 /// good choice in most cases. But implementing `IntoFuture` is most useful when
30 /// implementing "async builder" types, which allow their values to be modified
31 /// multiple times before being `.await`ed.
34 /// use std::future::{ready, Ready, IntoFuture};
36 /// /// Eventually multiply two numbers
37 /// pub struct Multiply {
43 /// /// Construct a new instance of `Multiply`.
44 /// pub fn new(num: u16, factor: u16) -> Self {
45 /// Self { num, factor }
48 /// /// Set the number to multiply by the factor.
49 /// pub fn number(mut self, num: u16) -> Self {
54 /// /// Set the factor to multiply the number with.
55 /// pub fn factor(mut self, factor: u16) -> Self {
56 /// self.factor = factor;
61 /// impl IntoFuture for Multiply {
62 /// type Output = u16;
63 /// type IntoFuture = Ready<Self::Output>;
65 /// fn into_future(self) -> Self::IntoFuture {
66 /// ready(self.num * self.factor)
70 /// // NOTE: Rust does not yet have an `async fn main` function, that functionality
71 /// // currently only exists in the ecosystem.
73 /// let num = Multiply::new(0, 0) // initialize the builder to number: 0, factor: 0
74 /// .number(2) // change the number to 2
75 /// .factor(2) // change the factor to 2
76 /// .await; // convert to future and .await
78 /// assert_eq!(num, 4);
82 /// # Usage in trait bounds
84 /// Using `IntoFuture` in trait bounds allows a function to be generic over both
85 /// `Future` and `IntoFuture`. This is convenient for users of the function, so
86 /// when they are using it they don't have to make an extra call to
87 /// `IntoFuture::into_future` to obtain an instance of `Future`:
90 /// use std::future::IntoFuture;
92 /// /// Convert the output of a future to a string.
93 /// async fn fut_to_string<Fut>(fut: Fut) -> String
96 /// Fut::Output: std::fmt::Debug,
98 /// format!("{:?}", fut.await)
101 #[stable(feature = "into_future", since = "1.64.0")]
102 pub trait IntoFuture
{
103 /// The output that the future will produce on completion.
104 #[stable(feature = "into_future", since = "1.64.0")]
107 /// Which kind of future are we turning this into?
108 #[stable(feature = "into_future", since = "1.64.0")]
109 type IntoFuture
: Future
<Output
= Self::Output
>;
111 /// Creates a future from a value.
118 /// use std::future::IntoFuture;
120 /// # async fn foo() {
121 /// let v = async { "meow" };
122 /// let mut fut = v.into_future();
123 /// assert_eq!("meow", fut.await);
126 #[stable(feature = "into_future", since = "1.64.0")]
127 #[lang = "into_future"]
128 fn into_future(self) -> Self::IntoFuture
;
131 #[stable(feature = "into_future", since = "1.64.0")]
132 impl<F
: Future
> IntoFuture
for F
{
133 type Output
= F
::Output
;
136 fn into_future(self) -> Self::IntoFuture
{