X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibcore%2Foption.rs;h=0e54397db02476942bdda8998f04f4447b64a5a4;hb=0b1669d96cee9dec9035a50fdf0a967a68605f98;hp=2b01da0de5203da0124144100fe4a9ed740ba06a;hpb=a0538c8f79c70cfa7a59412d5bb7664ada023c81;p=rust.git diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 2b01da0de52..0e54397db02 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1,13 +1,3 @@ -// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - //! Optional values. //! //! Type [`Option`] represents an optional value: every [`Option`] @@ -884,6 +874,48 @@ pub fn replace(&mut self, value: T) -> 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 = "57126")] + 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 = "57126")] + 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. @@ -981,8 +1013,6 @@ impl Option> { /// # Examples /// /// ``` - /// #![feature(transpose_result)] - /// /// #[derive(Debug, Eq, PartialEq)] /// struct SomeErr; /// @@ -991,7 +1021,7 @@ impl Option> { /// assert_eq!(x, y.transpose()); /// ``` #[inline] - #[unstable(feature = "transpose_result", issue = "47338")] + #[stable(feature = "transpose_result", since = "1.33.0")] pub fn transpose(self) -> Result, E> { match self { Some(Ok(x)) => Ok(Some(x)),