-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace unstable Waker::noop()
with Waker::NOOP
.
#122924
Conversation
This comment has been minimized.
This comment has been minimized.
🤦 Didn't do the find and replace everywhere. I thought this was a suspiciously small diff… |
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)>.
The Miri subtree was changed cc @rust-lang/miri Some changes occurred in coverage tests. cc @Zalathar |
const WAKER: &Waker = &Waker { waker: RawWaker::NOOP }; | ||
WAKER | ||
} | ||
pub const NOOP: &'static Waker = &Waker { waker: RawWaker::NOOP }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already tried that initially and the compiler rejects it.
I prepared a change which replaced
Waker::noop()
withWaker::NOOP
. Unfortunately, it seems that this doesn't enable&NOOP
usage:let mut cx = &mut Context::from_waker(&Waker::NOOP); cx.waker().wake_by_ref();error[E0716]: temporary value dropped while borrowed --> src/wake.rs:404:44 | 404 | let mut cx = &mut Context::from_waker(&Waker::NOOP); | ^^^^^^^^^^^ - temporary value is freed at the end of this statement
If I understand correctly now, this is not possible because the
Waker
has a destructor. Therefore, since use via constant promotion is not possible, it seems wise to offer a&Waker
value, in which case making it a constant rather than aconst fn
doesn't provide any advantages (but still might be a choice of API aesthetics).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, true, if it has a destructor it cannot get promoted. Good point.
I'm strongly opposed to this. Constants are used when we actually care about the specific value in question (so you could e.g. |
My own interest in this matter is that I want a stable noop waker, and in service of that, I'm trying to put work into making it more usable ( I do appreciate your point that constants imply matchability/equality that is irrelevant here. |
r? libs-api |
@bors r+ |
Ah, right, it was a working group, not a team that gave the go-ahead. My bad. @bors r- |
Curious, do we happen to have a policy or guideline written up anywhere on this?
Is this true in this case? Could it not be useful to have a canonical |
That can't be achieved with a And for various reasons (most of them related to pattern matching), consts currently cannot point to statics. |
That seems like potentially a good reason to not make a |
Agreed (speaking personally). Regarding WG-async, nobody had particularly strong feelings about whether this should be a const or a const fn. We were persuaded by the consistency argument here. But if it's more consistent or otherwise advantageous to make this a |
Regardless of how the API for getting a noop waker turns out,
Based on discussion with T-libs-api, I am currently inclined to close this PR and propose no other API changes — there is no use case any of us know of that would be improved by making the noop waker available as a One possibility that was raised was stabilizing |
In that case we'll move forward on stabilizing |
As discussed on Zulip, across
std
, outside of argumentlessnew()
constructor functions, stable constant values are generally provided usingconst
items rather thanconst fn
s. Therefore, this change is more consistent API design.Further, WG-async writes:
This PR implements that decision.