core/task/
ready.rs

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