Skip to content

Commit 6f8a944

Browse files
committed
Change return type of unstable Waker::noop() from Waker to &Waker.
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)
1 parent 3deb9bb commit 6f8a944

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Diff for: library/core/src/task/wake.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,14 @@ impl Waker {
329329
Waker { waker }
330330
}
331331

332-
/// Creates a new `Waker` that does nothing when `wake` is called.
332+
/// Returns a reference to a `Waker` that does nothing when used.
333333
///
334334
/// This is mostly useful for writing tests that need a [`Context`] to poll
335335
/// some futures, but are not expecting those futures to wake the waker or
336336
/// do not need to do anything specific if it happens.
337337
///
338+
/// If an owned `Waker` is needed, `clone()` this one.
339+
///
338340
/// # Examples
339341
///
340342
/// ```
@@ -343,16 +345,20 @@ impl Waker {
343345
/// use std::future::Future;
344346
/// use std::task;
345347
///
346-
/// let waker = task::Waker::noop();
347-
/// let mut cx = task::Context::from_waker(&waker);
348+
/// let mut cx = task::Context::from_waker(task::Waker::noop());
348349
///
349350
/// let mut future = Box::pin(async { 10 });
350351
/// assert_eq!(future.as_mut().poll(&mut cx), task::Poll::Ready(10));
351352
/// ```
352353
#[inline]
353354
#[must_use]
354355
#[unstable(feature = "noop_waker", issue = "98286")]
355-
pub const fn noop() -> Waker {
356+
pub const fn noop() -> &'static Waker {
357+
// Ideally all this data would be explicitly `static` because it is used by reference and
358+
// only ever needs one copy. But `const fn`s (and `const` items) cannot refer to statics,
359+
// even though their values can be promoted to static. (That might change; see #119618.)
360+
// An alternative would be a `pub static NOOP: &Waker`, but associated static items are not
361+
// currently allowed either, and making it non-associated would be unergonomic.
356362
const VTABLE: RawWakerVTable = RawWakerVTable::new(
357363
// Cloning just returns a new no-op raw waker
358364
|_| RAW,
@@ -364,8 +370,9 @@ impl Waker {
364370
|_| {},
365371
);
366372
const RAW: RawWaker = RawWaker::new(ptr::null(), &VTABLE);
373+
const WAKER_REF: &Waker = &Waker { waker: RAW };
367374

368-
Waker { waker: RAW }
375+
WAKER_REF
369376
}
370377

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

0 commit comments

Comments
 (0)