Skip to content

Commit

Permalink
Auto merge of rust-lang#40281 - jimmycuadra:try-from-from-str, r=aturon
Browse files Browse the repository at this point in the history
Rename TryFrom's associated type and implement str::parse using TryFrom.

Per discussion on the tracking issue, naming `TryFrom`'s associated type `Error` is generally more consistent with similar traits in the Rust ecosystem, and what people seem to assume it should be called. It also helps disambiguate from `Result::Err`, the most common "Err".

See rust-lang#33417 (comment).

`TryFrom<&str>` and `FromStr` are equivalent, so have the latter provide the former to ensure that. Using `TryFrom` in the implementation of `str::parse` means types that implement either trait can use it. When we're ready to stabilize `TryFrom`, we should update `FromStr` to
suggest implementing `TryFrom<&str>` instead for new code.

See rust-lang#33417 (comment)
and rust-lang#33417 (comment).

Refs rust-lang#33417.
  • Loading branch information
bors committed Mar 20, 2017
2 parents 6eb9960 + 2561dcd commit 6738cd4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ impl From<u8> for char {

#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<u32> for char {
type Err = CharTryFromError;
type Error = CharTryFromError;

#[inline]
fn try_from(i: u32) -> Result<Self, Self::Err> {
fn try_from(i: u32) -> Result<Self, Self::Error> {
if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) {
Err(CharTryFromError(()))
} else {
Expand Down
24 changes: 18 additions & 6 deletions src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#![stable(feature = "rust1", since = "1.0.0")]

use str::FromStr;

/// A cheap, reference-to-reference conversion.
///
/// `AsRef` is very similar to, but different than, [`Borrow`]. See
Expand Down Expand Up @@ -212,20 +214,20 @@ pub trait From<T>: Sized {
#[unstable(feature = "try_from", issue = "33417")]
pub trait TryInto<T>: Sized {
/// The type returned in the event of a conversion error.
type Err;
type Error;

/// Performs the conversion.
fn try_into(self) -> Result<T, Self::Err>;
fn try_into(self) -> Result<T, Self::Error>;
}

/// Attempt to construct `Self` via a conversion.
#[unstable(feature = "try_from", issue = "33417")]
pub trait TryFrom<T>: Sized {
/// The type returned in the event of a conversion error.
type Err;
type Error;

/// Performs the conversion.
fn try_from(value: T) -> Result<Self, Self::Err>;
fn try_from(value: T) -> Result<Self, Self::Error>;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -290,9 +292,9 @@ impl<T> From<T> for T {
// TryFrom implies TryInto
#[unstable(feature = "try_from", issue = "33417")]
impl<T, U> TryInto<U> for T where U: TryFrom<T> {
type Err = U::Err;
type Error = U::Error;

fn try_into(self) -> Result<U, U::Err> {
fn try_into(self) -> Result<U, U::Error> {
U::try_from(self)
}
}
Expand Down Expand Up @@ -322,3 +324,13 @@ impl AsRef<str> for str {
self
}
}

// FromStr implies TryFrom<&str>
#[unstable(feature = "try_from", issue = "33417")]
impl<'a, T> TryFrom<&'a str> for T where T: FromStr {
type Error = <T as FromStr>::Err;

fn try_from(s: &'a str) -> Result<T, Self::Error> {
FromStr::from_str(s)
}
}
6 changes: 3 additions & 3 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,7 @@ macro_rules! same_sign_try_from_int_impl {
($storage:ty, $target:ty, $($source:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<$source> for $target {
type Err = TryFromIntError;
type Error = TryFromIntError;

fn try_from(u: $source) -> Result<$target, TryFromIntError> {
let min = <$target as FromStrRadixHelper>::min_value() as $storage;
Expand Down Expand Up @@ -2623,7 +2623,7 @@ macro_rules! cross_sign_from_int_impl {
($unsigned:ty, $($signed:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<$unsigned> for $signed {
type Err = TryFromIntError;
type Error = TryFromIntError;

fn try_from(u: $unsigned) -> Result<$signed, TryFromIntError> {
let max = <$signed as FromStrRadixHelper>::max_value() as u128;
Expand All @@ -2637,7 +2637,7 @@ macro_rules! cross_sign_from_int_impl {

#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<$signed> for $unsigned {
type Err = TryFromIntError;
type Error = TryFromIntError;

fn try_from(u: $signed) -> Result<$unsigned, TryFromIntError> {
let max = <$unsigned as FromStrRadixHelper>::max_value() as u128;
Expand Down
7 changes: 5 additions & 2 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use self::pattern::Pattern;
use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};

use char;
use convert::TryFrom;
use fmt;
use iter::{Map, Cloned, FusedIterator};
use mem;
Expand Down Expand Up @@ -1782,7 +1783,7 @@ pub trait StrExt {
#[stable(feature = "core", since = "1.6.0")]
fn is_empty(&self) -> bool;
#[stable(feature = "core", since = "1.6.0")]
fn parse<T: FromStr>(&self) -> Result<T, T::Err>;
fn parse<'a, T: TryFrom<&'a str>>(&'a self) -> Result<T, T::Error>;
}

// truncate `&str` to length at most equal to `max`
Expand Down Expand Up @@ -2081,7 +2082,9 @@ impl StrExt for str {
fn is_empty(&self) -> bool { self.len() == 0 }

#[inline]
fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }
fn parse<'a, T>(&'a self) -> Result<T, T::Error> where T: TryFrom<&'a str> {
T::try_from(self)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit 6738cd4

Please sign in to comment.