3 use core::ops::{ControlFlow, FromResidual, Try};
6 /// Extracts the successful type of a [`Poll<T>`].
8 /// This macro bakes in propagation of [`Pending`] signals by returning early.
10 /// [`Poll<T>`]: crate::task::Poll
11 /// [`Pending`]: crate::task::Poll::Pending
16 /// #![feature(ready_macro)]
18 /// use std::task::{ready, Context, Poll};
19 /// use std::future::{self, Future};
20 /// use std::pin::Pin;
22 /// pub fn do_poll(cx: &mut Context<'_>) -> Poll<()> {
23 /// let mut fut = future::ready(42);
24 /// let fut = Pin::new(&mut fut);
26 /// let num = ready!(fut.poll(cx));
34 /// The `ready!` call expands to:
37 /// # #![feature(ready_macro)]
38 /// # use std::task::{Context, Poll};
39 /// # use std::future::{self, Future};
40 /// # use std::pin::Pin;
42 /// # pub fn do_poll(cx: &mut Context<'_>) -> Poll<()> {
43 /// # let mut fut = future::ready(42);
44 /// # let fut = Pin::new(&mut fut);
46 /// let num = match fut.poll(cx) {
47 /// Poll::Ready(t) => t,
48 /// Poll::Pending => return Poll::Pending,
56 #[unstable(feature = "ready_macro", issue = "70922")]
57 #[rustc_macro_transparency = "semitransparent"]
58 pub macro ready($e:expr) {
60 $crate::task::Poll::Ready(t) => t,
61 $crate::task::Poll::Pending => {
62 return $crate::task::Poll::Pending;
67 /// Extracts the successful type of a [`Poll<T>`].
69 /// See [`Poll::ready`] for details.
70 #[unstable(feature = "poll_ready", issue = "89780")]
71 pub struct Ready<T>(pub(crate) Poll<T>);
73 #[unstable(feature = "poll_ready", issue = "89780")]
74 impl<T> Try for Ready<T> {
76 type Residual = Ready<convert::Infallible>;
79 fn from_output(output: Self::Output) -> Self {
80 Ready(Poll::Ready(output))
84 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
86 Poll::Ready(v) => ControlFlow::Continue(v),
87 Poll::Pending => ControlFlow::Break(Ready(Poll::Pending)),
92 #[unstable(feature = "poll_ready", issue = "89780")]
93 impl<T> FromResidual for Ready<T> {
95 fn from_residual(residual: Ready<convert::Infallible>) -> Self {
97 Poll::Pending => Ready(Poll::Pending),
102 #[unstable(feature = "poll_ready", issue = "89780")]
103 impl<T> FromResidual<Ready<convert::Infallible>> for Poll<T> {
105 fn from_residual(residual: Ready<convert::Infallible>) -> Self {
107 Poll::Pending => Poll::Pending,
112 #[unstable(feature = "poll_ready", issue = "89780")]
113 impl<T> fmt::Debug for Ready<T> {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 f.debug_tuple("Ready").finish()