]> git.proxmox.com Git - rustc.git/blame - library/core/src/task/ready.rs
New upstream version 1.56.0+dfsg1
[rustc.git] / library / core / src / task / ready.rs
CommitLineData
fc512014 1/// Extracts the successful type of a [`Poll<T>`].
3dfed10e 2///
fc512014
XL
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
3dfed10e
XL
7///
8/// # Examples
9///
10/// ```
dc3f5686
XL
11/// #![feature(ready_macro)]
12///
fc512014
XL
13/// use std::task::{ready, Context, Poll};
14/// use std::future::{self, Future};
15/// use std::pin::Pin;
3dfed10e
XL
16///
17/// pub fn do_poll(cx: &mut Context<'_>) -> Poll<()> {
18/// let mut fut = future::ready(42);
19/// let fut = Pin::new(&mut fut);
20///
21/// let num = ready!(fut.poll(cx));
22/// # drop(num);
23/// // ... use num
24///
25/// Poll::Ready(())
26/// }
27/// ```
28///
29/// The `ready!` call expands to:
30///
31/// ```
dc3f5686 32/// # #![feature(ready_macro)]
fc512014
XL
33/// # use std::task::{Context, Poll};
34/// # use std::future::{self, Future};
35/// # use std::pin::Pin;
3dfed10e
XL
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/// ```
dc3f5686 51#[unstable(feature = "ready_macro", issue = "70922")]
3dfed10e
XL
52#[rustc_macro_transparency = "semitransparent"]
53pub 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}