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 5ddcf33
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 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

0 comments on commit 5ddcf33

Please sign in to comment.