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(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Self::Ok, Self::Error>>; 71 } 72 73 impl<F, T, E> TryFuture for F 74 where 75 F: ?Sized + Future<Output = Result<T, E>>, 76 { 77 type Ok = T; 78 type Error = E; 79 80 #[inline] try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>81 fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { 82 self.poll(cx) 83 } 84 } 85 86 #[cfg(feature = "alloc")] 87 mod if_alloc { 88 use super::*; 89 use alloc::boxed::Box; 90 91 impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for Box<F> { is_terminated(&self) -> bool92 fn is_terminated(&self) -> bool { 93 <F as FusedFuture>::is_terminated(&**self) 94 } 95 } 96 97 #[cfg(feature = "std")] 98 impl<F: FusedFuture> FusedFuture for std::panic::AssertUnwindSafe<F> { is_terminated(&self) -> bool99 fn is_terminated(&self) -> bool { 100 <F as FusedFuture>::is_terminated(&**self) 101 } 102 } 103 } 104