From e17f4fc1d4545f5c17b21805c5145b05495484ee Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 30 Mar 2015 17:56:48 -0700 Subject: [PATCH] convert: remove FromError, use From instead This removes the FromError trait, since it can now be expressed using the new convert::Into trait. All implementations of FromError where changed to From, 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] --- src/liballoc/boxed.rs | 6 +++--- src/libcore/convert.rs | 7 +++++++ src/libcore/error.rs | 35 ++++------------------------------- src/libcore/macros.rs | 2 +- src/libserialize/json.rs | 4 ++-- src/libstd/ffi/c_str.rs | 10 +++++----- src/libstd/io/buffered.rs | 6 +++--- src/libstd/macros.rs | 2 +- src/libstd/os.rs | 2 +- src/libstd/sync/poison.rs | 6 +++--- 10 files changed, 30 insertions(+), 50 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f9bd0ab2f1e..94a497dfe85 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -51,7 +51,7 @@ use core::prelude::*; use core::any::Any; use core::cmp::Ordering; use core::default::Default; -use core::error::{Error, FromError}; +use core::error::Error; use core::fmt; use core::hash::{self, Hash}; use core::mem; @@ -322,8 +322,8 @@ impl DoubleEndedIterator for Box { impl ExactSizeIterator for Box {} #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, E: Error + 'a> FromError for Box { - fn from_error(err: E) -> Box { +impl<'a, E: Error + 'a> From for Box { + fn from(err: E) -> Box { Box::new(err) } } diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 21f9b1f5513..3680daa2d5e 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -99,6 +99,13 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut for &'a mut T where T: AsMut { // } // } +// From itself is always itself +impl From for T { + fn from(t: T) -> T { + t + } +} + // From implies Into impl Into for T where U: From { fn into(self) -> U { diff --git a/src/libcore/error.rs b/src/libcore/error.rs index 51f3369a75b..73ec19f1a14 100644 --- a/src/libcore/error.rs +++ b/src/libcore/error.rs @@ -34,17 +34,6 @@ //! particular implementation, but also reveal some of its implementation for //! debugging via `cause` chains. //! -//! # The `FromError` trait -//! -//! `FromError` is a simple trait that expresses conversions between different -//! error types. To provide maximum flexibility, it does not require either of -//! the types to actually implement the `Error` trait, although this will be the -//! common case. -//! -//! The main use of this trait is in the `try!` macro, which uses it to -//! automatically convert a given error to the error specified in a function's -//! return type. -//! //! For example, //! //! ``` @@ -59,14 +48,14 @@ //! Map(MapError) //! } //! -//! impl FromError for MyError { -//! fn from_error(err: IoError) -> MyError { +//! impl From for MyError { +//! fn from(err: IoError) -> MyError { //! MyError::Io(err) //! } //! } //! -//! impl FromError for MyError { -//! fn from_error(err: MapError) -> MyError { +//! impl From for MyError { +//! fn from(err: MapError) -> MyError { //! MyError::Map(err) //! } //! } @@ -100,19 +89,3 @@ pub trait Error: Debug + Display { #[stable(feature = "rust1", since = "1.0.0")] fn cause(&self) -> Option<&Error> { None } } - -/// A trait for types that can be converted from a given error type `E`. -#[stable(feature = "rust1", since = "1.0.0")] -pub trait FromError { - /// Perform the conversion. - #[stable(feature = "rust1", since = "1.0.0")] - fn from_error(err: E) -> Self; -} - -// Any type is convertable from itself -#[stable(feature = "rust1", since = "1.0.0")] -impl FromError for E { - fn from_error(err: E) -> E { - err - } -} diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index d5a7c1d6b26..19626aa5056 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -156,7 +156,7 @@ macro_rules! debug_assert_eq { /// Short circuiting evaluation on Err /// -/// `libstd` contains a more general `try!` macro that uses `FromError`. +/// `libstd` contains a more general `try!` macro that uses `From`. #[macro_export] macro_rules! try { ($e:expr) => ({ diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index f6f059f7210..cd7f66f50c8 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -365,8 +365,8 @@ impl std::error::Error for EncoderError { fn description(&self) -> &str { "encoder error" } } -impl std::error::FromError for EncoderError { - fn from_error(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) } +impl From for EncoderError { + fn from(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) } } pub type EncodeResult = Result<(), EncoderError>; diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index a00f7708025..f5c7d1d18d5 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -12,7 +12,7 @@ use convert::Into; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; -use error::{Error, FromError}; +use error::Error; use fmt; use io; use iter::Iterator; @@ -298,8 +298,8 @@ impl fmt::Display for NulError { } #[stable(feature = "rust1", since = "1.0.0")] -impl FromError for io::Error { - fn from_error(_: NulError) -> io::Error { +impl From for io::Error { + fn from(_: NulError) -> io::Error { io::Error::new(io::ErrorKind::InvalidInput, "data provided contains a nul byte", None) } @@ -307,8 +307,8 @@ impl FromError for io::Error { #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated)] -impl FromError for old_io::IoError { - fn from_error(_: NulError) -> old_io::IoError { +impl From for old_io::IoError { + fn from(_: NulError) -> old_io::IoError { old_io::IoError { kind: old_io::IoErrorKind::InvalidInput, desc: "data provided contains a nul byte", diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 98581fc43f8..f03ed7a3dde 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -16,7 +16,7 @@ use prelude::v1::*; use io::prelude::*; use cmp; -use error::{self, FromError}; +use error; use fmt; use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind}; use ptr; @@ -264,8 +264,8 @@ impl IntoInnerError { } #[stable(feature = "rust1", since = "1.0.0")] -impl FromError> for Error { - fn from_error(iie: IntoInnerError) -> Error { iie.1 } +impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 52492a019a2..88106811093 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -97,7 +97,7 @@ macro_rules! try { ($expr:expr) => (match $expr { $crate::result::Result::Ok(val) => val, $crate::result::Result::Err(err) => { - return $crate::result::Result::Err($crate::error::FromError::from_error(err)) + return $crate::result::Result::Err($crate::convert::From::from(err)) } }) } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index e19c734b8a3..9aebdbe9eec 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -40,7 +40,7 @@ use boxed::Box; use clone::Clone; use convert::From; use env; -use error::{FromError, Error}; +use error::Error; use ffi::{OsString, OsStr}; use fmt; use iter::Iterator; diff --git a/src/libstd/sync/poison.rs b/src/libstd/sync/poison.rs index c07c83d37f4..cea2def30f1 100644 --- a/src/libstd/sync/poison.rs +++ b/src/libstd/sync/poison.rs @@ -11,7 +11,7 @@ use prelude::v1::*; use cell::UnsafeCell; -use error::{Error, FromError}; +use error::{Error}; use fmt; use thread; @@ -144,8 +144,8 @@ impl PoisonError { pub fn get_mut(&mut self) -> &mut T { &mut self.guard } } -impl FromError> for TryLockError { - fn from_error(err: PoisonError) -> TryLockError { +impl From> for TryLockError { + fn from(err: PoisonError) -> TryLockError { TryLockError::Poisoned(err) } }