Skip to content

Commit

Permalink
Replace unstable Waker::noop() with Waker::NOOP.
Browse files Browse the repository at this point in the history
As discussed in
<https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Associated.20constants.20vs.2E.20functions.20in.20std.20API>,
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
<rust-lang#98286 (comment)>.
  • Loading branch information
kpreid committed Mar 23, 2024
1 parent 85e449a commit 25c9f50
Show file tree
Hide file tree
Showing 27 changed files with 32 additions and 37 deletions.
2 changes: 1 addition & 1 deletion library/alloc/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker {
/// // cast the Rc<Task> 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();
///
Expand Down
15 changes: 5 additions & 10 deletions library/core/src/task/wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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();
///
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/async_iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/async-closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = T>) -> 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/async-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async fn uninhabited_variant() {
fn run_fut<T>(fut: impl Future<Output = T>) -> 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/dyn-star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) } {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/miri/tests/pass/future-self-referential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Future for DoStuff {
}

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

let mut pinned = pin!(fut);
loop {
Expand All @@ -89,7 +89,7 @@ fn run_fut<T>(fut: impl Future<Output = T>) -> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/issues/issue-miri-2068.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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 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,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/move-data-across-await-point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn data_moved() {
fn run_fut<T>(fut: impl Future<Output = T>) -> 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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async2.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +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 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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async_block.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +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 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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/async_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/closure_macro_async.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
LL| | #[coverage(off)]
LL| | pub fn block_on<F: Future>(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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/closure_macro_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod executor {
#[coverage(off)]
pub fn block_on<F: Future>(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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/async_closure_shims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = T>) -> 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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-closures/auxiliary/block-on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::task::*;
pub fn block_on<T>(fut: impl Future<Output = T>) -> 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) } {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-fn/auxiliary/block-on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::task::*;
pub fn block_on<T>(fut: impl Future<Output = T>) -> 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) } {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/for-await-passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
2 changes: 1 addition & 1 deletion tests/ui/async-await/for-await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coroutine/async-gen-yield-ty-is-unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion tests/ui/coroutine/async_gen_fn_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/dyn-star/dispatch-on-pin-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 25c9f50

Please sign in to comment.