-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 Option::unwrap_unchecked #48278
Comments
There is already an external package providing this function. https://crates.io/crates/unsafe_unwrap |
Ah good to know. I still think it could be useful in the stdlib, but it's definitely less of a concern with that crate. |
My compare of default unwrap() and unsafe_unwrap():
The bench output:
Code compare:
I tried to change code of unsafe_unwrap():
New bench output:
So, panic! macro slows down default unwrap() in ~2.5 times! |
…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.
…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.
Available in nightly now. For |
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 <ojeda@kernel.org>
Now that
NonNull
has been added andOption<NonNull<T>>
is preferred over*mut T
, if you have code that used to know that a*mut T
was non-null and dereferenced it, you now know that yourOption<NonNull<T>>
isSome
and can call.unwrap()
on it. However,.unwrap()
can incur runtime overhead that dereferencing*mut T
never would. Since a lot of performance-critical code is written using raw pointers (and now,NonNull
), this runtime overhead may be worth worrying about.I propose, for these cases, to introduce an unsafe
unwrap_unchecked
method toOption
that returns aT
by simply assuming that theOption
is currentlySome(T)
.The text was updated successfully, but these errors were encountered: