]> git.lizzy.rs Git - rust.git/blob - src/libcore/error.rs
rollup merge of #20642: michaelwoerister/sane-source-locations-pt1
[rust.git] / src / libcore / error.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Traits for working with Errors.
12 //!
13 //! # The `Error` trait
14 //!
15 //! `Error` is a trait representing the basic expectations for error values,
16 //! i.e. values of type `E` in `Result<T, E>`. At a minimum, errors must provide
17 //! a description, but they may optionally provide additional detail (via
18 //! `Display`) and cause chain information:
19 //!
20 //! ```
21 //! use std::fmt::Display;
22 //!
23 //! trait Error: Display {
24 //!     fn description(&self) -> &str;
25 //!
26 //!     fn cause(&self) -> Option<&Error> { None }
27 //! }
28 //! ```
29 //!
30 //! The `cause` method is generally used when errors cross "abstraction
31 //! boundaries", i.e.  when a one module must report an error that is "caused"
32 //! by an error from a lower-level module. This setup makes it possible for the
33 //! high-level module to provide its own errors that do not commit to any
34 //! particular implementation, but also reveal some of its implementation for
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 //! use std::error::FromError;
52 //! use std::io::{File, IoError};
53 //! use std::os::{MemoryMap, MapError};
54 //! use std::path::Path;
55 //!
56 //! enum MyError {
57 //!     Io(IoError),
58 //!     Map(MapError)
59 //! }
60 //!
61 //! impl FromError<IoError> for MyError {
62 //!     fn from_error(err: IoError) -> MyError {
63 //!         MyError::Io(err)
64 //!     }
65 //! }
66 //!
67 //! impl FromError<MapError> for MyError {
68 //!     fn from_error(err: MapError) -> MyError {
69 //!         MyError::Map(err)
70 //!     }
71 //! }
72 //!
73 //! #[allow(unused_variables)]
74 //! fn open_and_map() -> Result<(), MyError> {
75 //!     let f = try!(File::open(&Path::new("foo.txt")));
76 //!     let m = try!(MemoryMap::new(0, &[]));
77 //!     // do something interesting here...
78 //!     Ok(())
79 //! }
80 //! ```
81
82 #![stable]
83
84 use prelude::*;
85 use fmt::Display;
86
87 /// Base functionality for all errors in Rust.
88 #[unstable = "the exact API of this trait may change"]
89 pub trait Error: Display {
90     /// A short description of the error; usually a static string.
91     fn description(&self) -> &str;
92
93     /// The lower-level cause of this error, if any.
94     fn cause(&self) -> Option<&Error> { None }
95 }
96
97 /// A trait for types that can be converted from a given error type `E`.
98 #[stable]
99 pub trait FromError<E> {
100     /// Perform the conversion.
101     fn from_error(err: E) -> Self;
102 }
103
104 // Any type is convertable from itself
105 #[stable]
106 impl<E> FromError<E> for E {
107     fn from_error(err: E) -> E {
108         err
109     }
110 }