From 51b29d618ae949cb31dfacb91959309582ed0715 Mon Sep 17 00:00:00 2001 From: panicbit Date: Mon, 7 Aug 2017 01:25:43 +0200 Subject: [PATCH 1/3] libcore: Implement cloned() for Option<&mut T> --- src/libcore/option.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index ef41b6794105d..5328877a64b7c 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -774,6 +774,25 @@ impl<'a, T: Clone> Option<&'a T> { } } +impl<'a, T: Clone> Option<&'a mut T> { + /// Maps an `Option<&mut T>` to an `Option` by cloning the contents of the + /// option. + /// + /// # Examples + /// + /// ``` + /// let mut x = 12; + /// let opt_x = Some(&mut x); + /// assert_eq!(opt_x, Some(&mut 12)); + /// let cloned = opt_x.cloned(); + /// assert_eq!(cloned, Some(12)); + /// ``` + #[unstable(feature = "option_ref_mut_cloned", issue = "0")] + pub fn cloned(self) -> Option { + self.map(|t| t.clone()) + } +} + impl Option { /// Returns the contained value or a default /// From 5383205555cf8b498deb849682c50dd5029739be Mon Sep 17 00:00:00 2001 From: panicbit Date: Mon, 7 Aug 2017 12:54:40 +0200 Subject: [PATCH 2/3] Fix Option<&mut T>::cloned doc test --- src/libcore/option.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 5328877a64b7c..a3f449f1b2d76 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -781,6 +781,7 @@ impl<'a, T: Clone> Option<&'a mut T> { /// # Examples /// /// ``` + /// #![feature(option_ref_mut_cloned)] /// let mut x = 12; /// let opt_x = Some(&mut x); /// assert_eq!(opt_x, Some(&mut 12)); From 96182997dab6436b17d01d7333e068e683de7ba5 Mon Sep 17 00:00:00 2001 From: panicbit Date: Tue, 8 Aug 2017 15:33:58 +0200 Subject: [PATCH 3/3] Assign tracking issue to option_ref_mut_cloned --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index a3f449f1b2d76..6577607df0a7e 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -788,7 +788,7 @@ impl<'a, T: Clone> Option<&'a mut T> { /// let cloned = opt_x.cloned(); /// assert_eq!(cloned, Some(12)); /// ``` - #[unstable(feature = "option_ref_mut_cloned", issue = "0")] + #[unstable(feature = "option_ref_mut_cloned", issue = "43738")] pub fn cloned(self) -> Option { self.map(|t| t.clone()) }