File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed
Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,7 @@ use mem;
150150use result:: { Result , Ok , Err } ;
151151use slice;
152152use slice:: AsSlice ;
153+ use clone:: Clone ;
153154
154155// Note that this is not a lang item per se, but it has a hidden dependency on
155156// `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -676,6 +677,14 @@ impl<T> Option<T> {
676677 }
677678}
678679
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+ #[ unstable = "recently added as part of collections reform" ]
683+ pub fn cloned ( self ) -> Option < T > {
684+ self . map( |t| t. clone( ) )
685+ }
686+ }
687+
679688impl <T : Default > Option < T > {
680689 /// Returns the contained value or a default
681690 ///
Original file line number Diff line number Diff line change 1111use core:: option:: * ;
1212use core:: kinds:: marker;
1313use core:: mem;
14+ use core:: clone:: Clone ;
1415
1516#[ test]
1617fn test_get_ptr ( ) {
@@ -239,3 +240,15 @@ fn test_collect() {
239240
240241 assert ! ( v == None ) ;
241242}
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