Skip to content

Commit

Permalink
Turn unstable Waker::noop() into Waker::NOOP.
Browse files Browse the repository at this point in the history
The advantage of this is that `&Waker::NOOP` can live for '`static`,
so it does not need to be assigned to a variable to be used in a
`Context` creation, which is, as shown in the changes to the tests in
this commit, is the most common thing to want to do with a noop waker.

An alternative would be to make it be of type `&'static Waker`,
which would make that common case even shorter, but that would mean that
if an owned `Waker` is desired it must be `.clone()`d from the constant.
Or, both versions could be provided, like `futures::task::noop_waker()`
and `futures::task::noop_waker_ref()`. But either option seems less
elegant to me.

Previous discussion on the tracking issue starting here:
rust-lang#98286 (comment)
  • Loading branch information
kpreid committed Jan 14, 2024
1 parent 3deb9bb commit e8e1fb1
Show file tree
Hide file tree
Showing 22 changed files with 26 additions and 51 deletions.
11 changes: 4 additions & 7 deletions library/core/src/task/wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl Waker {
Waker { waker }
}

/// Creates a new `Waker` that does nothing when `wake` is called.
/// A `Waker` that does nothing when its `wake()` is called.
///
/// This is mostly useful for writing tests that need a [`Context`] to poll
/// some futures, but are not expecting those futures to wake the waker or
Expand All @@ -343,16 +343,13 @@ impl Waker {
/// use std::future::Future;
/// use std::task;
///
/// let waker = task::Waker::noop();
/// let mut cx = task::Context::from_waker(&waker);
/// let mut cx = task::Context::from_waker(&task::Waker::NOOP);
///
/// let mut future = Box::pin(async { 10 });
/// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10));
/// ```
#[inline]
#[must_use]
#[unstable(feature = "noop_waker", issue = "98286")]
pub const fn noop() -> Waker {
pub const NOOP: Waker = {
const VTABLE: RawWakerVTable = RawWakerVTable::new(
// Cloning just returns a new no-op raw waker
|_| RAW,
Expand All @@ -366,7 +363,7 @@ impl Waker {
const RAW: RawWaker = RawWaker::new(ptr::null(), &VTABLE);

Waker { waker: RAW }
}
};

/// Get a reference to the underlying [`RawWaker`].
#[inline]
Expand Down
3 changes: 1 addition & 2 deletions library/core/tests/async_iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ fn into_async_iter() {
let async_iter = async_iter::from_iter(0..3);
let mut async_iter = pin!(async_iter.into_async_iter());

let waker = core::task::Waker::noop();
let mut cx = &mut core::task::Context::from_waker(&waker);
let mut cx = &mut core::task::Context::from_waker(&core::task::Waker::NOOP);

assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(0)));
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/pass/async-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ async fn uninhabited_variant() {
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
use std::task::{Context, Poll, Waker};

let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(&Waker::NOOP);

let mut pinned = Box::pin(fut);
loop {
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/pass/dyn-star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ fn dispatch_on_pin_mut() {
let mut fut = async_main();

// Poll loop, just to test the future...
let waker = Waker::noop();
let ctx = &mut Context::from_waker(&waker);
let ctx = &mut Context::from_waker(&Waker::NOOP);

loop {
match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } {
Expand Down
6 changes: 2 additions & 4 deletions src/tools/miri/tests/pass/future-self-referential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ impl Future for DoStuff {
}

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

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

fn self_referential_box() {
let waker = Waker::noop();
let cx = &mut Context::from_waker(&waker);
let cx = &mut Context::from_waker(&Waker::NOOP);

async fn my_fut() -> i32 {
let val = 10;
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/pass/issues/issue-miri-2068.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::task::{Context, Poll, Waker};

pub fn fuzzing_block_on<O, F: Future<Output = O>>(fut: F) -> O {
let mut fut = std::pin::pin!(fut);
let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(&Waker::NOOP);
loop {
match fut.as_mut().poll(&mut context) {
Poll::Ready(v) => return v,
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/pass/move-data-across-await-point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ fn data_moved() {
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
use std::task::{Context, Poll, Waker};

let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(&Waker::NOOP);

let mut pinned = Box::pin(fut);
loop {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/async.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let waker = Waker::noop();
LL| | let mut context = Context::from_waker(&waker);
LL| | let mut context = Context::from_waker(&Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(&Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/async2.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let waker = Waker::noop();
LL| | let mut context = Context::from_waker(&waker);
LL| | let mut context = Context::from_waker(&Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/async2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(&Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/async_block.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let waker = Waker::noop();
LL| | let mut context = Context::from_waker(&waker);
LL| | let mut context = Context::from_waker(&Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/async_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(&Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/closure_macro_async.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output {
LL| | let mut future = pin!(future);
LL| | let waker = Waker::noop();
LL| | let mut context = Context::from_waker(&waker);
LL| | let mut context = Context::from_waker(&Waker::NOOP);
LL| |
LL| | loop {
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/closure_macro_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(mut future: F) -> F::Output {
let mut future = pin!(future);
let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(&Waker::NOOP);

loop {
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/async-await/for-await-passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ async fn real_main() {

fn main() {
let future = real_main();
let waker = std::task::Waker::noop();
let mut cx = &mut core::task::Context::from_waker(&waker);
let mut cx = &mut core::task::Context::from_waker(&std::task::Waker::NOOP);
let mut future = core::pin::pin!(future);
while let core::task::Poll::Pending = future.as_mut().poll(&mut cx) {}
}
3 changes: 1 addition & 2 deletions tests/ui/async-await/for-await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ async fn real_main() {

fn main() {
let future = real_main();
let waker = std::task::Waker::noop();
let mut cx = &mut core::task::Context::from_waker(&waker);
let mut cx = &mut core::task::Context::from_waker(&std::task::Waker::NOOP);
let mut future = core::pin::pin!(future);
while let core::task::Poll::Pending = future.as_mut().poll(&mut cx) {}
}
3 changes: 1 addition & 2 deletions tests/ui/async-await/in-trait/async-default-fn-overridden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let waker = Waker::noop();
let ctx = &mut Context::from_waker(&waker);
let ctx = &mut Context::from_waker(&Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let waker = Waker::noop();
let ctx = &mut Context::from_waker(&waker);
let ctx = &mut Context::from_waker(&Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/coroutine/async-gen-yield-ty-is-unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ async gen fn gen_fn() -> &'static str {

pub fn main() {
let async_iterator = pin!(gen_fn());
let waker = Waker::noop();
let ctx = &mut Context::from_waker(&waker);
let ctx = &mut Context::from_waker(&Waker::NOOP);
async_iterator.poll_next(ctx);
}
3 changes: 1 addition & 2 deletions tests/ui/coroutine/async_gen_fn_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let waker = Waker::noop();
let ctx = &mut Context::from_waker(&waker);
let ctx = &mut Context::from_waker(&Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/dyn-star/dispatch-on-pin-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fn main() {
let mut fut = pin!(async_main());

// Poll loop, just to test the future...
let waker = Waker::noop();
let ctx = &mut Context::from_waker(&waker);
let ctx = &mut Context::from_waker(&Waker::NOOP);

loop {
match fut.as_mut().poll(ctx) {
Expand Down

0 comments on commit e8e1fb1

Please sign in to comment.