Skip to content

Commit 237cfa0

Browse files
yoshuawuytsStjepan Glavina
authored and
Stjepan Glavina
committed
add IntoFuture (#259)
* add IntoFuture Signed-off-by: Yoshua Wuyts <[email protected]> * blanket impl for IntoFuture Signed-off-by: Yoshua Wuyts <[email protected]> * cargo fmt Signed-off-by: Yoshua Wuyts <[email protected]> * example Signed-off-by: Yoshua Wuyts <[email protected]> * mark as unstable Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent 23beab4 commit 237cfa0

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/future/into_future.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::future::Future;
2+
3+
/// Convert a type into a `Future`.
4+
///
5+
/// # Examples
6+
///
7+
/// ```
8+
/// use async_std::future::{Future, IntoFuture};
9+
/// use async_std::io;
10+
/// use async_std::pin::Pin;
11+
///
12+
/// struct Client;
13+
///
14+
/// impl Client {
15+
/// pub async fn send(self) -> io::Result<()> {
16+
/// // Send a request
17+
/// Ok(())
18+
/// }
19+
/// }
20+
///
21+
/// impl IntoFuture for Client {
22+
/// type Output = io::Result<()>;
23+
///
24+
/// type Future = Pin<Box<dyn Future<Output = Self::Output>>>;
25+
///
26+
/// fn into_future(self) -> Self::Future {
27+
/// Box::pin(async {
28+
/// self.send().await
29+
/// })
30+
/// }
31+
/// }
32+
/// ```
33+
#[cfg(any(feature = "unstable", feature = "docs"))]
34+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
35+
pub trait IntoFuture {
36+
/// The type of value produced on completion.
37+
type Output;
38+
39+
/// Which kind of future are we turning this into?
40+
type Future: Future<Output = Self::Output>;
41+
42+
/// Create a future from a value
43+
fn into_future(self) -> Self::Future;
44+
}
45+
46+
impl<T: Future> IntoFuture for T {
47+
type Output = T::Output;
48+
49+
type Future = T;
50+
51+
fn into_future(self) -> Self::Future {
52+
self
53+
}
54+
}

src/future/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ mod ready;
6363

6464
cfg_if! {
6565
if #[cfg(any(feature = "unstable", feature = "docs"))] {
66+
mod into_future;
6667
mod timeout;
68+
69+
pub use into_future::IntoFuture;
6770
pub use timeout::{timeout, TimeoutError};
6871
}
6972
}

0 commit comments

Comments
 (0)