-
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
Impl Try for Option #42526
Impl Try for Option #42526
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (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. |
src/libcore/option.rs
Outdated
/// The `Option` type. See [the module level documentation](index.html) for more. | ||
#[unstable(feature = "try_trait", issue = "42327")] | ||
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] | ||
pub struct Missing; |
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.
Wrong documentation?
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.
Yes, I needed something there in order to compile and forgot to change it. I'm not sure what to replace it with; maybe The equivalent of Option::None for a Result type.
?
This is exciting, and applying right after branching for release is probably the best option we have for insta-stable things like this. (This will allow Something I didn't want to push in the RFC discussion, but thought interesting: what about a That's intentionally non-constructible for now, since what I think it ought to be is Though I guess the type will stay unstable for a while, so it doesn't matter for now... |
src/libcore/tests/option.rs
Outdated
assert_eq!(try_option_none(), None); | ||
|
||
fn try_option_ok() -> Result<u8, Missing> { | ||
let val = Ok(1)?; |
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.
This is not going through the Option. I already works now... Maybe you meant to use Some here?
src/libcore/tests/option.rs
Outdated
assert_eq!(try_option_ok(), Ok(1)); | ||
|
||
fn try_option_err() -> Result<u8, Missing> { | ||
let val = Err(Missing)?; |
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.
And None here
assert_eq!(try_result_none(), None); | ||
|
||
fn try_result_ok() -> Result<u8, u8> { | ||
let val = Ok(1)?; |
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.
This is also nothing new... maybe change it into a compile-fail test by using Some/None and triggering a compilation error
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 didn't see any tests for Result's Try implemenation, but if they exist I can remove these tests.
I think one way to prevent it from being insta-stable would be to make it I am open to bikeshedding the name of |
Maybe |
@kennytm well that was more about a public "cfg", but yeah apparently we don't have |
I also like At this point what do I need to do? |
I think we should settle on the name -- I am not 100% sure when we should merge this RFC though. The insta-stable thing bothers me, but I don't have a good idea what else to do about it. |
@rust-lang/lang @rust-lang/libs -- anybody care to "weigh in" on the name of the "error type" to use in the |
If we follow the naming practice in libstd, it should be |
What's the reason not to use |
@withoutboats rust-lang/rfcs#1859 explained why the error must be a dedicated type (to prevent accidentally ?-returning an |
I think
Any thoughts on how to better document this, regardless of what the final name is? |
Ergonomically at least this seems most likely to arise in something like: impl From<Missing> for MyErrorType (if at all). That way you could |
|
The feeling was that enum List<T> {
Nil,
Cons(T, Box<List<T>>),
} Imagine that I implemented |
👍 for "The error type that results from applying the try operator ( |
10fc9fb
to
e30d92b
Compare
@huntiep I took the liberty of rebasing for you. |
@nikomatsakis I'm sorry, I missed that part of your last comment. For future reference, can you briefly explain what you meant by rebase? |
@huntiep it refers to |
@nikomatsakis did you mean to review at bors? |
@bors: r=nikomatsakis |
📌 Commit e30d92b has been approved by |
Impl Try for Option This is part of #31436.
☀️ Test successful - status-appveyor, status-travis |
Short-circuiting internal iteration with Iterator::try_fold & try_rfold These are the core methods in terms of which the other methods (`fold`, `all`, `any`, `find`, `position`, `nth`, ...) can be implemented, allowing Iterator implementors to get the full goodness of internal iteration by only overriding one method (per direction). Based off the `Try` trait, so works with both `Result` and `Option` (:tada: #42526). The `try_fold` rustdoc examples use `Option` and the `try_rfold` ones use `Result`. AKA continuing in the vein of PRs #44682 & #44856 for more of `Iterator`. New bench following the pattern from the latter of those: ``` test iter::bench_take_while_chain_ref_sum ... bench: 1,130,843 ns/iter (+/- 25,110) test iter::bench_take_while_chain_sum ... bench: 362,530 ns/iter (+/- 391) ``` I also ran the benches without the `fold` & `rfold` overrides to test their new default impls, with basically no change. I left them there, though, to take advantage of existing overrides and because `AlwaysOk` has some sub-optimality due to #43278 (which 45225 should fix). If you're wondering why there are three type parameters, see issue #45462 Thanks for @bluss for the [original IRLO thread](https://internals.rust-lang.org/t/pre-rfc-fold-ok-is-composable-internal-iteration/4434) and the rfold PR and to @cuviper for adding so many folds, [encouraging me](#45379 (comment)) to make this PR, and finding a catastrophic bug in a pre-review.
This is part of #31436.