• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Futures.
2 
3 use core::ops::DerefMut;
4 use core::pin::Pin;
5 use core::task::{Context, Poll};
6 
7 #[doc(no_inline)]
8 pub use core::future::Future;
9 
10 /// 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")]
13 pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>;
14 
15 /// `BoxFuture`, but without the `Send` requirement.
16 #[cfg(feature = "alloc")]
17 pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>;
18 
19 /// 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.
27 pub trait FusedFuture: Future {
28     /// Returns `true` if the underlying future should no longer be polled.
is_terminated(&self) -> bool29     fn is_terminated(&self) -> bool;
30 }
31 
32 impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for &mut F {
is_terminated(&self) -> bool33     fn is_terminated(&self) -> bool {
34         <F as FusedFuture>::is_terminated(&**self)
35     }
36 }
37 
38 impl<P> FusedFuture for Pin<P>
39 where
40     P: DerefMut + Unpin,
41     P::Target: FusedFuture,
42 {
is_terminated(&self) -> bool43     fn is_terminated(&self) -> bool {
44         <P::Target as FusedFuture>::is_terminated(&**self)
45     }
46 }
47 
48 mod private_try_future {
49     use super::Future;
50 
51     pub trait Sealed {}
52 
53     impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {}
54 }
55 
56 /// A convenience for futures that return `Result` values that includes
57 /// a variety of adapters tailored to such futures.
58 pub trait TryFuture: Future + private_try_future::Sealed {
59     /// The type of successful values yielded by this future
60     type Ok;
61 
62     /// The type of failures yielded by this future
63     type Error;
64 
65     /// 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.
try_poll( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<Self::Ok, Self::Error>>70     fn try_poll(
71         self: Pin<&mut Self>,
72         cx: &mut Context<'_>,
73     ) -> Poll<Result<Self::Ok, Self::Error>>;
74 }
75 
76 impl<F, T, E> TryFuture for F
77     where F: ?Sized + Future<Output = Result<T, E>>
78 {
79     type Ok = T;
80     type Error = E;
81 
82     #[inline]
try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>83     fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
84         self.poll(cx)
85     }
86 }
87 
88 #[cfg(feature = "alloc")]
89 mod if_alloc {
90     use alloc::boxed::Box;
91     use super::*;
92 
93     impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for Box<F> {
is_terminated(&self) -> bool94         fn is_terminated(&self) -> bool {
95             <F as FusedFuture>::is_terminated(&**self)
96         }
97     }
98 
99     #[cfg(feature = "std")]
100     impl<F: FusedFuture> FusedFuture for std::panic::AssertUnwindSafe<F> {
is_terminated(&self) -> bool101         fn is_terminated(&self) -> bool {
102             <F as FusedFuture>::is_terminated(&**self)
103         }
104     }
105 }
106