33
33
//! high-level module to provide its own errors that do not commit to any
34
34
//! particular implementation, but also reveal some of its implementation for
35
35
//! debugging via `cause` chains.
36
- //!
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
- //!
48
- //! For example,
49
- //!
50
- //! ```
51
- //! #![feature(core)]
52
- //! use std::error::FromError;
53
- //! use std::{io, str};
54
- //! use std::fs::File;
55
- //!
56
- //! enum MyError {
57
- //! Io(io::Error),
58
- //! Utf8(str::Utf8Error),
59
- //! }
60
- //!
61
- //! impl FromError<io::Error> for MyError {
62
- //! fn from_error(err: io::Error) -> MyError { MyError::Io(err) }
63
- //! }
64
- //!
65
- //! impl FromError<str::Utf8Error> for MyError {
66
- //! fn from_error(err: str::Utf8Error) -> MyError { MyError::Utf8(err) }
67
- //! }
68
- //!
69
- //! #[allow(unused_variables)]
70
- //! fn open_and_map() -> Result<(), MyError> {
71
- //! let b = b"foo.txt";
72
- //! let s = try!(str::from_utf8(b));
73
- //! let f = try!(File::open(s));
74
- //!
75
- //! // do something interesting here...
76
- //! Ok(())
77
- //! }
78
- //! ```
79
36
80
37
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
81
38
@@ -97,19 +54,3 @@ pub trait Error: Debug + Display {
97
54
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
98
55
fn cause ( & self ) -> Option < & Error > { None }
99
56
}
100
-
101
- /// A trait for types that can be converted from a given error type `E`.
102
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
103
- pub trait FromError < E > {
104
- /// Perform the conversion.
105
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
106
- fn from_error ( err : E ) -> Self ;
107
- }
108
-
109
- // Any type is convertable from itself
110
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
111
- impl < E > FromError < E > for E {
112
- fn from_error ( err : E ) -> E {
113
- err
114
- }
115
- }
0 commit comments