-
Notifications
You must be signed in to change notification settings - Fork 13.4k
add insert
to Option
#77392
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
Merged
+37
−7
Merged
add insert
to Option
#77392
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9b90e17
add `insert` and `insert_with` to `Option`
Canop e8df2a4
remove `option.insert_with`
Canop 60a96ca
more tests in option.insert, code cleaning in option
Canop cc8b77a
fix naming unconsistency between function doc and prototype
Canop 3955779
Update library/core/src/option.rs
Canop 415a8e5
Update library/core/src/option.rs
Canop 216d0fe
add tracking issue number to option_insert feature gate
Canop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -562,6 +562,36 @@ impl<T> Option<T> { | |
} | ||
} | ||
|
||
/// Inserts `value` into the option then returns a mutable reference to it. | ||
/// | ||
/// If the option already contains a value, the old value is dropped. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// #![feature(option_insert)] | ||
/// | ||
/// let mut opt = None; | ||
/// let val = opt.insert(1); | ||
/// assert_eq!(*val, 1); | ||
/// assert_eq!(opt.unwrap(), 1); | ||
/// let val = opt.insert(2); | ||
/// assert_eq!(*val, 2); | ||
/// *val = 3; | ||
/// assert_eq!(opt.unwrap(), 3); | ||
/// ``` | ||
#[inline] | ||
#[unstable(feature = "option_insert", reason = "newly added", issue = "78271")] | ||
pub fn insert(&mut self, value: T) -> &mut T { | ||
*self = Some(value); | ||
|
||
match self { | ||
Some(v) => v, | ||
// SAFETY: the code above just filled the option | ||
None => unsafe { hint::unreachable_unchecked() }, | ||
} | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////// | ||
// Iterator constructors | ||
///////////////////////////////////////////////////////////////////////// | ||
|
@@ -792,7 +822,7 @@ impl<T> Option<T> { | |
// Entry-like operations to insert if None and return a reference | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
/// Inserts `v` into the option if it is [`None`], then | ||
/// Inserts `value` into the option if it is [`None`], then | ||
/// returns a mutable reference to the contained value. | ||
/// | ||
/// # Examples | ||
|
@@ -811,12 +841,12 @@ impl<T> Option<T> { | |
/// ``` | ||
#[inline] | ||
#[stable(feature = "option_entry", since = "1.20.0")] | ||
pub fn get_or_insert(&mut self, v: T) -> &mut T { | ||
self.get_or_insert_with(|| v) | ||
pub fn get_or_insert(&mut self, value: T) -> &mut T { | ||
m-ou-se marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.get_or_insert_with(|| value) | ||
} | ||
|
||
/// Inserts a value computed from `f` into the option if it is [`None`], then | ||
/// returns a mutable reference to the contained value. | ||
/// Inserts a value computed from `f` into the option if it is [`None`], | ||
/// then returns a mutable reference to the contained value. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -839,8 +869,8 @@ impl<T> Option<T> { | |
*self = Some(f()); | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. If we're changing this method anyway, might as well call match self {
Some(v) => v,
None => self.insert(f()),
} (GitHub didn't let me add this as a change suggestion) |
||
// SAFETY: a `None` variant for `self` would have been replaced by a `Some` | ||
// variant in the code above. | ||
None => unsafe { hint::unreachable_unchecked() }, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.