-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Stabilize Option::expect_none(msg) and Option::unwrap_none() #73077
Conversation
Also update must_use message on is_none().
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Closes #62633 |
@rust-lang/libs: impl<T: Debug> Option<T> {
pub fn expect_none(self, msg: &str);
pub fn unwrap_none(self);
} The unwrap_none panic looks like: thread 'main' panicked at 'called `Option::unwrap_none()` on a `Some` value: 100', src/main.rs:9:5 The expect_none panic in the case of e.g. thread 'main' panicked at 'msg msg: 100', src/main.rs:9:5 That formatting matches what Result::expect and Result::expect_err do with the user's message i.e. The naming of unwrap_none isn't 100% ideal because None isn't exactly "a wrapped |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns: Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
One reasonable alternative we might want to consider is to make these both take I don't think it's better enough to make up for the inconsistency with other expect/unwrap methods, but it would make these usable in more cases. |
I think taking |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Registering the previous two comments for a bit more discussion. |
I'm okay with either way. In the worst case if we go with One place the difference would be observable would be when not using method syntax, such as If we change to |
I think in this case it'd make sense to let it bake another release if we change the signature since we aren't sure which is the better option. |
This is an example of the kind of code that would require an extra as_ref() if the methods took pub struct Service {
field: Option<...>,
}
impl Service {
/// Precondition: xyz has not been frobbed yet.
pub fn f(&self) {
self.field.expect_none("...");
...
}
} |
I prefer the API-symmetry of keeping |
I think being consistent with all the other similar methods is fairly important. If somebody wants to write something (macro? trait?) that works with all the I see |
Are there any practical examples where this would affect macros/traits? All What I am thinking is that if |
How about |
I have a general concern about the lack of counterpressure on adding new methods to the core standard library types. As these APIs get more and more methods, they become harder and hard to figure out how to use for new users, because the std API docs become more and more overwhelming. Every method has some utility, and I'm worried we don't have any mechanism to judge out whether that utility is worthwhile for the downside of adding more methods (in essence, I think we currently greatly undervalue the cost of adding methods). That said, I read the internals thread and I saw that there was a lot of support for this functionality from many community members, mainly because they could add it to the end of a method chain. But I'm not in love with the fact that we're adding two methods to do what today can be done in 1 pattern without imports. And I'm not compelled that there's a lot of symmetry with unwrap/expect. So I think I would prefer to add just one method, That seems like the most parsiminous way to solve the user complaint with the |
I think the advantage of being "symmetric" to Another cause that this method was requested to be added was the convenience of method call chains, but workarounds like this (instead of using |
☔ The latest upstream changes (presumably #74235) made this pull request unmergeable. Please resolve the merge conflicts. |
Since there is major pushback and it likely won't get resolved, should I close this? |
Also update must_use message on is_none().