logo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::future::Future;

/// Convert a type into a `Future`.
///
/// # Examples
///
/// ```
/// use async_std::future::{Future, IntoFuture};
/// use async_std::io;
/// use async_std::pin::Pin;
///
/// struct Client;
///
/// impl Client {
///     pub async fn send(self) -> io::Result<()> {
///         // Send a request
///         Ok(())
///     }
/// }
///
/// impl IntoFuture for Client {
///     type Output = io::Result<()>;
///
///     type Future = Pin<Box<dyn Future<Output = Self::Output>>>;
///
///     fn into_future(self) -> Self::Future {
///         Box::pin(async {
///             self.send().await
///         })
///     }
/// }
/// ```
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
pub trait IntoFuture {
    /// The type of value produced on completion.
    type Output;

    /// Which kind of future are we turning this into?
    type Future: Future<Output = Self::Output>;

    /// Create a future from a value
    fn into_future(self) -> Self::Future;
}

impl<T: Future> IntoFuture for T {
    type Output = T::Output;
    type Future = T;

    fn into_future(self) -> Self::Future {
        self
    }
}