-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 a Result::into_ok_or_err
method to extract a T
from Result<T, T>
#80572
Conversation
r? @shepmaster (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
|
I'm comfortable renaming if desired. I think I don't care that much about the name so long as the functionality is available under some name though. Another suggestion I got was implementing |
/// cases where you don't care if the result was `Ok` or not. | ||
/// | ||
/// [`Atomic*::compare_exchange`]: crate::sync::atomic::AtomicBool::compare_exchange | ||
/// [binary_search]: ../../std/primitive.slice.html#method.binary_search |
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.
Note that:
- I can't use the fancy intradoc links stuff here for reasons that only @jyn514 comprehends.
- Apparently primitives are only documented in std (No documentation for primitives in
core
#80334 or something), so I have to link it instd
.
That said, I'm also okay with getting rid of these links entirely if that's desirable.
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.
You can now use intra-doc links here!
/// [binary_search]: ../../std/primitive.slice.html#method.binary_search | |
/// [binary_search]: slice::binary_search |
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.
Actually, better yet you can change the part above that has the link to just [`slice::binary_search`]
and remove this line.
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.
You can now use intra-doc links here!
No, the bootstrap bump hasn't happened yet. Intra-doc pointers landed in #80181, which is in 1.51, which is still in nightly.
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.
Dang, I always forget about the bootstrap bump :(
/// `Err`. | ||
/// | ||
/// This can be useful in conjunction with APIs such as | ||
/// [`Atomic*::compare_exchange`], or [`slice::binary_search`][binary_search], but only in |
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.
Binary search is the primary reason I've wanted similar, but I've also seen people want to use it in procedural macros where you use a TokenStream
for success and failure cases.
This seems reasonable to add as an unstable feature.
Naming consistency with |
☔ The latest upstream changes (presumably #81417) made this pull request unmergeable. Please resolve the merge conflicts. |
r? @m-ou-se |
We briefly discussed this in the libs team meeting last week, and agreed this is a function we should have. There was some discussion on the name, but felt comfortable landing this as Can you open a tracking issue? Thanks! |
|
Result::ok_or_err
method to extract a T
from Result<T, T>
Result::ok_or_err
~~ Result::into_ok_or_err
method to extract a T
from Result<T, T>
Result::ok_or_err
~~ Result::into_ok_or_err
method to extract a T
from Result<T, T>
Result::into_ok_or_err
method to extract a T
from Result<T, T>
@bors r+ rollup |
📌 Commit 404da0b has been approved by |
Rollup of 8 pull requests Successful merges: - rust-lang#77728 (Expose force_quotes on Windows.) - rust-lang#80572 (Add a `Result::into_ok_or_err` method to extract a `T` from `Result<T, T>`) - rust-lang#81860 (Fix SourceMap::start_point) - rust-lang#81869 (Simplify pattern grammar, improve or-pattern diagnostics) - rust-lang#81898 (Fix debug information for function arguments of type &str or slice.) - rust-lang#81972 (Placeholder lifetime error cleanup) - rust-lang#82007 (Implement reborrow for closure captures) - rust-lang#82021 (Spell out nested Self type in lint message) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
When updating code to handle the semi-recent deprecation of
compare_and_swap
in favor ofcompare_exchange
, which returnsResult<T, T>
, I wanted this. I've also wanted it with code usingslice::binary_search
before.The name (and perhaps the documentation) is the hardest part here, but this name seems consistent with the other Result methods, and equivalently memorable.