From 25c9f50c29bb00760705ea5cd150375c063c010f Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Fri, 22 Mar 2024 20:36:00 -0700 Subject: [PATCH] Replace unstable `Waker::noop()` with `Waker::NOOP`. As discussed in , across `std`, outside of argumentless `new()` constructor functions, stable constant values are generally provided using `const` items rather than `const fn`s. Therefore, this change is more consistent API design. WG-async approves of making this change, per . --- library/alloc/src/task.rs | 2 +- library/core/src/task/wake.rs | 15 +++++---------- library/core/tests/async_iter/mod.rs | 2 +- src/tools/miri/tests/pass/async-closure.rs | 2 +- src/tools/miri/tests/pass/async-fn.rs | 2 +- src/tools/miri/tests/pass/dyn-star.rs | 2 +- .../miri/tests/pass/future-self-referential.rs | 4 ++-- .../miri/tests/pass/issues/issue-miri-2068.rs | 2 +- .../tests/pass/move-data-across-await-point.rs | 2 +- tests/coverage/async.coverage | 2 +- tests/coverage/async.rs | 2 +- tests/coverage/async2.coverage | 2 +- tests/coverage/async2.rs | 2 +- tests/coverage/async_block.coverage | 2 +- tests/coverage/async_block.rs | 2 +- tests/coverage/closure_macro_async.coverage | 2 +- tests/coverage/closure_macro_async.rs | 2 +- tests/mir-opt/async_closure_shims.rs | 2 +- .../async-closures/auxiliary/block-on.rs | 2 +- .../ui/async-await/async-fn/auxiliary/block-on.rs | 2 +- tests/ui/async-await/for-await-passthrough.rs | 2 +- tests/ui/async-await/for-await.rs | 2 +- .../in-trait/async-default-fn-overridden.rs | 2 +- .../dont-project-to-specializable-projection.rs | 2 +- tests/ui/coroutine/async-gen-yield-ty-is-unit.rs | 2 +- tests/ui/coroutine/async_gen_fn_iter.rs | 2 +- tests/ui/dyn-star/dispatch-on-pin-mut.rs | 2 +- 27 files changed, 32 insertions(+), 37 deletions(-) diff --git a/library/alloc/src/task.rs b/library/alloc/src/task.rs index b40768a52b6b4..c833deb9dd549 100644 --- a/library/alloc/src/task.rs +++ b/library/alloc/src/task.rs @@ -247,7 +247,7 @@ fn raw_waker(waker: Arc) -> RawWaker { /// // cast the Rc into a `LocalWaker` /// let local_waker: LocalWaker = task.clone().into(); /// // Build the context using `ContextBuilder` -/// let mut cx = ContextBuilder::from_waker(Waker::noop()) +/// let mut cx = ContextBuilder::from_waker(Waker::NOOP) /// .local_waker(&local_waker) /// .build(); /// diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 1b43c46bda515..034656c6006e1 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -284,7 +284,7 @@ impl fmt::Debug for Context<'_> { /// use std::future::Future; /// /// let local_waker = LocalWaker::noop(); -/// let waker = Waker::noop(); +/// let waker = Waker::NOOP; /// /// let mut cx = ContextBuilder::from_waker(&waker) /// .local_waker(&local_waker) @@ -465,7 +465,7 @@ impl Waker { Waker { waker } } - /// Returns a reference to a `Waker` that does nothing when used. + /// A reference to a `Waker` that does nothing when used. /// /// 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 @@ -481,18 +481,13 @@ impl Waker { /// use std::future::Future; /// use std::task; /// - /// let mut cx = task::Context::from_waker(task::Waker::noop()); + /// 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() -> &'static Waker { - const WAKER: &Waker = &Waker { waker: RawWaker::NOOP }; - WAKER - } + pub const NOOP: &'static Waker = &Waker { waker: RawWaker::NOOP }; /// Get a reference to the underlying [`RawWaker`]. #[inline] @@ -697,7 +692,7 @@ impl LocalWaker { /// use std::future::Future; /// use std::task::{ContextBuilder, LocalWaker, Waker, Poll}; /// - /// let mut cx = ContextBuilder::from_waker(Waker::noop()) + /// let mut cx = ContextBuilder::from_waker(Waker::NOOP) /// .local_waker(LocalWaker::noop()) /// .build(); /// diff --git a/library/core/tests/async_iter/mod.rs b/library/core/tests/async_iter/mod.rs index 4f425d7286d09..397dd89ec7d49 100644 --- a/library/core/tests/async_iter/mod.rs +++ b/library/core/tests/async_iter/mod.rs @@ -7,7 +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 mut cx = &mut core::task::Context::from_waker(core::task::Waker::noop()); + 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))); diff --git a/src/tools/miri/tests/pass/async-closure.rs b/src/tools/miri/tests/pass/async-closure.rs index 9b2fc2948bf45..9d594e044aebe 100644 --- a/src/tools/miri/tests/pass/async-closure.rs +++ b/src/tools/miri/tests/pass/async-closure.rs @@ -6,7 +6,7 @@ use std::task::*; pub fn block_on(fut: impl Future) -> T { let mut fut = pin!(fut); - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match fut.as_mut().poll(ctx) { diff --git a/src/tools/miri/tests/pass/async-fn.rs b/src/tools/miri/tests/pass/async-fn.rs index 13400c88c7112..c9b3dbe5a99fc 100644 --- a/src/tools/miri/tests/pass/async-fn.rs +++ b/src/tools/miri/tests/pass/async-fn.rs @@ -76,7 +76,7 @@ async fn uninhabited_variant() { fn run_fut(fut: impl Future) -> T { use std::task::{Context, Poll, Waker}; - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); let mut pinned = Box::pin(fut); loop { diff --git a/src/tools/miri/tests/pass/dyn-star.rs b/src/tools/miri/tests/pass/dyn-star.rs index dab589b465181..f7ebbee8673a8 100644 --- a/src/tools/miri/tests/pass/dyn-star.rs +++ b/src/tools/miri/tests/pass/dyn-star.rs @@ -93,7 +93,7 @@ fn dispatch_on_pin_mut() { let mut fut = async_main(); // Poll loop, just to test the future... - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } { diff --git a/src/tools/miri/tests/pass/future-self-referential.rs b/src/tools/miri/tests/pass/future-self-referential.rs index 8aeb26a7a957b..11fe94d717356 100644 --- a/src/tools/miri/tests/pass/future-self-referential.rs +++ b/src/tools/miri/tests/pass/future-self-referential.rs @@ -77,7 +77,7 @@ impl Future for DoStuff { } fn run_fut(fut: impl Future) -> T { - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); let mut pinned = pin!(fut); loop { @@ -89,7 +89,7 @@ fn run_fut(fut: impl Future) -> T { } fn self_referential_box() { - let cx = &mut Context::from_waker(Waker::noop()); + let cx = &mut Context::from_waker(Waker::NOOP); async fn my_fut() -> i32 { let val = 10; diff --git a/src/tools/miri/tests/pass/issues/issue-miri-2068.rs b/src/tools/miri/tests/pass/issues/issue-miri-2068.rs index ccee2221e2958..59e581f5cc552 100644 --- a/src/tools/miri/tests/pass/issues/issue-miri-2068.rs +++ b/src/tools/miri/tests/pass/issues/issue-miri-2068.rs @@ -6,7 +6,7 @@ use std::task::{Context, Poll, Waker}; pub fn fuzzing_block_on>(fut: F) -> O { let mut fut = std::pin::pin!(fut); - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); loop { match fut.as_mut().poll(&mut context) { Poll::Ready(v) => return v, diff --git a/src/tools/miri/tests/pass/move-data-across-await-point.rs b/src/tools/miri/tests/pass/move-data-across-await-point.rs index 1a93a6bf66497..6790aa5463740 100644 --- a/src/tools/miri/tests/pass/move-data-across-await-point.rs +++ b/src/tools/miri/tests/pass/move-data-across-await-point.rs @@ -56,7 +56,7 @@ fn data_moved() { fn run_fut(fut: impl Future) -> T { use std::task::{Context, Poll, Waker}; - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); let mut pinned = Box::pin(fut); loop { diff --git a/tests/coverage/async.coverage b/tests/coverage/async.coverage index e943911d31040..b18202bf386ac 100644 --- a/tests/coverage/async.coverage +++ b/tests/coverage/async.coverage @@ -119,7 +119,7 @@ LL| | #[coverage(off)] LL| | pub fn block_on(mut future: F) -> F::Output { LL| | let mut future = pin!(future); - LL| | let mut context = Context::from_waker(Waker::noop()); + LL| | let mut context = Context::from_waker(Waker::NOOP); LL| | LL| | loop { LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/coverage/async.rs b/tests/coverage/async.rs index 5018ade0125f3..2fb55c4485984 100644 --- a/tests/coverage/async.rs +++ b/tests/coverage/async.rs @@ -112,7 +112,7 @@ mod executor { #[coverage(off)] pub fn block_on(mut future: F) -> F::Output { let mut future = pin!(future); - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); loop { if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/coverage/async2.coverage b/tests/coverage/async2.coverage index bd5b701491b5f..40f1dffa7cb6a 100644 --- a/tests/coverage/async2.coverage +++ b/tests/coverage/async2.coverage @@ -41,7 +41,7 @@ LL| | #[coverage(off)] LL| | pub fn block_on(mut future: F) -> F::Output { LL| | let mut future = pin!(future); - LL| | let mut context = Context::from_waker(Waker::noop()); + LL| | let mut context = Context::from_waker(Waker::NOOP); LL| | LL| | loop { LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/coverage/async2.rs b/tests/coverage/async2.rs index 713c70d277a76..6e4cd68c08314 100644 --- a/tests/coverage/async2.rs +++ b/tests/coverage/async2.rs @@ -39,7 +39,7 @@ mod executor { #[coverage(off)] pub fn block_on(mut future: F) -> F::Output { let mut future = pin!(future); - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); loop { if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/coverage/async_block.coverage b/tests/coverage/async_block.coverage index 7fd17bc51c3e2..dd3aa39c3e1e2 100644 --- a/tests/coverage/async_block.coverage +++ b/tests/coverage/async_block.coverage @@ -24,7 +24,7 @@ LL| | #[coverage(off)] LL| | pub fn block_on(mut future: F) -> F::Output { LL| | let mut future = pin!(future); - LL| | let mut context = Context::from_waker(Waker::noop()); + LL| | let mut context = Context::from_waker(Waker::NOOP); LL| | LL| | loop { LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/coverage/async_block.rs b/tests/coverage/async_block.rs index a70dd74703270..85e746a87d448 100644 --- a/tests/coverage/async_block.rs +++ b/tests/coverage/async_block.rs @@ -23,7 +23,7 @@ mod executor { #[coverage(off)] pub fn block_on(mut future: F) -> F::Output { let mut future = pin!(future); - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); loop { if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/coverage/closure_macro_async.coverage b/tests/coverage/closure_macro_async.coverage index 0557ce47d6873..410f9bd73e9b7 100644 --- a/tests/coverage/closure_macro_async.coverage +++ b/tests/coverage/closure_macro_async.coverage @@ -55,7 +55,7 @@ LL| | #[coverage(off)] LL| | pub fn block_on(mut future: F) -> F::Output { LL| | let mut future = pin!(future); - LL| | let mut context = Context::from_waker(Waker::noop()); + LL| | let mut context = Context::from_waker(Waker::NOOP); LL| | LL| | loop { LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/coverage/closure_macro_async.rs b/tests/coverage/closure_macro_async.rs index 735214629b6f2..9720ac772ed39 100644 --- a/tests/coverage/closure_macro_async.rs +++ b/tests/coverage/closure_macro_async.rs @@ -54,7 +54,7 @@ mod executor { #[coverage(off)] pub fn block_on(mut future: F) -> F::Output { let mut future = pin!(future); - let mut context = Context::from_waker(Waker::noop()); + let mut context = Context::from_waker(Waker::NOOP); loop { if let Poll::Ready(val) = future.as_mut().poll(&mut context) { diff --git a/tests/mir-opt/async_closure_shims.rs b/tests/mir-opt/async_closure_shims.rs index 7d226df686654..1cebed5885b1b 100644 --- a/tests/mir-opt/async_closure_shims.rs +++ b/tests/mir-opt/async_closure_shims.rs @@ -11,7 +11,7 @@ use std::task::*; pub fn block_on(fut: impl Future) -> T { let mut fut = pin!(fut); - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match fut.as_mut().poll(ctx) { diff --git a/tests/ui/async-await/async-closures/auxiliary/block-on.rs b/tests/ui/async-await/async-closures/auxiliary/block-on.rs index dcb710fc97c97..cfa0c94f0ca42 100644 --- a/tests/ui/async-await/async-closures/auxiliary/block-on.rs +++ b/tests/ui/async-await/async-closures/auxiliary/block-on.rs @@ -9,7 +9,7 @@ use std::task::*; pub fn block_on(fut: impl Future) -> T { let mut fut = pin!(fut); // Poll loop, just to test the future... - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match unsafe { fut.as_mut().poll(ctx) } { diff --git a/tests/ui/async-await/async-fn/auxiliary/block-on.rs b/tests/ui/async-await/async-fn/auxiliary/block-on.rs index dcb710fc97c97..cfa0c94f0ca42 100644 --- a/tests/ui/async-await/async-fn/auxiliary/block-on.rs +++ b/tests/ui/async-await/async-fn/auxiliary/block-on.rs @@ -9,7 +9,7 @@ use std::task::*; pub fn block_on(fut: impl Future) -> T { let mut fut = pin!(fut); // Poll loop, just to test the future... - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match unsafe { fut.as_mut().poll(ctx) } { diff --git a/tests/ui/async-await/for-await-passthrough.rs b/tests/ui/async-await/for-await-passthrough.rs index e09e843332eed..8f63bb2abf210 100644 --- a/tests/ui/async-await/for-await-passthrough.rs +++ b/tests/ui/async-await/for-await-passthrough.rs @@ -23,7 +23,7 @@ async fn real_main() { fn main() { let future = real_main(); - let mut cx = &mut core::task::Context::from_waker(std::task::Waker::noop()); + 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) {} } diff --git a/tests/ui/async-await/for-await.rs b/tests/ui/async-await/for-await.rs index acd9f01640a1a..f0d3dd46be4f1 100644 --- a/tests/ui/async-await/for-await.rs +++ b/tests/ui/async-await/for-await.rs @@ -17,7 +17,7 @@ async fn real_main() { fn main() { let future = real_main(); - let mut cx = &mut core::task::Context::from_waker(std::task::Waker::noop()); + 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) {} } diff --git a/tests/ui/async-await/in-trait/async-default-fn-overridden.rs b/tests/ui/async-await/in-trait/async-default-fn-overridden.rs index 9b0ce8663c203..4b05221c9eb19 100644 --- a/tests/ui/async-await/in-trait/async-default-fn-overridden.rs +++ b/tests/ui/async-await/in-trait/async-default-fn-overridden.rs @@ -40,7 +40,7 @@ fn main() { let mut fut = pin!(async_main()); // Poll loop, just to test the future... - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match fut.as_mut().poll(ctx) { diff --git a/tests/ui/async-await/in-trait/dont-project-to-specializable-projection.rs b/tests/ui/async-await/in-trait/dont-project-to-specializable-projection.rs index e0901dc6886e4..8750ae46e9721 100644 --- a/tests/ui/async-await/in-trait/dont-project-to-specializable-projection.rs +++ b/tests/ui/async-await/in-trait/dont-project-to-specializable-projection.rs @@ -43,7 +43,7 @@ fn main() { let mut fut = pin!(async_main()); // Poll loop, just to test the future... - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match fut.as_mut().poll(ctx) { diff --git a/tests/ui/coroutine/async-gen-yield-ty-is-unit.rs b/tests/ui/coroutine/async-gen-yield-ty-is-unit.rs index 62b9bafcd60aa..7205c7cd69230 100644 --- a/tests/ui/coroutine/async-gen-yield-ty-is-unit.rs +++ b/tests/ui/coroutine/async-gen-yield-ty-is-unit.rs @@ -11,6 +11,6 @@ async gen fn gen_fn() -> &'static str { pub fn main() { let async_iterator = pin!(gen_fn()); - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); async_iterator.poll_next(ctx); } diff --git a/tests/ui/coroutine/async_gen_fn_iter.rs b/tests/ui/coroutine/async_gen_fn_iter.rs index 42288712c7068..5e92a55720710 100644 --- a/tests/ui/coroutine/async_gen_fn_iter.rs +++ b/tests/ui/coroutine/async_gen_fn_iter.rs @@ -73,7 +73,7 @@ fn main() { let mut fut = pin!(async_main()); // Poll loop, just to test the future... - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match fut.as_mut().poll(ctx) { diff --git a/tests/ui/dyn-star/dispatch-on-pin-mut.rs b/tests/ui/dyn-star/dispatch-on-pin-mut.rs index e17aef4763424..623c95c4cc641 100644 --- a/tests/ui/dyn-star/dispatch-on-pin-mut.rs +++ b/tests/ui/dyn-star/dispatch-on-pin-mut.rs @@ -26,7 +26,7 @@ fn main() { let mut fut = pin!(async_main()); // Poll loop, just to test the future... - let ctx = &mut Context::from_waker(Waker::noop()); + let ctx = &mut Context::from_waker(Waker::NOOP); loop { match fut.as_mut().poll(ctx) {