]> git.lizzy.rs Git - rust.git/commitdiff
convert: remove FromError, use From<E> instead
authorSean McArthur <sean.monstar@gmail.com>
Tue, 31 Mar 2015 00:56:48 +0000 (17:56 -0700)
committerSean McArthur <sean.monstar@gmail.com>
Tue, 31 Mar 2015 01:08:58 +0000 (18:08 -0700)
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]

src/liballoc/boxed.rs
src/libcore/convert.rs
src/libcore/error.rs
src/libcore/macros.rs
src/libserialize/json.rs
src/libstd/ffi/c_str.rs
src/libstd/io/buffered.rs
src/libstd/macros.rs
src/libstd/os.rs
src/libstd/sync/poison.rs

index f9bd0ab2f1e0fdbe1e3cb88fcb61413d1e21b8b6..94a497dfe85f3f04efa965aafeb15b80ebeaf31e 100644 (file)
@@ -51,7 +51,7 @@
 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 @@ fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
 impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<'a, E: Error + 'a> FromError<E> for Box<Error + 'a> {
-    fn from_error(err: E) -> Box<Error + 'a> {
+impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
+    fn from(err: E) -> Box<Error + 'a> {
         Box::new(err)
     }
 }
index 21f9b1f5513aaaf7f7c8d5184616c3f3552371b8..3680daa2d5ece50ba7b95060050604f1c74d3e88 100644 (file)
@@ -99,6 +99,13 @@ fn as_mut(&mut self) -> &mut U {
 //     }
 // }
 
+// From itself is always itself
+impl<T> From<T> for T {
+    fn from(t: T) -> T {
+        t
+    }
+}
+
 // From implies Into
 impl<T, U> Into<U> for T where U: From<T> {
     fn into(self) -> U {
index 51f3369a75bd3c0cd1cec4b9cae32f82bbbb19f6..73ec19f1a14914fb027e7b43c9c0c8f56f5c8266 100644 (file)
 //! 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,
 //!
 //! ```
 //!     Map(MapError)
 //! }
 //!
-//! impl FromError<IoError> for MyError {
-//!     fn from_error(err: IoError) -> MyError {
+//! impl From<IoError> for MyError {
+//!     fn from(err: IoError) -> MyError {
 //!         MyError::Io(err)
 //!     }
 //! }
 //!
-//! impl FromError<MapError> for MyError {
-//!     fn from_error(err: MapError) -> MyError {
+//! impl From<MapError> 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<E> {
-    /// 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<E> FromError<E> for E {
-    fn from_error(err: E) -> E {
-        err
-    }
-}
index d5a7c1d6b26472ac3fb49cadc6e0c764252593c8..19626aa505620f7261f22d1d3c83a3d51792fc58 100644 (file)
@@ -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<E>`.
 #[macro_export]
 macro_rules! try {
     ($e:expr) => ({
index f6f059f7210ffaea1b57809add1c349ea03e0ffd..cd7f66f50c865b07b035916fe761440a84f4917e 100644 (file)
@@ -365,8 +365,8 @@ impl std::error::Error for EncoderError {
     fn description(&self) -> &str { "encoder error" }
 }
 
-impl std::error::FromError<fmt::Error> for EncoderError {
-    fn from_error(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) }
+impl From<fmt::Error> for EncoderError {
+    fn from(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) }
 }
 
 pub type EncodeResult = Result<(), EncoderError>;
index a00f77080252c70292950b2ac17202478674be99..f5c7d1d18d5f705f7943a679cfbea4db12781f40 100644 (file)
@@ -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 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl FromError<NulError> for io::Error {
-    fn from_error(_: NulError) -> io::Error {
+impl From<NulError> 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 @@ fn from_error(_: NulError) -> io::Error {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 #[allow(deprecated)]
-impl FromError<NulError> for old_io::IoError {
-    fn from_error(_: NulError) -> old_io::IoError {
+impl From<NulError> 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",
index 98581fc43f89e978584db5a707d05aad051ac6f8..f03ed7a3dde55c16bdc8eb7587362a247268d8f6 100644 (file)
@@ -16,7 +16,7 @@
 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 @@ pub fn into_inner(self) -> W { self.0 }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<W> FromError<IntoInnerError<W>> for Error {
-    fn from_error(iie: IntoInnerError<W>) -> Error { iie.1 }
+impl<W> From<IntoInnerError<W>> for Error {
+    fn from(iie: IntoInnerError<W>) -> Error { iie.1 }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
index 52492a019a2988170e2e94f52457b438264c5958..8810681109359928db649ab37b17b11f74138be0 100644 (file)
@@ -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))
         }
     })
 }
index e19c734b8a3acc3646a8712c4ff83558d16389ea..9aebdbe9eececcb0f4e98cdab71080e3ae15f63b 100644 (file)
@@ -40,7 +40,7 @@
 use clone::Clone;
 use convert::From;
 use env;
-use error::{FromError, Error};
+use error::Error;
 use ffi::{OsString, OsStr};
 use fmt;
 use iter::Iterator;
index c07c83d37f48881b041966d9c9cfbf48d82f5d8a..cea2def30f1bcf552fca73791439f1c7cda6c2c0 100644 (file)
@@ -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 @@ pub fn get_ref(&self) -> &T { &self.guard }
     pub fn get_mut(&mut self) -> &mut T { &mut self.guard }
 }
 
-impl<T> FromError<PoisonError<T>> for TryLockError<T> {
-    fn from_error(err: PoisonError<T>) -> TryLockError<T> {
+impl<T> From<PoisonError<T>> for TryLockError<T> {
+    fn from(err: PoisonError<T>) -> TryLockError<T> {
         TryLockError::Poisoned(err)
     }
 }