Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IntoFuture #259

Merged
5 commits merged into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/future/into_future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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<Box<dyn Future<Output = Self::Output>>>;
///
/// fn into_future(self) -> Self::Future {
/// Box::pin(async {
/// self.send().await
/// })
/// }
/// }
/// ```
#[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;

/// 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
}
}
3 changes: 3 additions & 0 deletions src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ 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};
}
}