From 705383adb037dddd834970d2c10edb32dae9fa56 Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Wed, 5 Dec 2018 14:39:36 +0100 Subject: [PATCH] Add unstable Option::copied() --- src/libcore/option.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index cf1c77041b91..67bed63e6ff0 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -884,6 +884,48 @@ impl Option { } } +impl<'a, T: Copy> Option<&'a T> { + /// Maps an `Option<&T>` to an `Option` by copying the contents of the + /// option. + /// + /// # Examples + /// + /// ``` + /// #![feature(copied)] + /// + /// let x = 12; + /// let opt_x = Some(&x); + /// assert_eq!(opt_x, Some(&12)); + /// let copied = opt_x.copied(); + /// assert_eq!(copied, Some(12)); + /// ``` + #[unstable(feature = "copied", issue = "0")] + pub fn copied(self) -> Option { + self.map(|&t| t) + } +} + +impl<'a, T: Copy> Option<&'a mut T> { + /// Maps an `Option<&mut T>` to an `Option` by copying the contents of the + /// option. + /// + /// # Examples + /// + /// ``` + /// #![feature(copied)] + /// + /// let mut x = 12; + /// let opt_x = Some(&mut x); + /// assert_eq!(opt_x, Some(&mut 12)); + /// let copied = opt_x.copied(); + /// assert_eq!(copied, Some(12)); + /// ``` + #[unstable(feature = "copied", issue = "0")] + pub fn copied(self) -> Option { + self.map(|&mut t| t) + } +} + impl<'a, T: Clone> Option<&'a T> { /// Maps an `Option<&T>` to an `Option` by cloning the contents of the /// option.