Skip to content

Commit

Permalink
Rollup merge of #119984 - kpreid:waker-noop, r=dtolnay
Browse files Browse the repository at this point in the history
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. It also avoids unnecessarily executing the dynamically dispatched drop function when the noop waker is dropped.

If an owned noop waker is desired, it can be created by cloning, but the reverse is harder to do since it requires declaring a constant. 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 reduction in boilerplate.

[Previous discussion on the tracking issue starting here](rust-lang/rust#98286 (comment))
  • Loading branch information
matthiaskrgr authored Jan 19, 2024
2 parents a181529 + c5b2274 commit 99f2f18
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 12 deletions.
3 changes: 1 addition & 2 deletions tests/pass/async-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ async fn uninhabited_variant() {
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
use std::task::{Context, Poll, Waker};

let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(Waker::noop());

let mut pinned = Box::pin(fut);
loop {
Expand Down
3 changes: 1 addition & 2 deletions tests/pass/dyn-star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ fn dispatch_on_pin_mut() {
let mut fut = async_main();

// Poll loop, just to test the future...
let waker = Waker::noop();
let ctx = &mut Context::from_waker(&waker);
let ctx = &mut Context::from_waker(Waker::noop());

loop {
match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } {
Expand Down
6 changes: 2 additions & 4 deletions tests/pass/future-self-referential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ impl Future for DoStuff {
}

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

let mut pinned = pin!(fut);
loop {
Expand All @@ -90,8 +89,7 @@ fn run_fut<T>(fut: impl Future<Output = T>) -> T {
}

fn self_referential_box() {
let waker = Waker::noop();
let cx = &mut Context::from_waker(&waker);
let cx = &mut Context::from_waker(Waker::noop());

async fn my_fut() -> i32 {
let val = 10;
Expand Down
3 changes: 1 addition & 2 deletions tests/pass/issues/issue-miri-2068.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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 waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(Waker::noop());
loop {
match fut.as_mut().poll(&mut context) {
Poll::Ready(v) => return v,
Expand Down
3 changes: 1 addition & 2 deletions tests/pass/move-data-across-await-point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ fn data_moved() {
fn run_fut<T>(fut: impl Future<Output = T>) -> T {
use std::task::{Context, Poll, Waker};

let waker = Waker::noop();
let mut context = Context::from_waker(&waker);
let mut context = Context::from_waker(Waker::noop());

let mut pinned = Box::pin(fut);
loop {
Expand Down

0 comments on commit 99f2f18

Please sign in to comment.