From a0ab5a3651f5cb30efa2f82a3ee15a7df479080a Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 1 Aug 2019 00:26:47 +0800 Subject: [PATCH] Add Result::cloned{,_err} and Result::copied{,_err} --- src/libcore/result.rs | 160 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 559877ddd5a..a8f0f422cb4 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -820,6 +820,166 @@ pub fn unwrap_or_else T>(self, op: F) -> T { } } +impl Result<&T, E> { + /// Maps a `Result<&T, E>` to a `Result` by copying the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Ok(&val); + /// assert_eq!(x, Ok(&12)); + /// let copied = x.copied(); + /// assert_eq!(copied, Ok(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied(self) -> Result { + self.map(|&t| t) + } +} + +impl Result<&mut T, E> { + /// Maps a `Result<&mut T, E>` to a `Result` by copying the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Ok(&mut val); + /// assert_eq!(x, Ok(&mut 12)); + /// let copied = x.copied(); + /// assert_eq!(copied, Ok(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied(self) -> Result { + self.map(|&mut t| t) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by copying the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Err(&val); + /// assert_eq!(x, Err(&12)); + /// let copied = x.copied_err(); + /// assert_eq!(copied, Err(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied_err(self) -> Result { + self.map_err(|&e| e) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by copying the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Err(&mut val); + /// assert_eq!(x, Err(&mut 12)); + /// let copied = x.copied(); + /// assert_eq!(cloned, Err(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied_err(self) -> Result { + self.map_err(|&mut e| e) + } +} + +impl Result<&T, E> { + /// Maps a `Result<&T, E>` to a `Result` by cloning the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Ok(&val); + /// assert_eq!(x, Ok(&12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Ok(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned(self) -> Result { + self.map(|t| t.clone()) + } +} + +impl Result<&mut T, E> { + /// Maps a `Result<&mut T, E>` to a `Result` by cloning the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Ok(&mut val); + /// assert_eq!(x, Ok(&mut 12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Ok(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned(self) -> Result { + self.map(|t| t.clone()) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by cloning the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Err(&mut val); + /// assert_eq!(x, Err(&mut 12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Err(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned_err(self) -> Result { + self.map_err(|e| e.clone()) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by cloning the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Err(&mut val); + /// assert_eq!(x, Err(&mut 12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Err(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned_err(self) -> Result { + self.map_err(|e| e.clone()) + } +} + impl Result { /// Unwraps a result, yielding the content of an [`Ok`]. /// -- 2.44.0