|
| 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 | +//! pub trait Error: Send + Any { |
| 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::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 | +//! ``` |
| 75 | +
|
| 76 | +use any::{Any, AnyRefExt, AnyMutRefExt}; |
| 77 | +use mem::{transmute, transmute_copy}; |
| 78 | +use option::{Option, Some, None}; |
| 79 | +use raw::TraitObject; |
| 80 | +use intrinsics::TypeId; |
| 81 | +use kinds::Send; |
| 82 | +use string::String; |
| 83 | + |
| 84 | +/// Base functionality for all errors in Rust. |
| 85 | +pub trait Error: Send + Any { |
| 86 | + /// A short description of the error; usually a static string. |
| 87 | + fn description(&self) -> &str; |
| 88 | + |
| 89 | + /// A detailed description of the error, usually including dynamic information. |
| 90 | + fn detail(&self) -> Option<String> { None } |
| 91 | + |
| 92 | + /// The lower-level cause of this error, if any. |
| 93 | + fn cause(&self) -> Option<&Error> { None } |
| 94 | +} |
| 95 | + |
| 96 | +/// A trait for types that can be converted from a given error type `E`. |
| 97 | +pub trait FromError<E> { |
| 98 | + /// Perform the conversion. |
| 99 | + fn from_error(err: E) -> Self; |
| 100 | +} |
| 101 | + |
| 102 | +// Any type is convertable from itself |
| 103 | +impl<E> FromError<E> for E { |
| 104 | + fn from_error(err: E) -> E { |
| 105 | + err |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Note: the definitions below are copied from core::any, and should be unified |
| 110 | +// as soon as possible. |
| 111 | + |
| 112 | +impl<'a> AnyRefExt<'a> for &'a Error { |
| 113 | + #[inline] |
| 114 | + fn is<T: 'static>(self) -> bool { |
| 115 | + // Get TypeId of the type this function is instantiated with |
| 116 | + let t = TypeId::of::<T>(); |
| 117 | + |
| 118 | + // Get TypeId of the type in the trait object |
| 119 | + let boxed = self.get_type_id(); |
| 120 | + |
| 121 | + // Compare both TypeIds on equality |
| 122 | + t == boxed |
| 123 | + } |
| 124 | + |
| 125 | + #[inline] |
| 126 | + fn downcast_ref<T: 'static>(self) -> Option<&'a T> { |
| 127 | + if self.is::<T>() { |
| 128 | + unsafe { |
| 129 | + // Get the raw representation of the trait object |
| 130 | + let to: TraitObject = transmute_copy(&self); |
| 131 | + |
| 132 | + // Extract the data pointer |
| 133 | + Some(transmute(to.data)) |
| 134 | + } |
| 135 | + } else { |
| 136 | + None |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl<'a> AnyMutRefExt<'a> for &'a mut Error { |
| 142 | + #[inline] |
| 143 | + fn downcast_mut<T: 'static>(self) -> Option<&'a mut T> { |
| 144 | + if self.is::<T>() { |
| 145 | + unsafe { |
| 146 | + // Get the raw representation of the trait object |
| 147 | + let to: TraitObject = transmute_copy(&self); |
| 148 | + |
| 149 | + // Extract the data pointer |
| 150 | + Some(transmute(to.data)) |
| 151 | + } |
| 152 | + } else { |
| 153 | + None |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments