-
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
Tracking Issue for option_result_unwrap_unchecked
#81383
Comments
Also: start dogfooding this in |
Now that rust-lang#81383 is available, start using it. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Now that rust-lang#81383 is available, start using it. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
@camelid Indeed! I just submitted the first two PRs for that. |
There are a couple more that can be replaced in I have filled https://bugs.llvm.org/show_bug.cgi?id=48975 with the IR reduction and an analysis of why manually integrating the function (e.g. via a macro) helps sometimes. This may also help other |
…=scottmcm btree: use Option's unwrap_unchecked() Now that rust-lang#81383 is available, start using it.
Is there a way to express that this is a "safe" operation when we know that the |
That's an interesting idea, although I am not sure what the policy is on having differing function qualifiers for the same method name, @m-ou-se? I guess that could ideally be achieved with the future specialization feature (if it supports inherent impls). We can also (ab)use the priority of inherent methods over traits, but it feels a bit dirty: use std::convert::Infallible;
pub enum MyResult<T, E> {
Ok(T),
Err(E),
}
trait UnsafeUnwrapUnchecked<T> {
unsafe fn unwrap_unchecked(self) -> T;
}
impl<T, E> UnsafeUnwrapUnchecked<T> for MyResult<T, E> {
unsafe fn unwrap_unchecked(self) -> T {
match self {
MyResult::Ok(t) => t,
MyResult::Err(_) => core::hint::unreachable_unchecked(),
}
}
}
impl<T> MyResult<T, Infallible> {
fn unwrap_unchecked(self) -> T {
match self {
MyResult::Ok(t) => t,
MyResult::Err(_) => unsafe { core::hint::unreachable_unchecked() },
}
}
}
pub fn f() {
let _ = MyResult::<i32, Infallible>::Ok(42).unwrap_unchecked();
let _ = unsafe { MyResult::<i32, i32>::Ok(42).unwrap_unchecked() };
} |
There already is into_ok() and into_err() that do this, there is no need to overload unwrap_unchecked. |
Yeah, that is what I imagined, thanks! |
Personally I try to "label" unwraps by instead using |
That could be a bit misleading since the message will not be used. On the other hand, we could use it in debug builds for asserting. |
Yes, in almost all cases the message would never be printed. Note that I believe it requires Personally I would still find this useful. My flow is to start with |
Yeah. Some projects do build |
If |
I feel that |
I agree that the panic in debug mode should be documented, that seems very useful to know. |
Hi, I feel these methods are pretty helpful and am looking forward to using them in my lib. Is there a plan to stabilize now? |
One thing I've been wondering about is the slight inconsistency between the name |
Note that there are stable methods called So I would say the |
For this question, I think maybe just leave it an "undefined behavior" makes sense, which is what has been documented.
This sounds good to me. |
Yeah, we could always decide which behavior is best, if any, later. Possibly after the discussion around I will write a stabilization report. |
Stabilization reportSummaryUnsafe versions of let x = Some("air");
assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); Tests
Other relevant information
Resolutions of unresolved questions
|
Also, if it were needed for some reason, users could just write their own |
@rfcbot fcp merge |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. 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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
…olnay Stabilize `option_result_unwrap_unchecked` Closes rust-lang#81383. Stabilization report: rust-lang#81383 (comment). `@rustbot` label +A-option-result +T-libs-api
…olnay Stabilize `option_result_unwrap_unchecked` Closes rust-lang#81383. Stabilization report: rust-lang#81383 (comment). ``@rustbot`` label +A-option-result +T-libs-api
…olnay Stabilize `option_result_unwrap_unchecked` Closes rust-lang#81383. Stabilization report: rust-lang#81383 (comment). ```@rustbot``` label +A-option-result +T-libs-api
They were stabilized together with `Option::unwrap_unchecked` in rust-lang#81383. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Release notes: add `Result::unwrap_{,err_}unchecked` They were stabilized together with `Option::unwrap_unchecked` in rust-lang#81383. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Now that rust-lang/rust#81383 is available, start using it. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
btree: use Option's unwrap_unchecked() Now that rust-lang/rust#81383 is available, start using it.
Feature gate:
#![feature(option_result_unwrap_unchecked)]
This is a tracking issue for the unchecked variants of
unwrap()
andunwrap_err()
ofOption
andResult
.These are unsafe functions intended to be used in hot paths of performance sensitive applications where the variant an
Option
/Result
holds is known, allowing to skip the branch that the safe version introduces. They complement other*_unchecked()
methods already available, such asget_unchecked()
in slices.Public API
Steps / History
unwrap_unchecked()
methods forOption
andResult
#80876option_result_unwrap_unchecked
#81383 (comment)option_result_unwrap_unchecked
#81383 (comment)option_result_unwrap_unchecked
#89951Unresolved Questions
Should we add
expect_unchecked
, and if yes, do it within another feature flag, to unblockunwrap_unchecked
? Tracking Issue foroption_result_unwrap_unchecked
#81383 (comment)Should we panic on failure in debug (i.e. for those projects that build
core
themselves)? If yes, that would mean there is no way to actually remove the check for those projects, which could be potentially needed by someone. If we do, should it be documented? Tracking Issue foroption_result_unwrap_unchecked
#81383 (comment)debug_assert!
, which may be useful for projects that buildcore
with debug assertions enabled (e.g. the bors test suite or some embedded projects), but this is not documented, and indeed most users will not see such behavior, sincecore
is shipped in release mode.Should we name them
unchecked_unwrap
instead? Tracking Issue foroption_result_unwrap_unchecked
#81383 (comment).unwrap_unchecked
) follows the convention of other stable methods in the standard library such asget_unchecked
,map_unchecked
,new_unchecked
,unreachable_unchecked
... (see as well https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter).unchecked_{add,...}
methods seem to follow the opposite convention, but those are unstable and they likely do it because the stable{checked,...}_{add,...}
exist.The text was updated successfully, but these errors were encountered: