-
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 insert
to Option
#77392
add insert
to Option
#77392
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @LukasKalbertodt (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. |
I've wanted this for a while. 👍 You could consider also changing |
I will better use
This allow a more generic use of this feature Current alternative to unwrap: let checked_cache = cache.as_ref().filter(|e| e.is_valid());
let content = match checked_cache {
Some(e) => &e.content,
None => {
cache = None;
&checked_cache.get_or_insert_with(build).content
}
}; |
Thumbs up for |
@petervaro I see no reason to have |
@Stargateur that's fair! |
The name The goal isn't to get the old value, using a tupple would add verbosity while there's already |
I agree |
I also think |
match *self { | ||
Some(ref mut v) => v, | ||
match self { | ||
Some(v) => v, |
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.
If we're changing this method anyway, might as well call insert
:
match self {
Some(v) => v,
None => self.insert(f()),
}
(GitHub didn't let me add this as a change suggestion)
Don't we already have pub fn get_or_insert(&mut self, v: T) -> &mut T @jyn514 IMHO It even have similar code, I don't see why is this necessary. https://doc.rust-lang.org/stable/src/core/option.rs.html#852-861 @Canop maybe |
@pickfire No there are not equivalent, if option is |
Question is: Should I do something, remove or add something, in order for the PR to be merged ? |
@Canop this is waiting on review from @LukasKalbertodt , no one who's reviewed so far is on T-libs. |
Nice! (I was just looking for this method a few days ago.) It might be good to consider some other possible names. Can you open a tracking issue, add its number in the Thanks! |
This removes a cause of `unwrap` and code complexity. This allows replacing ``` option_value = Some(build()); option_value.as_mut().unwrap() ``` with ``` option_value.insert(build()) ``` or ``` option_value.insert_with(build) ``` It's also useful in contexts not requiring the mutability of the reference. Here's a typical cache example: ``` let checked_cache = cache.as_ref().filter(|e| e.is_valid()); let content = match checked_cache { Some(e) => &e.content, None => { cache = Some(compute_cache_entry()); // unwrap is OK because we just filled the option &cache.as_ref().unwrap().content } }; ``` It can be changed into ``` let checked_cache = cache.as_ref().filter(|e| e.is_valid()); let content = match checked_cache { Some(e) => &e.content, None => &cache.insert_with(compute_cache_entry).content, }; ```
`option.insert` covers both needs anyway, `insert_with` is redundant.
Code cleaning made according to suggestions in discussion on PR #rust-lang#77392 impacts insert, get_or_insert and get_or_insert_with.
Co-authored-by: Mara Bos <m-ou.se@m-ou.se>
Co-authored-by: Ivan Tham <pickfire@riseup.net>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks for your contribution! @bors r+ rollup This PR is now approved and waiting in the queue to be tested more thoroughly before it gets merged. The queue is a bit long at the moment, so it might take a while before this PR is merged. After it's merged, your new function should be available (unstably, feature gated) in the next nightly Rust. Once it's been in a few releases and all questions on the tracking issue are resolved, stabilization can be proposed, which will then require approval from the majority of the libs team. |
📌 Commit 216d0fe has been approved by |
…as-schievink Rollup of 15 pull requests Successful merges: - rust-lang#76649 (Add a spin loop hint for Arc::downgrade) - rust-lang#77392 (add `insert` to `Option`) - rust-lang#77716 (Revert "Allow dynamic linking for iOS/tvOS targets.") - rust-lang#78109 (Check for exhaustion in RangeInclusive::contains and slicing) - rust-lang#78198 (Simplify assert terminator only if condition evaluates to expected value) - rust-lang#78243 (--test-args flag description) - rust-lang#78249 (improve const infer error) - rust-lang#78250 (Document inline-const) - rust-lang#78264 (Add regression test for issue-77475) - rust-lang#78274 (Update description of Empty Enum for accuracy) - rust-lang#78278 (move `visit_predicate` into `TypeVisitor`) - rust-lang#78292 (Loop instead of recursion) - rust-lang#78293 (Always store Rustdoc theme when it's changed) - rust-lang#78300 (Make codegen coverage_context optional, and check) - rust-lang#78307 (Revert "Set .llvmbc and .llvmcmd sections as allocatable") Failed merges: r? `@ghost`
This is now available in nightly Rust: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.insert |
This removes a cause of
unwrap
and code complexity.This allows replacing
with
It's also useful in contexts not requiring the mutability of the reference.
Here's a typical cache example:
It can be changed into
(edited: I removed
insert_with
)