From 0cdde6e5e015ee6f6d9381ab624a312af7c9b069 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 27 Jan 2015 22:52:32 -0800 Subject: std: Stabilize FromStr and parse This commits adds an associated type to the `FromStr` trait representing an error payload for parses which do not succeed. The previous return value, `Option` did not allow for this form of payload. After the associated type was added, the following attributes were applied: * `FromStr` is now stable * `FromStr::Err` is now stable * `FromStr::from_str` is now stable * `StrExt::parse` is now stable * `FromStr for bool` is now stable * `FromStr for $float` is now stable * `FromStr for $integral` is now stable * Errors returned from stable `FromStr` implementations are stable * Errors implement `Display` and `Error` (both impl blocks being `#[stable]`) Closes #15138 --- src/libcore/str/mod.rs | 53 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'src/libcore/str') diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 8495a03747e..d08a2d3d77e 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -108,37 +108,62 @@ macro_rules! delegate_iter { /// A trait to abstract the idea of creating a new instance of a type from a /// string. -// FIXME(#17307): there should be an `E` associated type for a `Result` return -#[unstable(feature = "core", - reason = "will return a Result once associated types are working")] +#[stable(feature = "rust1", since = "1.0.0")] pub trait FromStr { + /// The associated error which can be returned from parsing. + #[stable(feature = "rust1", since = "1.0.0")] + type Err; + /// Parses a string `s` to return an optional value of this type. If the /// string is ill-formatted, the None is returned. - fn from_str(s: &str) -> Option; + #[stable(feature = "rust1", since = "1.0.0")] + fn from_str(s: &str) -> Result; } +#[stable(feature = "rust1", since = "1.0.0")] impl FromStr for bool { + type Err = ParseBoolError; + /// Parse a `bool` from a string. /// - /// Yields an `Option`, because `s` may or may not actually be parseable. + /// Yields an `Option`, because `s` may or may not actually be + /// parseable. /// /// # Examples /// /// ```rust - /// assert_eq!("true".parse(), Some(true)); - /// assert_eq!("false".parse(), Some(false)); - /// assert_eq!("not even a boolean".parse::(), None); + /// assert_eq!("true".parse(), Ok(true)); + /// assert_eq!("false".parse(), Ok(false)); + /// assert!("not even a boolean".parse::().is_err()); /// ``` #[inline] - fn from_str(s: &str) -> Option { + fn from_str(s: &str) -> Result { match s { - "true" => Some(true), - "false" => Some(false), - _ => None, + "true" => Ok(true), + "false" => Ok(false), + _ => Err(ParseBoolError { _priv: () }), } } } +/// An error returned when parsing a `bool` from a string fails. +#[derive(Show, Clone, PartialEq)] +#[allow(missing_copy_implementations)] +#[stable(feature = "rust1", since = "1.0.0")] +pub struct ParseBoolError { _priv: () } + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Display for ParseBoolError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + "provided string was not `true` or `false`".fmt(f) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl Error for ParseBoolError { + fn description(&self) -> &str { "failed to parse bool" } +} + /* Section: Creating a string */ @@ -1355,7 +1380,7 @@ pub trait StrExt { fn as_ptr(&self) -> *const u8; fn len(&self) -> uint; fn is_empty(&self) -> bool; - fn parse(&self) -> Option; + fn parse(&self) -> Result; } #[inline(never)] @@ -1670,7 +1695,7 @@ impl StrExt for str { fn is_empty(&self) -> bool { self.len() == 0 } #[inline] - fn parse(&self) -> Option { FromStr::from_str(self) } + fn parse(&self) -> Result { FromStr::from_str(self) } } /// Pluck a code point out of a UTF-8-like byte slice and return the -- cgit 1.4.1-3-g733a5