Skip to content

Commit 04f7b69

Browse files
committed
implement cloned for Option
1 parent 0c7a3d6 commit 04f7b69

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/libcore/option.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ use mem;
150150
use result::{Result, Ok, Err};
151151
use slice;
152152
use 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+
679688
impl<T: Default> Option<T> {
680689
/// Returns the contained value or a default
681690
///

src/libcoretest/option.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use core::option::*;
1212
use core::kinds::marker;
1313
use core::mem;
14+
use core::clone::Clone;
1415

1516
#[test]
1617
fn 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+
}

0 commit comments

Comments
 (0)