Skip to content

Commit 479c8ad

Browse files
authored
Rollup merge of #70817 - yoshuawuyts:task-ready, r=dtolnay
Add core::task::ready! macro This PR adds `ready!` as a top-level macro to `libcore` following the implementation of `futures_core::ready`, tracking issue #70922. This macro is commonly used when implementing `Future`, `AsyncRead`, `AsyncWrite` and `Stream`. And being only 5 lines, it seems like a useful and straight forward addition to std. ## Example ```rust use core::task::{Context, Poll}; use core::future::Future; use core::pin::Pin; async fn get_num() -> usize { 42 } pub fn do_poll(cx: &mut Context<'_>) -> Poll<()> { let mut f = get_num(); let f = unsafe { Pin::new_unchecked(&mut f) }; let num = ready!(f.poll(cx)); // ... use num Poll::Ready(()) } ``` ## Naming In `async-std` we chose to nest the macro under the `task` module instead of having the macro at the top-level. This is a pattern that currently does not occur in std, mostly due to this not being possible prior to Rust 2018. This PR proposes to add the `ready` macro as `core::ready`. But another option would be to introduce it as `core::task::ready` since it's really only useful when used in conjunction with `task::{Context, Poll}`. ## Implementation questions I tried rendering the documentation locally but the macro didn't show up under `core`. I'm not sure if I quite got this right. I used the [`todo!` macro PR](https://github.com/rust-lang/rust/pull/56348/files) as a reference, and our approaches look similar. ## References - [`futures::ready`](https://docs.rs/futures/0.3.4/futures/macro.ready.html) - [`async_std::task::ready`](https://docs.rs/async-std/1.5.0/async_std/task/index.html) - [`futures_core::ready`](https://docs.rs/futures-core/0.3.4/futures_core/macro.ready.html)
2 parents 1fa54ad + 18be370 commit 479c8ad

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/libcore/task/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ pub use self::poll::Poll;
99
mod wake;
1010
#[stable(feature = "futures_api", since = "1.36.0")]
1111
pub use self::wake::{Context, RawWaker, RawWakerVTable, Waker};
12+
13+
mod ready;
14+
#[unstable(feature = "ready_macro", issue = "70922")]
15+
pub use ready::ready;

src/libcore/task/ready.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
#![feature(ptr_internals)]
306306
#![feature(raw)]
307307
#![feature(raw_ref_macros)]
308+
#![feature(ready_macro)]
308309
#![feature(renamed_spin_loop)]
309310
#![feature(rustc_attrs)]
310311
#![feature(rustc_private)]

0 commit comments

Comments
 (0)