diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 77cb447b9546..0aa8fcb69b9c 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -835,7 +835,7 @@ impl Result<&T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { + pub fn copied_ok(self) -> Result { self.map(|&t| t) } } @@ -855,7 +855,7 @@ impl Result<&mut T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { + pub fn copied_ok(self) -> Result { self.map(|&mut t| t) } } @@ -900,6 +900,74 @@ impl Result { } } +impl Result<&T, &E> { + /// Maps a `Result<&T, &E>` to a `Result` by copying the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&1), Err(1)); + /// assert_eq!(Ok(&42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + +impl Result<&mut T, &E> { + /// Maps a `Result<&mut T, &E>` to a `Result` by copying the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&1), Err(1)); + /// assert_eq!(Ok(&mut 42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + +impl Result<&T, &mut E> { + /// Maps a `Result<&T, &mut E>` to a `Result` by copying the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&mut 1), Err(1)); + /// assert_eq!(Ok(&42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + +impl Result<&mut T, &mut E> { + /// Maps a `Result<&mut T, &mut E>` to a `Result` by copying + /// the contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&mut 1), Err(1)); + /// assert_eq!(Ok(&mut 42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + impl Result<&T, E> { /// Maps a `Result<&T, E>` to a `Result` by cloning the contents of the /// `Ok` part. @@ -915,7 +983,7 @@ impl Result<&T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { + pub fn cloned_ok(self) -> Result { self.map(|t| t.clone()) } } @@ -935,7 +1003,7 @@ impl Result<&mut T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { + pub fn cloned_ok(self) -> Result { self.map(|t| t.clone()) } } @@ -980,6 +1048,74 @@ impl Result { } } +impl Result<&T, &E> { + /// Maps a `Result<&T, &E>` to a `Result` by cloning the contents of the + /// result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&1).cloned(), Err(1)); + /// assert_eq!(Ok(&42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + +impl Result<&mut T, &E> { + /// Maps a `Result<&mut T, &E>` to a `Result` by cloning the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&1).cloned(), Err(1)); + /// assert_eq!(Ok(&mut 42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + +impl Result<&T, &mut E> { + /// Maps a `Result<&T, &mut E>` to a `Result` by cloning the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&mut 1).cloned(), Err(1)); + /// assert_eq!(Ok(&42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + +impl Result<&mut T, &mut E> { + /// Maps a `Result<&mut T, &mut E>` to a `Result` by cloning + /// the contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&mut 1).cloned(), Err(1)); + /// assert_eq!(Ok(&mut 42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + impl Result { /// Unwraps a result, yielding the content of an [`Ok`]. ///