futures_time/future/
into_future.rs

1use std::future::Future;
2
3/// Conversion into a `Future`.
4///
5/// By implementing `Intofuture` for a type, you define how it will be
6/// converted to a future. This is common for types which describe an
7/// asynchronous builder of some kind.
8///
9/// One benefit of implementing `IntoFuture` is that your type will [work
10/// with Rust's `.await` syntax](https://doc.rust-lang.org/std/keyword.await.html).
11///
12/// # Examples
13///
14/// Basic usage:
15///
16/// ```no_run
17/// use futures_time::future::IntoFuture;
18///
19/// # async fn foo() {
20/// let v = async { "meow" };
21/// let mut fut = v.into_future();
22/// assert_eq!("meow", fut.await);
23/// # }
24/// ```
25///
26/// It is common to use `IntoFuture` as a trait bound. This allows
27/// the input type to change, so long as it is still a future.
28/// Additional bounds can be specified by restricting on `Output`:
29///
30/// ```rust
31/// use futures_time::future::IntoFuture;
32/// async fn fut_to_string<Fut>(fut: Fut) -> String
33/// where
34///     Fut: IntoFuture,
35///     Fut::Output: std::fmt::Debug,
36/// {
37///     format!("{:?}", fut.into_future().await)
38/// }
39/// ```
40pub trait IntoFuture {
41    /// The output that the future will produce on completion.
42    type Output;
43
44    /// Which kind of future are we turning this into?
45    type IntoFuture: Future<Output = Self::Output>;
46
47    /// Creates a future from a value.
48    ///
49    /// # Examples
50    ///
51    /// Basic usage:
52    ///
53    /// ```no_run
54    /// use futures_time::future::IntoFuture;
55    ///
56    /// # async fn foo() {
57    /// let v = async { "meow" };
58    /// let mut fut = v.into_future();
59    /// assert_eq!("meow", fut.await);
60    /// # }
61    /// ```
62    fn into_future(self) -> Self::IntoFuture;
63}
64
65impl<F: Future> IntoFuture for F {
66    type Output = F::Output;
67    type IntoFuture = F;
68
69    fn into_future(self) -> Self::IntoFuture {
70        self
71    }
72}