|
| 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 and cause |
| 18 | +//! chain information: |
| 19 | +//! |
| 20 | +//! ``` |
| 21 | +//! trait Error: Send { |
| 22 | +//! fn description(&self) -> &str; |
| 23 | +//! |
| 24 | +//! fn detail(&self) -> Option<String> { None } |
| 25 | +//! fn cause(&self) -> Option<&Error> { None } |
| 26 | +//! } |
| 27 | +//! ``` |
| 28 | +//! |
| 29 | +//! The `cause` method is generally used when errors cross "abstraction |
| 30 | +//! boundaries", i.e. when a one module must report an error that is "caused" |
| 31 | +//! by an error from a lower-level module. This setup makes it possible for the |
| 32 | +//! high-level module to provide its own errors that do not commit to any |
| 33 | +//! particular implementation, but also reveal some of its implementation for |
| 34 | +//! debugging via `cause` chains. |
| 35 | +//! |
| 36 | +//! The trait inherits from `Any` to allow *downcasting*: converting from a |
| 37 | +//! trait object to a specific concrete type when applicable. |
| 38 | +//! |
| 39 | +//! # The `FromError` trait |
| 40 | +//! |
| 41 | +//! `FromError` is a simple trait that expresses conversions between different |
| 42 | +//! error types. To provide maximum flexibility, it does not require either of |
| 43 | +//! the types to actually implement the `Error` trait, although this will be the |
| 44 | +//! common case. |
| 45 | +//! |
| 46 | +//! The main use of this trait is in the `try!` macro, which uses it to |
| 47 | +//! automatically convert a given error to the error specified in a function's |
| 48 | +//! return type. |
| 49 | +//! |
| 50 | +//! For example, |
| 51 | +//! |
| 52 | +//! ``` |
| 53 | +//! use std::error::FromError; |
| 54 | +//! use std::io::{File, IoError}; |
| 55 | +//! use std::os::{MemoryMap, MapError}; |
| 56 | +//! use std::path::Path; |
| 57 | +//! |
| 58 | +//! enum MyError { |
| 59 | +//! Io(IoError), |
| 60 | +//! Map(MapError) |
| 61 | +//! } |
| 62 | +//! |
| 63 | +//! impl FromError<IoError> for MyError { |
| 64 | +//! fn from_error(err: IoError) -> MyError { |
| 65 | +//! Io(err) |
| 66 | +//! } |
| 67 | +//! } |
| 68 | +//! |
| 69 | +//! impl FromError<MapError> for MyError { |
| 70 | +//! fn from_error(err: MapError) -> MyError { |
| 71 | +//! Map(err) |
| 72 | +//! } |
| 73 | +//! } |
| 74 | +//! |
| 75 | +//! #[allow(unused_variables)] |
| 76 | +//! fn open_and_map() -> Result<(), MyError> { |
| 77 | +//! let f = try!(File::open(&Path::new("foo.txt"))); |
| 78 | +//! let m = try!(MemoryMap::new(0, &[])); |
| 79 | +//! // do something interesting here... |
| 80 | +//! Ok(()) |
| 81 | +//! } |
| 82 | +//! ``` |
| 83 | +
|
| 84 | +use option::{Option, None}; |
| 85 | +use kinds::Send; |
| 86 | +use string::String; |
| 87 | + |
| 88 | +/// Base functionality for all errors in Rust. |
| 89 | +pub trait Error: Send { |
| 90 | + /// A short description of the error; usually a static string. |
| 91 | + fn description(&self) -> &str; |
| 92 | + |
| 93 | + /// A detailed description of the error, usually including dynamic information. |
| 94 | + fn detail(&self) -> Option<String> { None } |
| 95 | + |
| 96 | + /// The lower-level cause of this error, if any. |
| 97 | + fn cause(&self) -> Option<&Error> { None } |
| 98 | +} |
| 99 | + |
| 100 | +/// A trait for types that can be converted from a given error type `E`. |
| 101 | +pub trait FromError<E> { |
| 102 | + /// Perform the conversion. |
| 103 | + fn from_error(err: E) -> Self; |
| 104 | +} |
| 105 | + |
| 106 | +// Any type is convertable from itself |
| 107 | +impl<E> FromError<E> for E { |
| 108 | + fn from_error(err: E) -> E { |
| 109 | + err |
| 110 | + } |
| 111 | +} |
0 commit comments