-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add unwrap_unchecked()
methods for Option
and Result
#80876
Conversation
In particular: - `unwrap_unchecked()` for `Option`. - `unwrap_unchecked()` and `unwrap_err_unchecked()` for `Result`. These complement other `*_unchecked()` methods in `core` etc. Currently there are a couple of places it may be used inside rustc (`LinkedList`, `BTree`). It is also easy to find other repositories with similar functionality. Fixes rust-lang#48278. Signed-off-by: Miguel Ojeda <[email protected]>
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (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. |
A few extra bits:
|
The job Click to see the possible cause of the failure (guessed by this bot)
|
Signed-off-by: Miguel Ojeda <[email protected]>
An RFC is generally not required for individual inherent method additions, unless they'd be setting a new pattern. Usually a method going in unstable like this just needs a code review and a "seems plausible" from a libs member. (Only stabilization requires the whole-team & FCP.) I can't predict whether this is desired. It seems plausible, but one could also argue that |
Yeah, I wasn't sure -- thanks for confirming & the explanation!
Indeed, I can see both angles. I think the major argument in favor is that even ourselves are writing things like: rust/library/alloc/src/collections/btree/mod.rs Lines 26 to 36 in d03fe84
|
Yeah, I don't doubt that its semantics are handy. Unfortunately a big downside is that Hopefully one day that'll no longer be the case and these will be more useful again... |
I feel that we should not add these, as they would encourage what is almost always an antipattern. The cases in which the UB actually improves performance in a manner that meaningfully affect the program overall are few, and it is not difficult to write this manually if necessary. r? @m-ou-se for libs team member review |
There are a few counter-points to that I can think of:
I understand it is not a very common use case and the desire to steer newcomers to the most commonly used methods, but not providing useful methods just for that reason is not tackling the right problem. If one wants to avoid newcomers using certain methods, one should add a feature to put them in an "expert" section in the documentation, or something like that (e.g. in the same spirit of Furthermore, this is an unsafe method, which means it is already being flagged as "you should know what you are doing". That is to say: it is not like adding a particularly unsuited (e.g. slow), but ultimately safe operation to a data structure that users should avoid unless they know what they are doing. In any case, like everything else (specially unsafe bits), it should be used judiciously. Having the method or not will not stop anyone determined enough to "improve" performance. In the end, Rust is a systems programming language, which means people is more likely to need this kind of functionality compared to other languages. |
I think it makes sense to have these. Of course we'd want to steer people away from using the unchecked functions, but that's what Let's try out the unstable version to get some experience with this function. Then we can have a more informed discussion about whether we want to stabilize this later. @ojeda Can you open a tracking issue for this? |
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.
Let's link the reference about undefined behaviour:
Suggested-by: Mara Bos <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
Signed-off-by: Miguel Ojeda <[email protected]>
Thanks @m-ou-se, done! |
Thanks! @bors r+ rollup |
📌 Commit 01250fc has been approved by |
🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened. |
…d, r=m-ou-se Add `unwrap_unchecked()` methods for `Option` and `Result` In particular: - `unwrap_unchecked()` for `Option`. - `unwrap_unchecked()` and `unwrap_err_unchecked()` for `Result`. These complement other `*_unchecked()` methods in `core` etc. Currently there are a couple of places it may be used inside rustc (`LinkedList`, `BTree`). It is also easy to find other repositories with similar functionality. Fixes rust-lang#48278.
Rollup of 14 pull requests Successful merges: - rust-lang#80812 (Update RELEASES.md for 1.50.0) - rust-lang#80876 (Add `unwrap_unchecked()` methods for `Option` and `Result`) - rust-lang#80900 (Fix ICE with `ReadPointerAsBytes` validation error) - rust-lang#81191 (BTreeMap: test all borrowing interfaces and test more chaotic order behavior) - rust-lang#81195 (Account for generics when suggesting bound) - rust-lang#81299 (Fix some bugs reported by eslint) - rust-lang#81325 (typeck: Don't suggest converting LHS exprs) - rust-lang#81353 (Fix spelling in documentation for error E0207) - rust-lang#81369 (rustc_codegen_ssa: use wall time for codegen_to_LLVM_IR time-passes entry) - rust-lang#81389 (rustdoc: Document CommonMark extensions.) - rust-lang#81399 (Update books) - rust-lang#81401 (tidy: Some code cleanup.) - rust-lang#81407 (Refine "remove semicolon" suggestion in trait selection) - rust-lang#81412 (Fix assertion in `MaybeUninit::array_assume_init()` for zero-length arrays) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
The `unwrap_unchecked` methods were recently added to the standard library [1] and are yet to be stabilized. [1]: rust-lang/rust#80876
In particular:
unwrap_unchecked()
forOption
.unwrap_unchecked()
andunwrap_err_unchecked()
forResult
.These complement other
*_unchecked()
methods incore
etc.Currently there are a couple of places it may be used inside rustc (
LinkedList
,BTree
). It is also easy to find other repositories with similar functionality.Fixes #48278.