Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 024be2f

Browse files
authored
Unrolled build for rust-lang#119984
Rollup merge of rust-lang#119984 - kpreid:waker-noop, r=dtolnay Change return type of unstable `Waker::noop()` from `Waker` to `&Waker`. The advantage of this is that it does not need to be assigned to a variable to be used in a `Context` creation, which is the most common thing to want to do with a noop waker. It also avoids unnecessarily executing the dynamically dispatched drop function when the noop waker is dropped. If an owned noop waker is desired, it can be created by cloning, but the reverse is harder to do since it requires declaring a constant. Alternatively, both versions could be provided, like `futures::task::noop_waker()` and `futures::task::noop_waker_ref()`, but that seems to me to be API clutter for a very small benefit, whereas having the `&'static` reference available is a large reduction in boilerplate. [Previous discussion on the tracking issue starting here](rust-lang#98286 (comment))
2 parents 0547c41 + c48cdfe commit 024be2f

23 files changed

+36
-51
lines changed

library/core/src/task/wake.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,14 @@ impl Waker {
329329
Waker { waker }
330330
}
331331

332-
/// Creates a new `Waker` that does nothing when `wake` is called.
332+
/// Returns a reference to a `Waker` that does nothing when used.
333333
///
334334
/// This is mostly useful for writing tests that need a [`Context`] to poll
335335
/// some futures, but are not expecting those futures to wake the waker or
336336
/// do not need to do anything specific if it happens.
337337
///
338+
/// If an owned `Waker` is needed, `clone()` this one.
339+
///
338340
/// # Examples
339341
///
340342
/// ```
@@ -343,16 +345,20 @@ impl Waker {
343345
/// use std::future::Future;
344346
/// use std::task;
345347
///
346-
/// let waker = task::Waker::noop();
347-
/// let mut cx = task::Context::from_waker(&waker);
348+
/// let mut cx = task::Context::from_waker(task::Waker::noop());
348349
///
349350
/// let mut future = Box::pin(async { 10 });
350351
/// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10));
351352
/// ```
352353
#[inline]
353354
#[must_use]
354355
#[unstable(feature = "noop_waker", issue = "98286")]
355-
pub const fn noop() -> Waker {
356+
pub const fn noop() -> &'static Waker {
357+
// Ideally all this data would be explicitly `static` because it is used by reference and
358+
// only ever needs one copy. But `const fn`s (and `const` items) cannot refer to statics,
359+
// even though their values can be promoted to static. (That might change; see #119618.)
360+
// An alternative would be a `pub static NOOP: &Waker`, but associated static items are not
361+
// currently allowed either, and making it non-associated would be unergonomic.
356362
const VTABLE: RawWakerVTable = RawWakerVTable::new(
357363
// Cloning just returns a new no-op raw waker
358364
|_| RAW,
@@ -364,8 +370,9 @@ impl Waker {
364370
|_| {},
365371
);
366372
const RAW: RawWaker = RawWaker::new(ptr::null(), &VTABLE);
373+
const WAKER_REF: &Waker = &Waker { waker: RAW };
367374

368-
Waker { waker: RAW }
375+
WAKER_REF
369376
}
370377

371378
/// Get a reference to the underlying [`RawWaker`].

library/core/tests/async_iter/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ fn into_async_iter() {
77
let async_iter = async_iter::from_iter(0..3);
88
let mut async_iter = pin!(async_iter.into_async_iter());
99

10-
let waker = core::task::Waker::noop();
11-
let mut cx = &mut core::task::Context::from_waker(&waker);
10+
let mut cx = &mut core::task::Context::from_waker(core::task::Waker::noop());
1211

1312
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(0)));
1413
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));

src/tools/miri/tests/pass/async-fn.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ async fn uninhabited_variant() {
7676
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
7777
use std::task::{Context, Poll, Waker};
7878

79-
let waker = Waker::noop();
80-
let mut context = Context::from_waker(&waker);
79+
let mut context = Context::from_waker(Waker::noop());
8180

8281
let mut pinned = Box::pin(fut);
8382
loop {

src/tools/miri/tests/pass/dyn-star.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ fn dispatch_on_pin_mut() {
9393
let mut fut = async_main();
9494

9595
// Poll loop, just to test the future...
96-
let waker = Waker::noop();
97-
let ctx = &mut Context::from_waker(&waker);
96+
let ctx = &mut Context::from_waker(Waker::noop());
9897

9998
loop {
10099
match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } {

src/tools/miri/tests/pass/future-self-referential.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ impl Future for DoStuff {
7777
}
7878

7979
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
80-
let waker = Waker::noop();
81-
let mut context = Context::from_waker(&waker);
80+
let mut context = Context::from_waker(Waker::noop());
8281

8382
let mut pinned = pin!(fut);
8483
loop {
@@ -90,8 +89,7 @@ fn run_fut<T>(fut: impl Future<Output = T>) -> T {
9089
}
9190

9291
fn self_referential_box() {
93-
let waker = Waker::noop();
94-
let cx = &mut Context::from_waker(&waker);
92+
let cx = &mut Context::from_waker(Waker::noop());
9593

9694
async fn my_fut() -> i32 {
9795
let val = 10;

src/tools/miri/tests/pass/issues/issue-miri-2068.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use std::task::{Context, Poll, Waker};
66

77
pub fn fuzzing_block_on<O, F: Future<Output = O>>(fut: F) -> O {
88
let mut fut = std::pin::pin!(fut);
9-
let waker = Waker::noop();
10-
let mut context = Context::from_waker(&waker);
9+
let mut context = Context::from_waker(Waker::noop());
1110
loop {
1211
match fut.as_mut().poll(&mut context) {
1312
Poll::Ready(v) => return v,

src/tools/miri/tests/pass/move-data-across-await-point.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ fn data_moved() {
5656
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
5757
use std::task::{Context, Poll, Waker};
5858

59-
let waker = Waker::noop();
60-
let mut context = Context::from_waker(&waker);
59+
let mut context = Context::from_waker(Waker::noop());
6160

6261
let mut pinned = Box::pin(fut);
6362
loop {

tests/coverage/async.coverage

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@
117117
LL| | #[coverage(off)]
118118
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
119119
LL| | let mut future = pin!(future);
120-
LL| | let waker = Waker::noop();
121-
LL| | let mut context = Context::from_waker(&waker);
120+
LL| | let mut context = Context::from_waker(Waker::noop());
122121
LL| |
123122
LL| | loop {
124123
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {

tests/coverage/async.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ mod executor {
110110
#[coverage(off)]
111111
pub fn block_on<F: Future>(mut future: F) -> F::Output {
112112
let mut future = pin!(future);
113-
let waker = Waker::noop();
114-
let mut context = Context::from_waker(&waker);
113+
let mut context = Context::from_waker(Waker::noop());
115114

116115
loop {
117116
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {

tests/coverage/async2.coverage

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
LL| | #[coverage(off)]
4242
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
4343
LL| | let mut future = pin!(future);
44-
LL| | let waker = Waker::noop();
45-
LL| | let mut context = Context::from_waker(&waker);
44+
LL| | let mut context = Context::from_waker(Waker::noop());
4645
LL| |
4746
LL| | loop {
4847
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {

0 commit comments

Comments
 (0)