|
9 | 9 | //! from a closure that defines its return value, and [`ready`](ready()),
|
10 | 10 | //! which constructs a future with an immediate defined value.
|
11 | 11 |
|
| 12 | +use core::pin::Pin; |
| 13 | +use futures_core::task::{Poll, Context}; |
| 14 | +#[cfg(feature = "alloc")] |
| 15 | +use alloc::boxed::Box; |
| 16 | + |
12 | 17 | #[doc(no_inline)]
|
13 | 18 | pub use core::future::Future;
|
14 | 19 |
|
15 |
| -#[cfg(feature = "alloc")] |
16 |
| -pub use futures_core::future::{BoxFuture, LocalBoxFuture}; |
17 |
| -pub use futures_core::future::{FusedFuture, TryFuture}; |
| 20 | +pub use futures_core::future::FusedFuture; |
18 | 21 | pub use futures_task::{FutureObj, LocalFutureObj, UnsafeFutureObj};
|
19 | 22 |
|
| 23 | +/// An owned dynamically typed [`Future`] for use in cases where you can't |
| 24 | +/// statically type your result or need to add some indirection. |
| 25 | +#[cfg(feature = "alloc")] |
| 26 | +pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; |
| 27 | + |
| 28 | +/// `BoxFuture`, but without the `Send` requirement. |
| 29 | +#[cfg(feature = "alloc")] |
| 30 | +pub type LocalBoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>; |
| 31 | + |
| 32 | + |
| 33 | +mod private_try_future { |
| 34 | + use super::Future; |
| 35 | + |
| 36 | + pub trait Sealed {} |
| 37 | + |
| 38 | + impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {} |
| 39 | +} |
| 40 | + |
| 41 | +/// A convenience for futures that return `Result` values that includes |
| 42 | +/// a variety of adapters tailored to such futures. |
| 43 | +pub trait TryFuture: Future + private_try_future::Sealed { |
| 44 | + /// The type of successful values yielded by this future |
| 45 | + type Ok; |
| 46 | + |
| 47 | + /// The type of failures yielded by this future |
| 48 | + type Error; |
| 49 | + |
| 50 | + /// Poll this `TryFuture` as if it were a `Future`. |
| 51 | + /// |
| 52 | + /// This method is a stopgap for a compiler limitation that prevents us from |
| 53 | + /// directly inheriting from the `Future` trait; in the future it won't be |
| 54 | + /// needed. |
| 55 | + fn try_poll( |
| 56 | + self: Pin<&mut Self>, |
| 57 | + cx: &mut Context<'_>, |
| 58 | + ) -> Poll<Result<Self::Ok, Self::Error>>; |
| 59 | +} |
| 60 | + |
| 61 | +impl<F, T, E> TryFuture for F |
| 62 | + where F: ?Sized + Future<Output = Result<T, E>> |
| 63 | +{ |
| 64 | + type Ok = T; |
| 65 | + type Error = E; |
| 66 | + |
| 67 | + #[inline] |
| 68 | + fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 69 | + self.poll(cx) |
| 70 | + } |
| 71 | +} |
| 72 | + |
20 | 73 | // Extension traits and combinators
|
21 | 74 | #[allow(clippy::module_inception)]
|
22 | 75 | mod future;
|
|
0 commit comments