From b8c60302e73796b98ec9a958740d492bac70acb9 Mon Sep 17 00:00:00 2001 From: Isaac van Bakel Date: Wed, 29 Aug 2018 02:06:22 +0100 Subject: [PATCH 1/6] Implemented map_or_else for Result --- src/libcore/result.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index ac908342655b6..cbf73df4efe69 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -470,6 +470,35 @@ impl Result { } } + /// Maps a `Result` to `T` by applying a function to a + /// contained [`Ok`] value, or a fallback function to a + /// contained [`Err`] value. + /// + /// This function can be used to unpack a successful result + /// while handling an error. + /// + /// [`Ok`]: enum.Result.html#variant.Ok + /// [`Err`]: enum.Result.html#variant.Err + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let k = 21; + /// + /// let x = Ok("foo"); + /// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 3); + /// + /// let x = Err("bar"); + /// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42); + /// ``` + #[inline] + #[stable(feature = "rust1", since = "1.30.0")] + pub fn map_or_else U, F: FnOnce(E) -> U>(self, fallback: F, map: M) -> U { + self.map(map).unwrap_or_else(fallback) + } + /// Maps a `Result` to `Result` by applying a function to a /// contained [`Err`] value, leaving an [`Ok`] value untouched. /// From 5eb679105fc4b6827ec64c7cfab12c652a01d6b0 Mon Sep 17 00:00:00 2001 From: Isaac van Bakel Date: Wed, 29 Aug 2018 13:08:52 +0100 Subject: [PATCH 2/6] Corrected feature name for map_or_else --- src/libcore/result.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index cbf73df4efe69..2493c63ab8631 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -494,7 +494,7 @@ impl Result { /// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42); /// ``` #[inline] - #[stable(feature = "rust1", since = "1.30.0")] + #[stable(feature = "result_map_or_else", since = "1.30.0")] pub fn map_or_else U, F: FnOnce(E) -> U>(self, fallback: F, map: M) -> U { self.map(map).unwrap_or_else(fallback) } From 71b16d8350d6f3dcc9b1d624de9332d99ea3acd0 Mon Sep 17 00:00:00 2001 From: Isaac van Bakel Date: Wed, 29 Aug 2018 14:58:27 +0100 Subject: [PATCH 3/6] Corrected bad typing in Result::map_or_else doc The error value now includes the type of the success. The success value now includes the type of the error. --- src/libcore/result.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 2493c63ab8631..0e10c41ece75f 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -487,10 +487,10 @@ impl Result { /// ``` /// let k = 21; /// - /// let x = Ok("foo"); + /// let x : Result<_, &str> = Ok("foo"); /// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 3); /// - /// let x = Err("bar"); + /// let x : Result<&str, _> = Err("bar"); /// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42); /// ``` #[inline] From 3eda9058cfd6f42a0b2b46ad2605f0dc52981a77 Mon Sep 17 00:00:00 2001 From: Isaac van Bakel Date: Wed, 29 Aug 2018 22:51:54 +0100 Subject: [PATCH 4/6] Corrected feature status of Result::map_or_else map_or_else is now correctly labelled unstable and points to the tracking issue on rust-lang/rust --- src/libcore/result.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 0e10c41ece75f..22a7cedc189ba 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -494,7 +494,7 @@ impl Result { /// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42); /// ``` #[inline] - #[stable(feature = "result_map_or_else", since = "1.30.0")] + #[unstable(feature = "result_map_or_else", issue = "53268")] pub fn map_or_else U, F: FnOnce(E) -> U>(self, fallback: F, map: M) -> U { self.map(map).unwrap_or_else(fallback) } From 6ff4f7946fc6c8dc6a199ef297bf0b4bc8aeafe0 Mon Sep 17 00:00:00 2001 From: Isaac van Bakel Date: Wed, 29 Aug 2018 22:55:21 +0100 Subject: [PATCH 5/6] Corrected type variable output T -> U in Result::map_or_else --- src/libcore/result.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 22a7cedc189ba..04e3b502ed8b9 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -470,7 +470,7 @@ impl Result { } } - /// Maps a `Result` to `T` by applying a function to a + /// Maps a `Result` to `U` by applying a function to a /// contained [`Ok`] value, or a fallback function to a /// contained [`Err`] value. /// From 79408c3f32e605ac6def31cb2849f7f4017c9084 Mon Sep 17 00:00:00 2001 From: Isaac van Bakel Date: Thu, 30 Aug 2018 00:51:09 +0100 Subject: [PATCH 6/6] Added feature attribute to example code in map_or_else doc --- src/libcore/result.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 04e3b502ed8b9..7443320e377ed 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -485,6 +485,7 @@ impl Result { /// Basic usage: /// /// ``` + /// #![feature(result_map_or_else)] /// let k = 21; /// /// let x : Result<_, &str> = Ok("foo");