Skip to content

Commit e17f4fc

Browse files
committed
convert: remove FromError, use From<E> instead
This removes the FromError trait, since it can now be expressed using the new convert::Into trait. All implementations of FromError<E> where changed to From<E>, and `try!` was changed to use From::from instead. Because this removes FromError, it is a breaking change, but fixing it simply requires changing the words `FromError` to `From`, and `from_error` to `from`. [breaking-change]
1 parent 9de34a8 commit e17f4fc

File tree

10 files changed

+30
-50
lines changed

10 files changed

+30
-50
lines changed

src/liballoc/boxed.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use core::prelude::*;
5151
use core::any::Any;
5252
use core::cmp::Ordering;
5353
use core::default::Default;
54-
use core::error::{Error, FromError};
54+
use core::error::Error;
5555
use core::fmt;
5656
use core::hash::{self, Hash};
5757
use core::mem;
@@ -322,8 +322,8 @@ impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
322322
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
323323

324324
#[stable(feature = "rust1", since = "1.0.0")]
325-
impl<'a, E: Error + 'a> FromError<E> for Box<Error + 'a> {
326-
fn from_error(err: E) -> Box<Error + 'a> {
325+
impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
326+
fn from(err: E) -> Box<Error + 'a> {
327327
Box::new(err)
328328
}
329329
}

src/libcore/convert.rs

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
9999
// }
100100
// }
101101

102+
// From itself is always itself
103+
impl<T> From<T> for T {
104+
fn from(t: T) -> T {
105+
t
106+
}
107+
}
108+
102109
// From implies Into
103110
impl<T, U> Into<U> for T where U: From<T> {
104111
fn into(self) -> U {

src/libcore/error.rs

+4-31
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@
3434
//! particular implementation, but also reveal some of its implementation for
3535
//! debugging via `cause` chains.
3636
//!
37-
//! # The `FromError` trait
38-
//!
39-
//! `FromError` is a simple trait that expresses conversions between different
40-
//! error types. To provide maximum flexibility, it does not require either of
41-
//! the types to actually implement the `Error` trait, although this will be the
42-
//! common case.
43-
//!
44-
//! The main use of this trait is in the `try!` macro, which uses it to
45-
//! automatically convert a given error to the error specified in a function's
46-
//! return type.
47-
//!
4837
//! For example,
4938
//!
5039
//! ```
@@ -59,14 +48,14 @@
5948
//! Map(MapError)
6049
//! }
6150
//!
62-
//! impl FromError<IoError> for MyError {
63-
//! fn from_error(err: IoError) -> MyError {
51+
//! impl From<IoError> for MyError {
52+
//! fn from(err: IoError) -> MyError {
6453
//! MyError::Io(err)
6554
//! }
6655
//! }
6756
//!
68-
//! impl FromError<MapError> for MyError {
69-
//! fn from_error(err: MapError) -> MyError {
57+
//! impl From<MapError> for MyError {
58+
//! fn from(err: MapError) -> MyError {
7059
//! MyError::Map(err)
7160
//! }
7261
//! }
@@ -100,19 +89,3 @@ pub trait Error: Debug + Display {
10089
#[stable(feature = "rust1", since = "1.0.0")]
10190
fn cause(&self) -> Option<&Error> { None }
10291
}
103-
104-
/// A trait for types that can be converted from a given error type `E`.
105-
#[stable(feature = "rust1", since = "1.0.0")]
106-
pub trait FromError<E> {
107-
/// Perform the conversion.
108-
#[stable(feature = "rust1", since = "1.0.0")]
109-
fn from_error(err: E) -> Self;
110-
}
111-
112-
// Any type is convertable from itself
113-
#[stable(feature = "rust1", since = "1.0.0")]
114-
impl<E> FromError<E> for E {
115-
fn from_error(err: E) -> E {
116-
err
117-
}
118-
}

src/libcore/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ macro_rules! debug_assert_eq {
156156

157157
/// Short circuiting evaluation on Err
158158
///
159-
/// `libstd` contains a more general `try!` macro that uses `FromError`.
159+
/// `libstd` contains a more general `try!` macro that uses `From<E>`.
160160
#[macro_export]
161161
macro_rules! try {
162162
($e:expr) => ({

src/libserialize/json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ impl std::error::Error for EncoderError {
365365
fn description(&self) -> &str { "encoder error" }
366366
}
367367

368-
impl std::error::FromError<fmt::Error> for EncoderError {
369-
fn from_error(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) }
368+
impl From<fmt::Error> for EncoderError {
369+
fn from(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) }
370370
}
371371

372372
pub type EncodeResult = Result<(), EncoderError>;

src/libstd/ffi/c_str.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use convert::Into;
1414
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
15-
use error::{Error, FromError};
15+
use error::Error;
1616
use fmt;
1717
use io;
1818
use iter::Iterator;
@@ -298,17 +298,17 @@ impl fmt::Display for NulError {
298298
}
299299

300300
#[stable(feature = "rust1", since = "1.0.0")]
301-
impl FromError<NulError> for io::Error {
302-
fn from_error(_: NulError) -> io::Error {
301+
impl From<NulError> for io::Error {
302+
fn from(_: NulError) -> io::Error {
303303
io::Error::new(io::ErrorKind::InvalidInput,
304304
"data provided contains a nul byte", None)
305305
}
306306
}
307307

308308
#[stable(feature = "rust1", since = "1.0.0")]
309309
#[allow(deprecated)]
310-
impl FromError<NulError> for old_io::IoError {
311-
fn from_error(_: NulError) -> old_io::IoError {
310+
impl From<NulError> for old_io::IoError {
311+
fn from(_: NulError) -> old_io::IoError {
312312
old_io::IoError {
313313
kind: old_io::IoErrorKind::InvalidInput,
314314
desc: "data provided contains a nul byte",

src/libstd/io/buffered.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use prelude::v1::*;
1616
use io::prelude::*;
1717

1818
use cmp;
19-
use error::{self, FromError};
19+
use error;
2020
use fmt;
2121
use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind};
2222
use ptr;
@@ -264,8 +264,8 @@ impl<W> IntoInnerError<W> {
264264
}
265265

266266
#[stable(feature = "rust1", since = "1.0.0")]
267-
impl<W> FromError<IntoInnerError<W>> for Error {
268-
fn from_error(iie: IntoInnerError<W>) -> Error { iie.1 }
267+
impl<W> From<IntoInnerError<W>> for Error {
268+
fn from(iie: IntoInnerError<W>) -> Error { iie.1 }
269269
}
270270

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

src/libstd/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ macro_rules! try {
9797
($expr:expr) => (match $expr {
9898
$crate::result::Result::Ok(val) => val,
9999
$crate::result::Result::Err(err) => {
100-
return $crate::result::Result::Err($crate::error::FromError::from_error(err))
100+
return $crate::result::Result::Err($crate::convert::From::from(err))
101101
}
102102
})
103103
}

src/libstd/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use boxed::Box;
4040
use clone::Clone;
4141
use convert::From;
4242
use env;
43-
use error::{FromError, Error};
43+
use error::Error;
4444
use ffi::{OsString, OsStr};
4545
use fmt;
4646
use iter::Iterator;

src/libstd/sync/poison.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use prelude::v1::*;
1212

1313
use cell::UnsafeCell;
14-
use error::{Error, FromError};
14+
use error::{Error};
1515
use fmt;
1616
use thread;
1717

@@ -144,8 +144,8 @@ impl<T> PoisonError<T> {
144144
pub fn get_mut(&mut self) -> &mut T { &mut self.guard }
145145
}
146146

147-
impl<T> FromError<PoisonError<T>> for TryLockError<T> {
148-
fn from_error(err: PoisonError<T>) -> TryLockError<T> {
147+
impl<T> From<PoisonError<T>> for TryLockError<T> {
148+
fn from(err: PoisonError<T>) -> TryLockError<T> {
149149
TryLockError::Poisoned(err)
150150
}
151151
}

0 commit comments

Comments
 (0)