File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,7 @@ use mem;
150
150
use result:: { Result , Ok , Err } ;
151
151
use slice;
152
152
use slice:: AsSlice ;
153
+ use clone:: Clone ;
153
154
154
155
// Note that this is not a lang item per se, but it has a hidden dependency on
155
156
// `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -676,6 +677,13 @@ impl<T> Option<T> {
676
677
}
677
678
}
678
679
680
+ impl <' a , T : Clone > Option < & ' a T > {
681
+ /// Maps an Option<&T> to an Option<T> by cloning the contents of the Option<&T>.
682
+ pub fn cloned ( self ) -> Option < T > {
683
+ self . map( |t| t. clone( ) )
684
+ }
685
+ }
686
+
679
687
impl <T : Default > Option < T > {
680
688
/// Returns the contained value or a default
681
689
///
Original file line number Diff line number Diff line change 11
11
use core:: option:: * ;
12
12
use core:: kinds:: marker;
13
13
use core:: mem;
14
+ use core:: clone:: Clone ;
14
15
15
16
#[ test]
16
17
fn test_get_ptr ( ) {
@@ -239,3 +240,15 @@ fn test_collect() {
239
240
240
241
assert ! ( v == None ) ;
241
242
}
243
+
244
+ fn test_cloned ( ) {
245
+ let s = 1u32 ;
246
+ let n: Option < & ' static u32 > = None ;
247
+ let o = Some ( & s) ;
248
+
249
+ assert_eq ! ( o. clone( ) , Some ( & s) ) ;
250
+ assert_eq ! ( o. cloned( ) , Some ( 1u32 ) ) ;
251
+
252
+ assert_eq ! ( n. clone( ) , None ) ;
253
+ assert_eq ! ( n. cloned( ) , None ) ;
254
+ }
You can’t perform that action at this time.
0 commit comments