Auto merge of #40281 - jimmycuadra:try-from-from-str, r=aturon
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 https://github.com/rust-lang/rust/issues/33417#issuecomment-269108968. `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 https://github.com/rust-lang/rust/issues/33417#issuecomment-277175994 and https://github.com/rust-lang/rust/issues/33417#issuecomment-277253827. Refs #33417.
This commit is contained in:
commit
6738cd4d47
@ -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 {
|
||||
|
@ -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
|
||||
@ -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>;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
|
@ -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;
|
||||
@ -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`
|
||||
@ -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")]
|
||||
|
Loading…
Reference in New Issue
Block a user