implement cloned for Option
This commit is contained in:
parent
0c7a3d6c16
commit
04f7b690ba
2 changed files with 22 additions and 0 deletions
|
@ -150,6 +150,7 @@ use mem;
|
||||||
use result::{Result, Ok, Err};
|
use result::{Result, Ok, Err};
|
||||||
use slice;
|
use slice;
|
||||||
use slice::AsSlice;
|
use slice::AsSlice;
|
||||||
|
use clone::Clone;
|
||||||
|
|
||||||
// Note that this is not a lang item per se, but it has a hidden dependency on
|
// 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
|
// `Iterator`, which is one. The compiler assumes that the `next` method of
|
||||||
|
@ -676,6 +677,14 @@ impl<T> Option<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Clone> Option<&'a T> {
|
||||||
|
/// Maps an Option<&T> to an Option<T> by cloning the contents of the Option<&T>.
|
||||||
|
#[unstable = "recently added as part of collections reform"]
|
||||||
|
pub fn cloned(self) -> Option<T> {
|
||||||
|
self.map(|t| t.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: Default> Option<T> {
|
impl<T: Default> Option<T> {
|
||||||
/// Returns the contained value or a default
|
/// Returns the contained value or a default
|
||||||
///
|
///
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
use core::option::*;
|
use core::option::*;
|
||||||
use core::kinds::marker;
|
use core::kinds::marker;
|
||||||
use core::mem;
|
use core::mem;
|
||||||
|
use core::clone::Clone;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_ptr() {
|
fn test_get_ptr() {
|
||||||
|
@ -239,3 +240,15 @@ fn test_collect() {
|
||||||
|
|
||||||
assert!(v == None);
|
assert!(v == None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_cloned() {
|
||||||
|
let s = 1u32;
|
||||||
|
let n: Option<&'static u32> = None;
|
||||||
|
let o = Some(&s);
|
||||||
|
|
||||||
|
assert_eq!(o.clone(), Some(&s));
|
||||||
|
assert_eq!(o.cloned(), Some(1u32));
|
||||||
|
|
||||||
|
assert_eq!(n.clone(), None);
|
||||||
|
assert_eq!(n.cloned(), None);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue