Skip to content

Commit c5a9e81

Browse files
committed
Incorporate FromError into try!
1 parent f95111f commit c5a9e81

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/libstd/error.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@
4646
//! The main use of this trait is in the `try!` macro, which uses it to
4747
//! automatically convert a given error to the error specified in a function's
4848
//! 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+
//! ```
4975
5076
use any::{Any, AnyRefExt, AnyMutRefExt};
5177
use mem::{transmute, transmute_copy};
@@ -80,13 +106,6 @@ impl<E> FromError<E> for E {
80106
}
81107
}
82108

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-
90109
// Note: the definitions below are copied from core::any, and should be unified
91110
// as soon as possible.
92111

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ mod std {
258258
pub use hash;
259259

260260
pub use comm; // used for select!()
261+
pub use error; // used for try!()
261262
pub use fmt; // used for any formatting strings
262263
pub use io; // used for println!()
263264
pub use local_data; // used for local_data_key!()

src/libstd/macros.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,13 @@ macro_rules! local_data_key(
317317
/// error if the value of the expression is `Err`. For more information, see
318318
/// `std::io`.
319319
#[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+
})
322327
)
323328

324329
/// Create a `std::vec::Vec` containing the arguments.

0 commit comments

Comments
 (0)