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/libstd/path/posix.rs | 13 +++++++++++-- src/libstd/path/windows.rs | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'src/libstd/path') diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 588f724134e..72c41f2399e 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -19,6 +19,7 @@ use iter::{AdditiveIterator, Extend}; use iter::{Iterator, IteratorExt, Map}; use marker::Sized; use option::Option::{self, Some, None}; +use result::Result::{self, Ok, Err}; use slice::{AsSlice, Split, SliceExt, SliceConcatExt}; use str::{self, FromStr, StrExt}; use vec::Vec; @@ -86,11 +87,19 @@ impl Ord for Path { } impl FromStr for Path { - fn from_str(s: &str) -> Option { - Path::new_opt(s) + type Err = ParsePathError; + fn from_str(s: &str) -> Result { + match Path::new_opt(s) { + Some(p) => Ok(p), + None => Err(ParsePathError), + } } } +/// Valuelue indicating that a path could not be parsed from a string. +#[derive(Show, Clone, PartialEq, Copy)] +pub struct ParsePathError; + impl hash::Hash for Path { #[inline] fn hash(&self, state: &mut S) { diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 88db27013ac..f3289492390 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -27,6 +27,7 @@ use mem; use option::Option::{self, Some, None}; #[cfg(stage0)] use ops::FullRange; +use result::Result::{self, Ok, Err}; use slice::{SliceExt, SliceConcatExt}; use str::{SplitTerminator, FromStr, StrExt}; use string::{String, ToString}; @@ -115,11 +116,19 @@ impl Ord for Path { } impl FromStr for Path { - fn from_str(s: &str) -> Option { - Path::new_opt(s) + type Err = ParsePathError; + fn from_str(s: &str) -> Result { + match Path::new_opt(s) { + Some(p) => Ok(p), + None => Err(ParsePathError), + } } } +/// Value indicating that a path could not be parsed from a string. +#[derive(Show, Clone, PartialEq, Copy)] +pub struct ParsePathError; + impl hash::Hash for Path { #[cfg(not(test))] #[inline] -- cgit 1.4.1-3-g733a5