Skip to content

Commit e03e290

Browse files
authored
Rollup merge of rust-lang#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 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.
2 parents df3ab37 + 2561dcd commit e03e290

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

src/libcore/char.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ impl From<u8> for char {
209209

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

214214
#[inline]
215-
fn try_from(i: u32) -> Result<Self, Self::Err> {
215+
fn try_from(i: u32) -> Result<Self, Self::Error> {
216216
if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) {
217217
Err(CharTryFromError(()))
218218
} else {

src/libcore/convert.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
4949
#![stable(feature = "rust1", since = "1.0.0")]
5050

51+
use str::FromStr;
52+
5153
/// A cheap, reference-to-reference conversion.
5254
///
5355
/// `AsRef` is very similar to, but different than, [`Borrow`]. See
@@ -212,20 +214,20 @@ pub trait From<T>: Sized {
212214
#[unstable(feature = "try_from", issue = "33417")]
213215
pub trait TryInto<T>: Sized {
214216
/// The type returned in the event of a conversion error.
215-
type Err;
217+
type Error;
216218

217219
/// Performs the conversion.
218-
fn try_into(self) -> Result<T, Self::Err>;
220+
fn try_into(self) -> Result<T, Self::Error>;
219221
}
220222

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

227229
/// Performs the conversion.
228-
fn try_from(value: T) -> Result<Self, Self::Err>;
230+
fn try_from(value: T) -> Result<Self, Self::Error>;
229231
}
230232

231233
////////////////////////////////////////////////////////////////////////////////
@@ -290,9 +292,9 @@ impl<T> From<T> for T {
290292
// TryFrom implies TryInto
291293
#[unstable(feature = "try_from", issue = "33417")]
292294
impl<T, U> TryInto<U> for T where U: TryFrom<T> {
293-
type Err = U::Err;
295+
type Error = U::Error;
294296

295-
fn try_into(self) -> Result<U, U::Err> {
297+
fn try_into(self) -> Result<U, U::Error> {
296298
U::try_from(self)
297299
}
298300
}
@@ -322,3 +324,13 @@ impl AsRef<str> for str {
322324
self
323325
}
324326
}
327+
328+
// FromStr implies TryFrom<&str>
329+
#[unstable(feature = "try_from", issue = "33417")]
330+
impl<'a, T> TryFrom<&'a str> for T where T: FromStr {
331+
type Error = <T as FromStr>::Err;
332+
333+
fn try_from(s: &'a str) -> Result<T, Self::Error> {
334+
FromStr::from_str(s)
335+
}
336+
}

src/libcore/num/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2591,7 +2591,7 @@ macro_rules! same_sign_try_from_int_impl {
25912591
($storage:ty, $target:ty, $($source:ty),*) => {$(
25922592
#[unstable(feature = "try_from", issue = "33417")]
25932593
impl TryFrom<$source> for $target {
2594-
type Err = TryFromIntError;
2594+
type Error = TryFromIntError;
25952595

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

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

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

26422642
fn try_from(u: $signed) -> Result<$unsigned, TryFromIntError> {
26432643
let max = <$unsigned as FromStrRadixHelper>::max_value() as u128;

src/libcore/str/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use self::pattern::Pattern;
1818
use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
1919

2020
use char;
21+
use convert::TryFrom;
2122
use fmt;
2223
use iter::{Map, Cloned, FusedIterator};
2324
use mem;
@@ -1782,7 +1783,7 @@ pub trait StrExt {
17821783
#[stable(feature = "core", since = "1.6.0")]
17831784
fn is_empty(&self) -> bool;
17841785
#[stable(feature = "core", since = "1.6.0")]
1785-
fn parse<T: FromStr>(&self) -> Result<T, T::Err>;
1786+
fn parse<'a, T: TryFrom<&'a str>>(&'a self) -> Result<T, T::Error>;
17861787
}
17871788

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

20832084
#[inline]
2084-
fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }
2085+
fn parse<'a, T>(&'a self) -> Result<T, T::Error> where T: TryFrom<&'a str> {
2086+
T::try_from(self)
2087+
}
20852088
}
20862089

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

0 commit comments

Comments
 (0)