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 /// 13 /// This type is often created by the [`boxed`] method on [`FutureExt`]. See its documentation for more. 14 /// 15 /// [`boxed`]: https://docs.rs/futures/latest/futures/future/trait.FutureExt.html#method.boxed 16 /// [`FutureExt`]: https://docs.rs/futures/latest/futures/future/trait.FutureExt.html 17 #[cfg(feature = "alloc")] 18 pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>; 19 20 /// `BoxFuture`, but without the `Send` requirement. 21 /// 22 /// This type is often created by the [`boxed_local`] method on [`FutureExt`]. See its documentation for more. 23 /// 24 /// [`boxed_local`]: https://docs.rs/futures/latest/futures/future/trait.FutureExt.html#method.boxed_local 25 /// [`FutureExt`]: https://docs.rs/futures/latest/futures/future/trait.FutureExt.html 26 #[cfg(feature = "alloc")] 27 pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>; 28 29 /// A future which tracks whether or not the underlying future 30 /// should no longer be polled. 31 /// 32 /// `is_terminated` will return `true` if a future should no longer be polled. 33 /// Usually, this state occurs after `poll` (or `try_poll`) returned 34 /// `Poll::Ready`. However, `is_terminated` may also return `true` if a future 35 /// has become inactive and can no longer make progress and should be ignored 36 /// or dropped rather than being `poll`ed again. 37 pub trait FusedFuture: Future { 38 /// Returns `true` if the underlying future should no longer be polled. is_terminated(&self) -> bool39 fn is_terminated(&self) -> bool; 40 } 41 42 impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for &mut F { is_terminated(&self) -> bool43 fn is_terminated(&self) -> bool { 44 <F as FusedFuture>::is_terminated(&**self) 45 } 46 } 47 48 impl<P> FusedFuture for Pin<P> 49 where 50 P: DerefMut + Unpin, 51 P::Target: FusedFuture, 52 { is_terminated(&self) -> bool53 fn is_terminated(&self) -> bool { 54 <P::Target as FusedFuture>::is_terminated(&**self) 55 } 56 } 57 58 mod private_try_future { 59 use super::Future; 60 61 pub trait Sealed {} 62 63 impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {} 64 } 65 66 /// A convenience for futures that return `Result` values that includes 67 /// a variety of adapters tailored to such futures. 68 pub trait TryFuture: Future + private_try_future::Sealed { 69 /// The type of successful values yielded by this future 70 type Ok; 71 72 /// The type of failures yielded by this future 73 type Error; 74 75 /// Poll this `TryFuture` as if it were a `Future`. 76 /// 77 /// This method is a stopgap for a compiler limitation that prevents us from 78 /// directly inheriting from the `Future` trait; in the future it won't be 79 /// needed. try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Self::Ok, Self::Error>>80 fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Self::Ok, Self::Error>>; 81 } 82 83 impl<F, T, E> TryFuture for F 84 where 85 F: ?Sized + Future<Output = Result<T, E>>, 86 { 87 type Ok = T; 88 type Error = E; 89 90 #[inline] try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>91 fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { 92 self.poll(cx) 93 } 94 } 95 96 #[cfg(feature = "alloc")] 97 mod if_alloc { 98 use super::*; 99 use alloc::boxed::Box; 100 101 impl<F: FusedFuture + ?Sized + Unpin> FusedFuture for Box<F> { is_terminated(&self) -> bool102 fn is_terminated(&self) -> bool { 103 <F as FusedFuture>::is_terminated(&**self) 104 } 105 } 106 107 #[cfg(feature = "std")] 108 impl<F: FusedFuture> FusedFuture for std::panic::AssertUnwindSafe<F> { is_terminated(&self) -> bool109 fn is_terminated(&self) -> bool { 110 <F as FusedFuture>::is_terminated(&**self) 111 } 112 } 113 } 114