|
| 1 | +/// Extracts the successful type of a `Poll<T>`. |
| 2 | +/// |
| 3 | +/// This macro bakes in propagation of `Pending` signals by returning early. |
| 4 | +/// |
| 5 | +/// # Examples |
| 6 | +/// |
| 7 | +/// ``` |
| 8 | +/// #![feature(future_readiness_fns)] |
| 9 | +/// #![feature(ready_macro)] |
| 10 | +/// |
| 11 | +/// use core::task::{ready, Context, Poll}; |
| 12 | +/// use core::future::{self, Future}; |
| 13 | +/// use core::pin::Pin; |
| 14 | +/// |
| 15 | +/// pub fn do_poll(cx: &mut Context<'_>) -> Poll<()> { |
| 16 | +/// let mut fut = future::ready(42); |
| 17 | +/// let fut = Pin::new(&mut fut); |
| 18 | +/// |
| 19 | +/// let num = ready!(fut.poll(cx)); |
| 20 | +/// # drop(num); |
| 21 | +/// // ... use num |
| 22 | +/// |
| 23 | +/// Poll::Ready(()) |
| 24 | +/// } |
| 25 | +/// ``` |
| 26 | +/// |
| 27 | +/// The `ready!` call expands to: |
| 28 | +/// |
| 29 | +/// ``` |
| 30 | +/// # #![feature(future_readiness_fns)] |
| 31 | +/// # #![feature(ready_macro)] |
| 32 | +/// # |
| 33 | +/// # use core::task::{Context, Poll}; |
| 34 | +/// # use core::future::{self, Future}; |
| 35 | +/// # use core::pin::Pin; |
| 36 | +/// # |
| 37 | +/// # pub fn do_poll(cx: &mut Context<'_>) -> Poll<()> { |
| 38 | +/// # let mut fut = future::ready(42); |
| 39 | +/// # let fut = Pin::new(&mut fut); |
| 40 | +/// # |
| 41 | +/// let num = match fut.poll(cx) { |
| 42 | +/// Poll::Ready(t) => t, |
| 43 | +/// Poll::Pending => return Poll::Pending, |
| 44 | +/// }; |
| 45 | +/// # drop(num); |
| 46 | +/// # // ... use num |
| 47 | +/// # |
| 48 | +/// # Poll::Ready(()) |
| 49 | +/// # } |
| 50 | +/// ``` |
| 51 | +#[unstable(feature = "ready_macro", issue = "70922")] |
| 52 | +#[rustc_macro_transparency = "semitransparent"] |
| 53 | +pub macro ready($e:expr) { |
| 54 | + match $e { |
| 55 | + $crate::task::Poll::Ready(t) => t, |
| 56 | + $crate::task::Poll::Pending => { |
| 57 | + return $crate::task::Poll::Pending; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments