From fee30882c159f9742996296ad64362cc6fd19513 Mon Sep 17 00:00:00 2001 From: Denis Kolodin Date: Tue, 30 Aug 2016 09:58:56 +0300 Subject: [PATCH] Improve Option::cloned method to allow mutable references --- src/libcore/option.rs | 3 ++- src/libcoretest/option.rs | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index cf52849e01972..6a5f9f9e833bf 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -141,6 +141,7 @@ use iter::{FromIterator, FusedIterator}; use mem; +use ops::Deref; // Note that this is not a lang item per se, but it has a hidden dependency on // `Iterator`, which is one. The compiler assumes that the `next` method of @@ -642,7 +643,7 @@ impl Option { } } -impl<'a, T: Clone> Option<&'a T> { +impl> Option { /// Maps an `Option<&T>` to an `Option` by cloning the contents of the /// option. #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcoretest/option.rs b/src/libcoretest/option.rs index 51b0655f680f6..a352712ff6d35 100644 --- a/src/libcoretest/option.rs +++ b/src/libcoretest/option.rs @@ -254,6 +254,8 @@ fn test_cloned() { let val = 1; let val_ref = &val; let opt_none: Option<&'static u32> = None; + let mut opt_mut = Some(val); + let opt_box: Option> = Some(Box::new(val)); let opt_ref = Some(&val); let opt_ref_ref = Some(&val_ref); @@ -269,4 +271,10 @@ fn test_cloned() { assert_eq!(opt_ref_ref.clone(), Some(&val_ref)); assert_eq!(opt_ref_ref.clone().cloned(), Some(&val)); assert_eq!(opt_ref_ref.cloned().cloned(), Some(1)); + + // Mutable ref works + assert_eq!(opt_mut.as_mut().cloned(), Some(1)); + + // Deep Deref works + assert_eq!(opt_box.cloned(), Some(1)); }