-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Provide Entry-like API for Option #39289
Conversation
r? @brson (rust_highfive has picked a reviewer for you, use r? to override) |
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.
mostly formatting nits
src/libcore/option.rs
Outdated
///////////////////////////////////////////////////////////////////////// | ||
|
||
/// Inserts `v` into the option if it is None, then returns a reference to | ||
/// the contained value |
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.
needs a period at the end
src/libcore/option.rs
Outdated
// Entry-like operations to insert if None and return a reference | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
/// Inserts `v` into the option if it is None, then returns a reference to |
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.
None
should also be in backticks
src/libcore/option.rs
Outdated
|
||
|
||
/// Inserts a value computed from `f` into the option if it is None, then | ||
/// returns a reference to the contained value |
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.
same two issues as above
src/libcore/option.rs
Outdated
} | ||
|
||
/// Inserts `v` into the option if it is None, then | ||
/// returns a mutable reference to the contained value |
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.
same as above
src/libcore/option.rs
Outdated
} | ||
|
||
/// Inserts a value computed from `f` into the option if it is None, then | ||
/// returns a mutable reference to the contained value |
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.
same as above
src/libcore/option.rs
Outdated
/// | ||
/// let mut x = None; | ||
/// let y: &mut u32 = x.get_mut_or_insert(5); | ||
/// assert_eq!(y, &5); |
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.
could you put new lines before and after this, give it a little space?
src/libcore/option.rs
Outdated
/// let mut x = None; | ||
/// let y: &mut u32 = x.get_mut_or_insert(5); | ||
/// assert_eq!(y, &5); | ||
/// *y = 7; |
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.
should there be another assert_eq after 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.
I didn't do it on purpose, because the only thing that was interesting here was the mutability of the returned value and I wanted the example shorter. But I am happy to add it if you think that's better?
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, this gave me an idea for something hopefully even better. Running tests locally before pushing new version :)
src/libcore/option.rs
Outdated
/// let mut x = None; | ||
/// let y: &mut u32 = x.get_mut_or_insert_with(|| 5); | ||
/// assert_eq!(y, &5); | ||
/// *y = 7; |
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.
same issue as above
/// ``` | ||
/// #![feature(option_entry)] | ||
/// | ||
/// let mut x = None; |
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.
could you put a newline after this please?
/// ``` | ||
/// #![feature(option_entry)] | ||
/// | ||
/// let mut x = None; |
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.
could you put a newline after this please?
I think I addressed all your review comments, let me know if you like it. I can also squash the changes if that's preferable. |
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.
two tiny more. this is great 👍 squash is probably good, but feel free to wait until someone signs off on the implementations too
src/libcore/option.rs
Outdated
/// | ||
/// *y = 7; | ||
/// } | ||
/// assert_eq!(x, Some(7)); |
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.
one more newline above this?
this example is much better 👍
src/libcore/option.rs
Outdated
/// | ||
/// *y = 7; | ||
/// } | ||
/// assert_eq!(x, Some(7)); |
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.
same here
src/libcore/option.rs
Outdated
/// *y = 7; | ||
/// | ||
/// { | ||
/// let y: &mut u32 = x.get_mut_or_insert_with(|| 5); |
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.
same here
src/libcore/option.rs
Outdated
/// *y = 7; | ||
/// | ||
/// { | ||
/// let y: &mut u32 = x.get_mut_or_insert(5); |
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.
all this code should be indented another two spaces; four is the standard style.
changes made, thanks :) |
I'm just learning rust but isn't this a huge api (4 new functions) for what seems like a trivial problem? Option does not have so many functions and I can guess most of the people just learn them all. This is just more to learn. |
To me, this should be a part of the stdlib because for the user it's very hard to reason about the unreachable path and whether that'll be optimized out or not. Entry-style APIs should be ubiquitous, imho. Edit: And with that comment, I think if this is deemed acceptable it should use the intrinsics unreachable for the implementation. |
Why are the immutable versions required at all? You can always use the mutable reference like an immutable one. |
Oh indeed. |
And now you can remove the |
Ping, should I be doing anything here? Like providing a rebase or something? Hope I'm not accidentally blocking this by messing up a required step. |
📌 Commit 25c94c3 has been approved by |
Sure I shouldn't rebase it? But yay anyway :) |
Nah this is still mergeable (no need for a rebase). If you want to squash down the commits though feel free and I'll re-r+ |
This implements rust-lang#39288. Thanks to @steveklabnik and @oli-obk for their review comments :)
I squashed it up, now less messy. Thanks |
@bors r=alexcrichton |
📌 Commit 8e02ad0 has been approved by |
Provide Entry-like API for Option This implements rust-lang#39288. I am wondering whether to use std::intrinsics::unreachable!() here. Both seems fine to me (the second match optimizes away in release mode).
This implements #39288.
I am wondering whether to use std::intrinsics::unreachable!() here. Both seems fine to me (the second match optimizes away in release mode).