From 1c2151b7f9b460cda4a028f7ad031d9dae3f5913 Mon Sep 17 00:00:00 2001 From: Devon Hollowood Date: Wed, 19 Oct 2016 22:08:49 -0700 Subject: [PATCH 1/4] Add `unwrap_or_default` method to `Result` --- src/libcore/result.rs | 33 +++++++++++++++++++++++++++++++++ src/libcoretest/result.rs | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 96845259299be..8964b29b3a62f 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -792,6 +792,39 @@ impl Result { } } +impl Result { + /// Returns the contained value or a default + /// + /// Consumes the `self` argument then, if `Ok`, returns the contained + /// value, otherwise if `Err`, returns the default value for that + /// type. + /// + /// # Examples + /// + /// Convert a string to an integer, turning poorly-formed strings + /// into 0 (the default value for integers). `parse` converts + /// a string to any other type that implements `FromStr`, returning an + /// `Err` on error. + /// + /// ``` + /// let good_year_from_input = "1909"; + /// let bad_year_from_input = "190blarg"; + /// let good_year = good_year_from_input.parse().unwrap_or_default(); + /// let bad_year = bad_year_from_input.parse().unwrap_or_default(); + /// + /// assert_eq!(1909, good_year); + /// assert_eq!(0, bad_year); + /// ``` + #[inline] + #[stable(feature = "rust1", since = "1.0.0")] + pub fn unwrap_or_default(self) -> T { + match self { + Ok(x) => x, + Err(_) => Default::default(), + } + } +} + // This is a separate function to reduce the code size of the methods #[inline(never)] #[cold] diff --git a/src/libcoretest/result.rs b/src/libcoretest/result.rs index 6e9f653dcd8ac..bc2cd8bbfc651 100644 --- a/src/libcoretest/result.rs +++ b/src/libcoretest/result.rs @@ -183,3 +183,9 @@ pub fn test_iter_mut() { } assert_eq!(err, Err("error")); } + +#[test] +pub fn test_unwrap_or_default() { + assert_eq!(op1().unwrap_or_default(), 666); + assert_eq!(op2().unwrap_or_default(), 0); +} From fb1ef4f42c0aefa3f92b3f81ad4f24bdcb0b1753 Mon Sep 17 00:00:00 2001 From: Devon Hollowood Date: Thu, 20 Oct 2016 23:50:33 -0700 Subject: [PATCH 2/4] Make `Result`'s `unwrap_or_default` unstable --- src/libcore/result.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 8964b29b3a62f..f4973b3a254ba 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -802,8 +802,8 @@ impl Result { /// # Examples /// /// Convert a string to an integer, turning poorly-formed strings - /// into 0 (the default value for integers). `parse` converts - /// a string to any other type that implements `FromStr`, returning an + /// into 0 (the default value for integers). [`parse`] converts + /// a string to any other type that implements [`FromStr`], returning an /// `Err` on error. /// /// ``` @@ -814,9 +814,12 @@ impl Result { /// /// assert_eq!(1909, good_year); /// assert_eq!(0, bad_year); + /// + /// [`parse`]: ../../std/primitive.str.html#method.parse + /// [`FromStr`]: ../../std/str/trait.FromStr.html /// ``` #[inline] - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "result_unwrap_or_default", issue = "0")] pub fn unwrap_or_default(self) -> T { match self { Ok(x) => x, From 095850594f9140ae9d25eb3801aa4442295b0a91 Mon Sep 17 00:00:00 2001 From: Devon Hollowood Date: Fri, 21 Oct 2016 00:48:39 -0700 Subject: [PATCH 3/4] Fix `test_unwrap_or_default` --- src/libcoretest/result.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcoretest/result.rs b/src/libcoretest/result.rs index bc2cd8bbfc651..f80cb8d520736 100644 --- a/src/libcoretest/result.rs +++ b/src/libcoretest/result.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(result_unwrap_or_default)] + fn op1() -> Result { Ok(666) } fn op2() -> Result { Err("sadface") } From 5d31a818df97e8a71545e5ba6de0627abb7837ee Mon Sep 17 00:00:00 2001 From: Devon Hollowood Date: Fri, 21 Oct 2016 14:29:42 -0700 Subject: [PATCH 4/4] Fix use of `result_unwrap_or_default` feature --- src/libcore/result.rs | 2 ++ src/libcoretest/lib.rs | 1 + src/libcoretest/result.rs | 2 -- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index f4973b3a254ba..e7768106aa6ad 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -807,6 +807,8 @@ impl Result { /// `Err` on error. /// /// ``` + /// #![feature(result_unwrap_or_default)] + /// /// let good_year_from_input = "1909"; /// let bad_year_from_input = "190blarg"; /// let good_year = good_year_from_input.parse().unwrap_or_default(); diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 590bf478aa723..cdbc214731806 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -34,6 +34,7 @@ #![feature(unique)] #![feature(iter_max_by)] #![feature(iter_min_by)] +#![feature(result_unwrap_or_default)] extern crate core; extern crate test; diff --git a/src/libcoretest/result.rs b/src/libcoretest/result.rs index f80cb8d520736..bc2cd8bbfc651 100644 --- a/src/libcoretest/result.rs +++ b/src/libcoretest/result.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(result_unwrap_or_default)] - fn op1() -> Result { Ok(666) } fn op2() -> Result { Err("sadface") }