-
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
Add Result::{ok, err, and, or, unwrap_or} as const #92385
Conversation
r? @scottmcm (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
Aaaaaand now I find out why these are not marked as const. Okay. |
library/core/src/result.rs
Outdated
@@ -643,7 +643,8 @@ impl<T, E> Result<T, E> { | |||
/// ``` | |||
#[inline] | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
pub fn ok(self) -> Option<T> { | |||
#[rustc_const_unstable(feature = "const_result_option", issue = "92384")] | |||
pub const fn ok(self) -> Option<T> { |
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'm not too familiar with the feature, but would adding a ~const Drop
bound work here?
pub const fn ok(self) -> Option<T> { | |
pub const fn ok(self) -> Option<T> | |
where | |
E: ~const Drop, | |
{ |
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.
See also #91928, which constified most of Option
's methods.
You're right; there is a way to make this work. Reopening. |
This comment has been minimized.
This comment has been minimized.
Okay, so, a couple things.
In general there should probably be just a tracking issue in general for what the semantics of |
This comment has been minimized.
This comment has been minimized.
Have you tried writing the start of the pub const fn ok(self) -> Option<T>
where
E: ~const Drop,
{
match self {
Ok(x) => Some(x),
Err(_x) => None,
}
}
pub const fn err(self) -> Option<E>
where
T: ~const Drop,
{
match self {
Ok(_x) => None,
Err(x) => Some(x),
}
} I kind of feel like that might make it work without the |
Yeah, I was on to something here. The following works exactly as written, for example: #![feature(const_fn_trait_bound, const_precise_live_drops, const_trait_impl)]
pub const fn ok<T, E>(res: Result<T, E>) -> Option<T>
where
E: ~const Drop,
{
match res {
Ok(x) => Some(x),
// Actually works without the leading `_` also,
// but then you get an "unused variable" lint.
Err(_x) => None,
}
}
pub const fn err<T, E>(res: Result<T, E>) -> Option<E>
where
T: ~const Drop,
{
match res {
// Actually works without the leading `_` also,
// but then you get an "unused variable" lint.
Ok(_x) => None,
Err(x) => Some(x),
}
}
const A: Result<u32, &str> = Ok(2);
const B: Result<u32, &str> = Err("Nothing here");
const C: Option<u32> = ok(A);
const D: Option<u32> = ok(B);
const E: Option<&str> = err(A);
const F: Option<&str> = err(B);
fn main() {
assert_eq!(C, Some(2));
assert_eq!(D, None);
assert_eq!(E, None);
assert_eq!(F, Some("Nothing here"));
} |
I'm going to re-roll this to someone who will hopefully have more r? rust-lang/libs |
f503949
to
15433ce
Compare
Since this hasn't been CRed yet and I found that there were a few other methods like these, I decided to add them to the PR with the appropriate cc @joshtriplett since you seem to have won the review lottery. |
15433ce
to
f7627d0
Compare
This comment has been minimized.
This comment has been minimized.
f7627d0
to
19645ac
Compare
📌 Commit 19645ac has been approved by |
Add Result::{ok, err, and, or, unwrap_or} as const Already opened tracking issue rust-lang#92384. I don't think that this should actually cause any issues as long as the constness is unstable, but we may want to double-check that this doesn't get interpreted as a weird `Drop` bound even for non-const usages.
Now that that's merged, I think this is ready to merge again? Let me know if there are any changes in the code and I can fix them, @fee1-dead.
|
cc @oli-obk for potential blockers (otherwise r=me) |
@bors r=fee1-dead |
📌 Commit 19645ac has been approved by |
Add Result::{ok, err, and, or, unwrap_or} as const Already opened tracking issue rust-lang#92384. I don't think that this should actually cause any issues as long as the constness is unstable, but we may want to double-check that this doesn't get interpreted as a weird `Drop` bound even for non-const usages.
Add Result::{ok, err, and, or, unwrap_or} as const Already opened tracking issue rust-lang#92384. I don't think that this should actually cause any issues as long as the constness is unstable, but we may want to double-check that this doesn't get interpreted as a weird `Drop` bound even for non-const usages.
…askrgr Rollup of 8 pull requests Successful merges: - rust-lang#91993 (Tweak output for non-exhaustive `match` expression) - rust-lang#92385 (Add Result::{ok, err, and, or, unwrap_or} as const) - rust-lang#94559 (Remove argument from closure in thread::Scope::spawn.) - rust-lang#94580 (Emit `unused_attributes` if a level attr only has a reason) - rust-lang#94586 (Generalize `get_nullable_type` to allow types where null is all-ones.) - rust-lang#94708 (diagnostics: only talk about `Cargo.toml` if running under Cargo) - rust-lang#94712 (promot debug_assert to assert) - rust-lang#94726 (:arrow_up: rust-analyzer) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
This unstable library feature, which covers the `const fn` version of `Result::{ok, error, and, or, unwrap_or}`, was added by [rust-lang/rust#92385][1]. [1]: rust-lang/rust#92385
Already opened tracking issue #92384.
I don't think that this should actually cause any issues as long as the constness is unstable, but we may want to double-check that this doesn't get interpreted as a weird
Drop
bound even for non-const usages.