File tree 3 files changed +34
-9
lines changed
3 files changed +34
-9
lines changed Original file line number Diff line number Diff line change 46
46
//! The main use of this trait is in the `try!` macro, which uses it to
47
47
//! automatically convert a given error to the error specified in a function's
48
48
//! return type.
49
+ //!
50
+ //! For example,
51
+ //!
52
+ //! ```
53
+ //! use std::io::IoError;
54
+ //! use std::os::MapError;
55
+ //!
56
+ //! impl FromError<IoError> for Box<Error> {
57
+ //! fn from_error(err: IoError) -> Box<Error> {
58
+ //! box err
59
+ //! }
60
+ //! }
61
+ //!
62
+ //! impl FromError<MapError> for Box<Error> {
63
+ //! fn from_error(err: MapError) -> Box<Error> {
64
+ //! box err
65
+ //! }
66
+ //! }
67
+ //!
68
+ //! #[allow(unused_variables)]
69
+ //! fn open_and_map() -> Box<Error> {
70
+ //! let f = try!(io::File::open("foo.txt"));
71
+ //! let m = try!(os::MemoryMap::new(0, &[]));
72
+ //! // do something interesting here...
73
+ //! }
74
+ //! ```
49
75
50
76
use any:: { Any , AnyRefExt , AnyMutRefExt } ;
51
77
use mem:: { transmute, transmute_copy} ;
@@ -80,13 +106,6 @@ impl<E> FromError<E> for E {
80
106
}
81
107
}
82
108
83
- // FIXME (#https://github.com/rust-lang/rust/pull/17669/): Add this once multidispatch lands
84
- // impl<E: Error> FromError<E> for Box<Error> {
85
- // fn from_err(err: E) -> Box<Error> {
86
- // box err as Box<Error>
87
- // }
88
- // }
89
-
90
109
// Note: the definitions below are copied from core::any, and should be unified
91
110
// as soon as possible.
92
111
Original file line number Diff line number Diff line change @@ -258,6 +258,7 @@ mod std {
258
258
pub use hash;
259
259
260
260
pub use comm; // used for select!()
261
+ pub use error; // used for try!()
261
262
pub use fmt; // used for any formatting strings
262
263
pub use io; // used for println!()
263
264
pub use local_data; // used for local_data_key!()
Original file line number Diff line number Diff line change @@ -317,8 +317,13 @@ macro_rules! local_data_key(
317
317
/// error if the value of the expression is `Err`. For more information, see
318
318
/// `std::io`.
319
319
#[ macro_export]
320
- macro_rules! try(
321
- ( $e: expr) => ( match $e { Ok ( e) => e, Err ( e) => return Err ( e) } )
320
+ macro_rules! try (
321
+ ( $expr: expr) => ( {
322
+ match $expr {
323
+ Ok ( val) => val,
324
+ Err ( err) => return Err ( :: std:: error:: FromError :: from_error( err) )
325
+ }
326
+ } )
322
327
)
323
328
324
329
/// Create a `std::vec::Vec` containing the arguments.
You can’t perform that action at this time.
0 commit comments