From 012f6b61770555321afe0ace5cd9f9b6f92a003a Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Sat, 28 Sep 2019 19:43:24 +0200 Subject: [PATCH 1/5] add IntoFuture Signed-off-by: Yoshua Wuyts --- src/future/into_future.rs | 13 +++++++++++++ src/future/mod.rs | 2 ++ 2 files changed, 15 insertions(+) create mode 100644 src/future/into_future.rs diff --git a/src/future/into_future.rs b/src/future/into_future.rs new file mode 100644 index 000000000..090eb4cdb --- /dev/null +++ b/src/future/into_future.rs @@ -0,0 +1,13 @@ +use crate::future::Future; + +/// Convert a type into a `Future`. +pub trait IntoFuture { + /// The type of value produced on completion. + type Output; + + /// Which kind of future are we turning this into? + type Future: Future; + + /// Create a future from a value + fn into_future(self) -> Self::Future; +} diff --git a/src/future/mod.rs b/src/future/mod.rs index 1f67e1aa5..7e25b4485 100644 --- a/src/future/mod.rs +++ b/src/future/mod.rs @@ -53,7 +53,9 @@ use cfg_if::cfg_if; pub use pending::pending; pub use poll_fn::poll_fn; pub use ready::ready; +pub use into_future::IntoFuture; +mod into_future; mod pending; mod poll_fn; mod ready; From b3c688ebb9f54d85481e33ac2d393f12c0b67214 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Sat, 28 Sep 2019 19:46:17 +0200 Subject: [PATCH 2/5] blanket impl for IntoFuture Signed-off-by: Yoshua Wuyts --- src/future/into_future.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/future/into_future.rs b/src/future/into_future.rs index 090eb4cdb..6b66ce772 100644 --- a/src/future/into_future.rs +++ b/src/future/into_future.rs @@ -11,3 +11,13 @@ pub trait IntoFuture { /// Create a future from a value fn into_future(self) -> Self::Future; } + +impl IntoFuture for T { + type Output = T::Output; + + type Future = T; + + fn into_future(self) -> Self::Future { + self + } +} From 722b354fd072d409aa32126e212c134f750d59da Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Sun, 29 Sep 2019 04:02:52 +0200 Subject: [PATCH 3/5] cargo fmt Signed-off-by: Yoshua Wuyts --- src/future/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/future/mod.rs b/src/future/mod.rs index 7e25b4485..b48f9d93f 100644 --- a/src/future/mod.rs +++ b/src/future/mod.rs @@ -50,10 +50,10 @@ pub use async_macros::{join, select, try_join, try_select}; use cfg_if::cfg_if; +pub use into_future::IntoFuture; pub use pending::pending; pub use poll_fn::poll_fn; pub use ready::ready; -pub use into_future::IntoFuture; mod into_future; mod pending; From d7e8b2d501af95caeba1a46d0b56d5b62f49dcd8 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Sun, 29 Sep 2019 04:14:29 +0200 Subject: [PATCH 4/5] example Signed-off-by: Yoshua Wuyts --- src/future/into_future.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/future/into_future.rs b/src/future/into_future.rs index 6b66ce772..aa23b5d7f 100644 --- a/src/future/into_future.rs +++ b/src/future/into_future.rs @@ -1,6 +1,35 @@ use crate::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>>; +/// +/// fn into_future(self) -> Self::Future { +/// Box::pin(async { +/// self.send().await +/// }) +/// } +/// } +/// ``` pub trait IntoFuture { /// The type of value produced on completion. type Output; From 9eab45f129b81cab98dec38fdd1241627ede975f Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Tue, 15 Oct 2019 15:48:13 +0200 Subject: [PATCH 5/5] mark as unstable Signed-off-by: Yoshua Wuyts --- src/future/into_future.rs | 2 ++ src/future/mod.rs | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/future/into_future.rs b/src/future/into_future.rs index aa23b5d7f..58b676615 100644 --- a/src/future/into_future.rs +++ b/src/future/into_future.rs @@ -30,6 +30,8 @@ use crate::future::Future; /// } /// } /// ``` +#[cfg(any(feature = "unstable", feature = "docs"))] +#[cfg_attr(feature = "docs", doc(cfg(unstable)))] pub trait IntoFuture { /// The type of value produced on completion. type Output; diff --git a/src/future/mod.rs b/src/future/mod.rs index b48f9d93f..b1a68b991 100644 --- a/src/future/mod.rs +++ b/src/future/mod.rs @@ -50,19 +50,20 @@ pub use async_macros::{join, select, try_join, try_select}; use cfg_if::cfg_if; -pub use into_future::IntoFuture; pub use pending::pending; pub use poll_fn::poll_fn; pub use ready::ready; -mod into_future; mod pending; mod poll_fn; mod ready; cfg_if! { if #[cfg(any(feature = "unstable", feature = "docs"))] { + mod into_future; mod timeout; + + pub use into_future::IntoFuture; pub use timeout::{timeout, TimeoutError}; } }