Skip to content

Commit 705383a

Browse files
committed
Add unstable Option::copied()
1 parent 21cb46a commit 705383a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/libcore/option.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,48 @@ impl<T> Option<T> {
884884
}
885885
}
886886

887+
impl<'a, T: Copy> Option<&'a T> {
888+
/// Maps an `Option<&T>` to an `Option<T>` by copying the contents of the
889+
/// option.
890+
///
891+
/// # Examples
892+
///
893+
/// ```
894+
/// #![feature(copied)]
895+
///
896+
/// let x = 12;
897+
/// let opt_x = Some(&x);
898+
/// assert_eq!(opt_x, Some(&12));
899+
/// let copied = opt_x.copied();
900+
/// assert_eq!(copied, Some(12));
901+
/// ```
902+
#[unstable(feature = "copied", issue = "0")]
903+
pub fn copied(self) -> Option<T> {
904+
self.map(|&t| t)
905+
}
906+
}
907+
908+
impl<'a, T: Copy> Option<&'a mut T> {
909+
/// Maps an `Option<&mut T>` to an `Option<T>` by copying the contents of the
910+
/// option.
911+
///
912+
/// # Examples
913+
///
914+
/// ```
915+
/// #![feature(copied)]
916+
///
917+
/// let mut x = 12;
918+
/// let opt_x = Some(&mut x);
919+
/// assert_eq!(opt_x, Some(&mut 12));
920+
/// let copied = opt_x.copied();
921+
/// assert_eq!(copied, Some(12));
922+
/// ```
923+
#[unstable(feature = "copied", issue = "0")]
924+
pub fn copied(self) -> Option<T> {
925+
self.map(|&mut t| t)
926+
}
927+
}
928+
887929
impl<'a, T: Clone> Option<&'a T> {
888930
/// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
889931
/// option.

0 commit comments

Comments
 (0)