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 15, 2024
1 parent 3deb9bb commit 7df054b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 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,15 @@ 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 {
const VTABLE: RawWakerVTable = RawWakerVTable::new(
// Cloning just returns a new no-op raw waker
|_| RAW,
Expand All @@ -364,8 +365,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 7df054b

Please sign in to comment.