1//! Futures.
23use core::ops::DerefMut;
4use core::pin::Pin;
5use core::task::{Context, Poll};
67#[doc(no_inline)]
8pub use core::future::Future;
910/// An owned dynamically typed [`Future`] for use in cases where you can't
11/// statically type your result or need to add some indirection.
12#[cfg(feature = "alloc")]
13pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>;
1415/// `BoxFuture`, but without the `Send` requirement.
16#[cfg(feature = "alloc")]
17pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>;
1819/// A future which tracks whether or not the underlying future
20/// should no longer be polled.
21///
22/// `is_terminated` will return `true` if a future should no longer be polled.
23/// Usually, this state occurs after `poll` (or `try_poll`) returned
24/// `Poll::Ready`. However, `is_terminated` may also return `true` if a future
25/// has become inactive and can no longer make progress and should be ignored
26/// or dropped rather than being `poll`ed again.
27pub trait FusedFuture: Future {
28/// Returns `true` if the underlying future should no longer be polled.
29fn is_terminated(&self) -> bool;
30}
3132impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for &mut F {
33fn is_terminated(&self) -> bool {
34 <F as FusedFuture>::is_terminated(&**self)
35 }
36}
3738impl<P> FusedFuture for Pin<P>
39where
40P: DerefMut + Unpin,
41 P::Target: FusedFuture,
42{
43fn is_terminated(&self) -> bool {
44 <P::Target as FusedFuture>::is_terminated(&**self)
45 }
46}
4748mod private_try_future {
49use super::Future;
5051pub trait Sealed {}
5253impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {}
54}
5556/// A convenience for futures that return `Result` values that includes
57/// a variety of adapters tailored to such futures.
58pub trait TryFuture: Future + private_try_future::Sealed {
59/// The type of successful values yielded by this future
60type Ok;
6162/// The type of failures yielded by this future
63type Error;
6465/// Poll this `TryFuture` as if it were a `Future`.
66 ///
67 /// This method is a stopgap for a compiler limitation that prevents us from
68 /// directly inheriting from the `Future` trait; in the future it won't be
69 /// needed.
70fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Self::Ok, Self::Error>>;
71}
7273impl<F, T, E> TryFuture for F
74where
75F: ?Sized + Future<Output = Result<T, E>>,
76{
77type Ok = T;
78type Error = E;
7980#[inline]
81fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
82self.poll(cx)
83 }
84}
8586#[cfg(feature = "alloc")]
87mod if_alloc {
88use super::*;
89use alloc::boxed::Box;
9091impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for Box<F> {
92fn is_terminated(&self) -> bool {
93 <F as FusedFuture>::is_terminated(&**self)
94 }
95 }
9697#[cfg(feature = "std")]
98impl<F: FusedFuture> FusedFuture for std::panic::AssertUnwindSafe<F> {
99fn is_terminated(&self) -> bool {
100 <F as FusedFuture>::is_terminated(&**self)
101 }
102 }
103}