Skip to content

Commit

Permalink
Change return type of unstable Waker::noop() from Waker to &Waker.
Browse files Browse the repository at this point in the history
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.

If an owned noop waker is desired, it can be created by cloning, but the
reverse is harder. 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 benefit.

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

/// Creates a new `Waker` that does nothing when `wake` is called.
/// Returns 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
/// do not need to do anything specific if it happens.
///
/// If an owned `Waker` is needed, `clone()` this one.
///
/// # Examples
///
/// ```
Expand All @@ -343,16 +345,20 @@ 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 fn noop() -> &'static Waker {
// Ideally all this data would be explicitly `static` because it is used by reference and
// only ever needs one copy. But `const fn`s (and `const` items) cannot refer to statics,
// even though their values can be promoted to static. (That might change; see #119618.)
// An alternative would be a `pub static NOOP: &Waker`, but associated static items are not
// currently allowed either, and making it non-associated would be unergonomic.
const VTABLE: RawWakerVTable = RawWakerVTable::new(
// Cloning just returns a new no-op raw waker
|_| RAW,
Expand All @@ -364,8 +370,9 @@ impl Waker {
|_| {},
);
const RAW: RawWaker = RawWaker::new(ptr::null(), &VTABLE);
const WAKER_REF: &Waker = &Waker { waker: RAW };

Waker { waker: RAW }
WAKER_REF
}

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

0 comments on commit 6f8a944

Please sign in to comment.