File tree 2 files changed +10
-1
lines changed
2 files changed +10
-1
lines changed Original file line number Diff line number Diff line change 141
141
142
142
use iter:: { FromIterator , FusedIterator } ;
143
143
use mem;
144
+ use ops:: Deref ;
144
145
145
146
// Note that this is not a lang item per se, but it has a hidden dependency on
146
147
// `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -642,7 +643,7 @@ impl<T> Option<T> {
642
643
}
643
644
}
644
645
645
- impl < ' a , T : Clone > Option < & ' a T > {
646
+ impl < T : Clone , D : Deref < Target = T > > Option < D > {
646
647
/// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
647
648
/// option.
648
649
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
Original file line number Diff line number Diff line change @@ -254,6 +254,8 @@ fn test_cloned() {
254
254
let val = 1 ;
255
255
let val_ref = & val;
256
256
let opt_none: Option < & ' static u32 > = None ;
257
+ let mut opt_mut = Some ( val) ;
258
+ let opt_box: Option < Box < u32 > > = Some ( Box :: new ( val) ) ;
257
259
let opt_ref = Some ( & val) ;
258
260
let opt_ref_ref = Some ( & val_ref) ;
259
261
@@ -269,4 +271,10 @@ fn test_cloned() {
269
271
assert_eq ! ( opt_ref_ref. clone( ) , Some ( & val_ref) ) ;
270
272
assert_eq ! ( opt_ref_ref. clone( ) . cloned( ) , Some ( & val) ) ;
271
273
assert_eq ! ( opt_ref_ref. cloned( ) . cloned( ) , Some ( 1 ) ) ;
274
+
275
+ // Mutable ref works
276
+ assert_eq ! ( opt_mut. as_mut( ) . cloned( ) , Some ( 1 ) ) ;
277
+
278
+ // Deep Deref works
279
+ assert_eq ! ( opt_box. cloned( ) , Some ( 1 ) ) ;
272
280
}
You can’t perform that action at this time.
0 commit comments