From 9b90e1762e6cb21baa504a27530b9aa404fbe3ac Mon Sep 17 00:00:00 2001 From: Canop Date: Thu, 1 Oct 2020 11:13:38 +0200 Subject: [PATCH 1/7] add `insert` and `insert_with` to `Option` 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, }; ``` --- library/core/src/option.rs | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 825144e5a6fbe..394be746ec2fa 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -562,6 +562,52 @@ impl Option { } } + ///////////////////////////////////////////////////////////////////////// + // Setting a new value + ///////////////////////////////////////////////////////////////////////// + + /// Inserts `v` into the option then returns a mutable reference + /// to the contained value. + /// + /// # Example + /// + /// ``` + /// #![feature(option_insert)] + /// + /// let mut o = None; + /// let v = o.insert(3); + /// assert_eq!(*v, 3); + /// ``` + #[inline] + #[unstable(feature = "option_insert", reason = "new API", issue = "none")] + pub fn insert(&mut self, v: T) -> &mut T { + self.insert_with(|| v) + } + + /// Inserts a value computed from `f` into the option, then returns a + /// mutable reference to the contained value. + /// + /// # Example + /// + /// ``` + /// #![feature(option_insert)] + /// + /// let mut o = None; + /// let v = o.insert_with(|| 3); + /// assert_eq!(*v, 3); + /// ``` + #[inline] + #[unstable(feature = "option_insert", reason = "new API", issue = "none")] + pub fn insert_with T>(&mut self, f: F) -> &mut T { + *self = Some(f()); + + match *self { + Some(ref mut v) => v, + // SAFETY: the code above just filled the option + None => unsafe { hint::unreachable_unchecked() }, + } + } + ///////////////////////////////////////////////////////////////////////// // Iterator constructors ///////////////////////////////////////////////////////////////////////// From e8df2a426959fa3ff4f65eae85e618394bf27e28 Mon Sep 17 00:00:00 2001 From: Canop Date: Thu, 1 Oct 2020 13:24:33 +0200 Subject: [PATCH 2/7] remove `option.insert_with` `option.insert` covers both needs anyway, `insert_with` is redundant. --- library/core/src/option.rs | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 394be746ec2fa..64541da300e86 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -581,25 +581,7 @@ impl Option { #[inline] #[unstable(feature = "option_insert", reason = "new API", issue = "none")] pub fn insert(&mut self, v: T) -> &mut T { - self.insert_with(|| v) - } - - /// Inserts a value computed from `f` into the option, then returns a - /// mutable reference to the contained value. - /// - /// # Example - /// - /// ``` - /// #![feature(option_insert)] - /// - /// let mut o = None; - /// let v = o.insert_with(|| 3); - /// assert_eq!(*v, 3); - /// ``` - #[inline] - #[unstable(feature = "option_insert", reason = "new API", issue = "none")] - pub fn insert_with T>(&mut self, f: F) -> &mut T { - *self = Some(f()); + *self = Some(v); match *self { Some(ref mut v) => v, From 60a96cae336b621be3a5e01cf6c87649b327f836 Mon Sep 17 00:00:00 2001 From: Canop Date: Thu, 1 Oct 2020 16:05:01 +0200 Subject: [PATCH 3/7] more tests in option.insert, code cleaning in option Code cleaning made according to suggestions in discussion on PR ##77392 impacts insert, get_or_insert and get_or_insert_with. --- library/core/src/option.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 64541da300e86..65575f4c41bad 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -574,17 +574,22 @@ impl Option { /// ``` /// #![feature(option_insert)] /// - /// let mut o = None; - /// let v = o.insert(3); - /// assert_eq!(*v, 3); + /// 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 = "new API", issue = "none")] - pub fn insert(&mut self, v: T) -> &mut T { - *self = Some(v); + #[unstable(feature = "option_insert", reason = "newly added", issue = "none")] + pub fn insert(&mut self, val: T) -> &mut T { + *self = Some(val); - match *self { - Some(ref mut v) => v, + match self { + Some(v) => v, // SAFETY: the code above just filled the option None => unsafe { hint::unreachable_unchecked() }, } @@ -839,8 +844,8 @@ impl Option { /// ``` #[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, val: T) -> &mut T { + self.get_or_insert_with(|| val) } /// Inserts a value computed from `f` into the option if it is [`None`], then @@ -867,8 +872,8 @@ impl Option { *self = Some(f()); } - match *self { - Some(ref mut v) => v, + match self { + Some(v) => v, // SAFETY: a `None` variant for `self` would have been replaced by a `Some` // variant in the code above. None => unsafe { hint::unreachable_unchecked() }, From cc8b77a7cf0e1179b148a70c8f760a7ba3c1debd Mon Sep 17 00:00:00 2001 From: Canop Date: Sat, 3 Oct 2020 10:29:11 +0200 Subject: [PATCH 4/7] fix naming unconsistency between function doc and prototype --- library/core/src/option.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 65575f4c41bad..8fd3bfe77b800 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -566,8 +566,7 @@ impl Option { // Setting a new value ///////////////////////////////////////////////////////////////////////// - /// Inserts `v` into the option then returns a mutable reference - /// to the contained value. + /// Inserts `value` into the option then returns a mutable reference to it. /// /// # Example /// @@ -585,8 +584,8 @@ impl Option { /// ``` #[inline] #[unstable(feature = "option_insert", reason = "newly added", issue = "none")] - pub fn insert(&mut self, val: T) -> &mut T { - *self = Some(val); + pub fn insert(&mut self, value: T) -> &mut T { + *self = Some(value); match self { Some(v) => v, @@ -825,7 +824,7 @@ impl Option { // 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 @@ -844,12 +843,12 @@ impl Option { /// ``` #[inline] #[stable(feature = "option_entry", since = "1.20.0")] - pub fn get_or_insert(&mut self, val: T) -> &mut T { - self.get_or_insert_with(|| val) + pub fn get_or_insert(&mut self, value: T) -> &mut T { + 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 /// From 39557799c763d75b58cbd7235af49a29c4d1212c Mon Sep 17 00:00:00 2001 From: Canop Date: Fri, 23 Oct 2020 11:08:09 +0200 Subject: [PATCH 5/7] Update library/core/src/option.rs Co-authored-by: Mara Bos --- library/core/src/option.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 8fd3bfe77b800..4e8a74d9162a5 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -568,6 +568,8 @@ impl Option { /// 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 /// /// ``` From 415a8e526d0e99f0e85e3b06bad9d2f2867910c5 Mon Sep 17 00:00:00 2001 From: Canop Date: Fri, 23 Oct 2020 11:09:15 +0200 Subject: [PATCH 6/7] Update library/core/src/option.rs Co-authored-by: Ivan Tham --- library/core/src/option.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 4e8a74d9162a5..a3d20f016fd98 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -562,10 +562,6 @@ impl Option { } } - ///////////////////////////////////////////////////////////////////////// - // Setting a new value - ///////////////////////////////////////////////////////////////////////// - /// Inserts `value` into the option then returns a mutable reference to it. /// /// If the option already contains a value, the old value is dropped. From 216d0fe36466ce9307a643a67afa41ebfb8c43dd Mon Sep 17 00:00:00 2001 From: Canop Date: Fri, 23 Oct 2020 11:44:58 +0200 Subject: [PATCH 7/7] add tracking issue number to option_insert feature gate --- library/core/src/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index a3d20f016fd98..3daf26208b937 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -581,7 +581,7 @@ impl Option { /// assert_eq!(opt.unwrap(), 3); /// ``` #[inline] - #[unstable(feature = "option_insert", reason = "newly added", issue = "none")] + #[unstable(feature = "option_insert", reason = "newly added", issue = "78271")] pub fn insert(&mut self, value: T) -> &mut T { *self = Some(value);