mirror of
https://github.com/async-rs/async-std.git
synced 2025-02-06 12:45:32 +00:00
add IntoFuture (#259)
* add IntoFuture Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * blanket impl for IntoFuture Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * cargo fmt Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * example Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * mark as unstable Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
This commit is contained in:
parent
23beab4125
commit
237cfa0315
2 changed files with 57 additions and 0 deletions
54
src/future/into_future.rs
Normal file
54
src/future/into_future.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -63,7 +63,10 @@ mod ready;
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(any(feature = "unstable", feature = "docs"))] {
|
if #[cfg(any(feature = "unstable", feature = "docs"))] {
|
||||||
|
mod into_future;
|
||||||
mod timeout;
|
mod timeout;
|
||||||
|
|
||||||
|
pub use into_future::IntoFuture;
|
||||||
pub use timeout::{timeout, TimeoutError};
|
pub use timeout::{timeout, TimeoutError};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue