2015-04-01 14:25:47 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Traits for working with Errors.
|
|
|
|
//!
|
|
|
|
//! # The `Error` trait
|
|
|
|
//!
|
|
|
|
//! `Error` is a trait representing the basic expectations for error values,
|
|
|
|
//! i.e. values of type `E` in `Result<T, E>`. At a minimum, errors must provide
|
|
|
|
//! a description, but they may optionally provide additional detail (via
|
|
|
|
//! `Display`) and cause chain information:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use std::fmt::Display;
|
|
|
|
//!
|
|
|
|
//! trait Error: Display {
|
|
|
|
//! fn description(&self) -> &str;
|
|
|
|
//!
|
|
|
|
//! fn cause(&self) -> Option<&Error> { None }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The `cause` method is generally used when errors cross "abstraction
|
|
|
|
//! boundaries", i.e. when a one module must report an error that is "caused"
|
|
|
|
//! by an error from a lower-level module. This setup makes it possible for the
|
|
|
|
//! high-level module to provide its own errors that do not commit to any
|
|
|
|
//! particular implementation, but also reveal some of its implementation for
|
|
|
|
//! debugging via `cause` chains.
|
|
|
|
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
|
|
|
// A note about crates and the facade:
|
|
|
|
//
|
|
|
|
// Originally, the `Error` trait was defined in libcore, and the impls
|
|
|
|
// were scattered about. However, coherence objected to this
|
|
|
|
// arrangement, because to create the blanket impls for `Box` required
|
|
|
|
// knowing that `&str: !Error`, and we have no means to deal with that
|
|
|
|
// sort of conflict just now. Therefore, for the time being, we have
|
|
|
|
// moved the `Error` trait into libstd. As we evolve a sol'n to the
|
|
|
|
// coherence challenge (e.g., specialization, neg impls, etc) we can
|
|
|
|
// reconsider what crate these items belong in.
|
|
|
|
|
2015-04-24 16:34:57 -05:00
|
|
|
use any::TypeId;
|
2015-06-10 21:33:04 -05:00
|
|
|
use boxed::Box;
|
2015-04-01 14:25:47 -05:00
|
|
|
use convert::From;
|
|
|
|
use fmt::{self, Debug, Display};
|
2015-04-24 16:34:57 -05:00
|
|
|
use marker::{Send, Sync, Reflect};
|
|
|
|
use mem::transmute;
|
2015-04-01 14:25:47 -05:00
|
|
|
use num;
|
2015-04-24 16:34:57 -05:00
|
|
|
use option::Option::{self, Some, None};
|
|
|
|
use result::Result::{self, Ok, Err};
|
|
|
|
use raw::TraitObject;
|
2015-04-01 14:25:47 -05:00
|
|
|
use str;
|
|
|
|
use string::{self, String};
|
|
|
|
|
|
|
|
/// Base functionality for all errors in Rust.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
pub trait Error: Debug + Display + Reflect {
|
2015-04-01 14:25:47 -05:00
|
|
|
/// A short description of the error.
|
|
|
|
///
|
|
|
|
/// The description should not contain newlines or sentence-ending
|
|
|
|
/// punctuation, to facilitate embedding in larger user-facing
|
|
|
|
/// strings.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
fn description(&self) -> &str;
|
|
|
|
|
|
|
|
/// The lower-level cause of this error, if any.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
fn cause(&self) -> Option<&Error> { None }
|
2015-04-24 16:34:57 -05:00
|
|
|
|
|
|
|
/// Get the `TypeId` of `self`
|
|
|
|
#[doc(hidden)]
|
2015-06-09 13:18:03 -05:00
|
|
|
#[unstable(feature = "error_type_id",
|
2015-08-13 12:12:38 -05:00
|
|
|
reason = "unclear whether to commit to this public implementation detail",
|
|
|
|
issue = "27745")]
|
2015-04-24 16:34:57 -05:00
|
|
|
fn type_id(&self) -> TypeId where Self: 'static {
|
|
|
|
TypeId::of::<Self>()
|
|
|
|
}
|
2015-04-01 14:25:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
|
|
|
|
fn from(err: E) -> Box<Error + 'a> {
|
|
|
|
Box::new(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-06 18:30:18 -05:00
|
|
|
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a> {
|
|
|
|
fn from(err: E) -> Box<Error + Send + Sync + 'a> {
|
2015-04-01 14:25:47 -05:00
|
|
|
Box::new(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-06 18:30:18 -05:00
|
|
|
impl From<String> for Box<Error + Send + Sync> {
|
|
|
|
fn from(err: String) -> Box<Error + Send + Sync> {
|
2015-04-01 14:25:47 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct StringError(String);
|
|
|
|
|
|
|
|
impl Error for StringError {
|
|
|
|
fn description(&self) -> &str { &self.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for StringError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
Display::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 15:10:25 -05:00
|
|
|
Box::new(StringError(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 06:21:08 -06:00
|
|
|
#[stable(feature = "string_box_error", since = "1.7.0")]
|
|
|
|
impl From<String> for Box<Error> {
|
|
|
|
fn from(str_err: String) -> Box<Error> {
|
|
|
|
let err1: Box<Error + Send + Sync> = From::from(str_err);
|
|
|
|
let err2: Box<Error> = err1;
|
|
|
|
err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 15:10:25 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-06 18:30:18 -05:00
|
|
|
impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a> {
|
|
|
|
fn from(err: &'b str) -> Box<Error + Send + Sync + 'a> {
|
2015-06-08 09:55:35 -05:00
|
|
|
From::from(String::from(err))
|
2015-04-01 14:25:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 17:38:25 -06:00
|
|
|
#[stable(feature = "string_box_error", since = "1.7.0")]
|
|
|
|
impl<'a> From<&'a str> for Box<Error> {
|
|
|
|
fn from(err: &'a str) -> Box<Error> {
|
|
|
|
From::from(String::from(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:25:47 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for str::ParseBoolError {
|
|
|
|
fn description(&self) -> &str { "failed to parse bool" }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for str::Utf8Error {
|
|
|
|
fn description(&self) -> &str {
|
2015-04-10 18:05:09 -05:00
|
|
|
"invalid utf-8: corrupt contents"
|
2015-04-01 14:25:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for num::ParseIntError {
|
|
|
|
fn description(&self) -> &str {
|
2015-06-11 11:56:13 -05:00
|
|
|
self.__description()
|
2015-04-01 14:25:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for num::ParseFloatError {
|
|
|
|
fn description(&self) -> &str {
|
2015-04-24 17:58:27 -05:00
|
|
|
self.__description()
|
2015-04-01 14:25:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for string::FromUtf8Error {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"invalid utf-8"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for string::FromUtf16Error {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"invalid utf-16"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 12:38:28 -06:00
|
|
|
#[stable(feature = "str_parse_error2", since = "1.8.0")]
|
|
|
|
impl Error for string::ParseError {
|
|
|
|
fn description(&self) -> &str {
|
2016-01-22 13:34:39 -06:00
|
|
|
match *self {}
|
2016-01-22 12:38:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 11:19:20 -06:00
|
|
|
#[stable(feature = "box_error", since = "1.7.0")]
|
|
|
|
impl<T: Error> Error for Box<T> {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
Error::description(&**self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
Error::cause(&**self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 16:34:57 -05:00
|
|
|
// copied from any.rs
|
|
|
|
impl Error + 'static {
|
|
|
|
/// Returns true if the boxed type is the same as `T`
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn is<T: Error + 'static>(&self) -> bool {
|
|
|
|
// Get TypeId of the type this function is instantiated with
|
|
|
|
let t = TypeId::of::<T>();
|
|
|
|
|
|
|
|
// Get TypeId of the type in the trait object
|
|
|
|
let boxed = self.type_id();
|
|
|
|
|
|
|
|
// Compare both TypeIds on equality
|
|
|
|
t == boxed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns some reference to the boxed value if it is of type `T`, or
|
|
|
|
/// `None` if it isn't.
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
|
|
|
if self.is::<T>() {
|
|
|
|
unsafe {
|
|
|
|
// Get the raw representation of the trait object
|
|
|
|
let to: TraitObject = transmute(self);
|
|
|
|
|
|
|
|
// Extract the data pointer
|
2015-07-23 20:04:55 -05:00
|
|
|
Some(&*(to.data as *const T))
|
2015-04-24 16:34:57 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns some mutable reference to the boxed value if it is of type `T`, or
|
|
|
|
/// `None` if it isn't.
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
|
|
|
if self.is::<T>() {
|
|
|
|
unsafe {
|
|
|
|
// Get the raw representation of the trait object
|
|
|
|
let to: TraitObject = transmute(self);
|
|
|
|
|
|
|
|
// Extract the data pointer
|
2015-07-23 20:04:55 -05:00
|
|
|
Some(&mut *(to.data as *const T as *mut T))
|
2015-04-24 16:34:57 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error + 'static + Send {
|
|
|
|
/// Forwards to the method defined on the type `Any`.
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn is<T: Error + 'static>(&self) -> bool {
|
|
|
|
<Error + 'static>::is::<T>(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forwards to the method defined on the type `Any`.
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
|
|
|
<Error + 'static>::downcast_ref::<T>(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forwards to the method defined on the type `Any`.
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
|
|
|
#[inline]
|
|
|
|
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
|
|
|
<Error + 'static>::downcast_mut::<T>(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error + 'static + Send + Sync {
|
|
|
|
/// Forwards to the method defined on the type `Any`.
|
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
|
|
|
#[inline]
|
|
|
|
pub fn is<T: Error + 'static>(&self) -> bool {
|
|
|
|
<Error + 'static>::is::<T>(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forwards to the method defined on the type `Any`.
|
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
|
|
|
#[inline]
|
|
|
|
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
|
|
|
<Error + 'static>::downcast_ref::<T>(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forwards to the method defined on the type `Any`.
|
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
|
|
|
<Error + 'static>::downcast_mut::<T>(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
#[inline]
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
/// Attempt to downcast the box to a concrete type.
|
|
|
|
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
|
|
|
|
if self.is::<T>() {
|
|
|
|
unsafe {
|
|
|
|
// Get the raw representation of the trait object
|
2015-06-10 21:33:04 -05:00
|
|
|
let raw = Box::into_raw(self);
|
2015-04-24 16:34:57 -05:00
|
|
|
let to: TraitObject =
|
|
|
|
transmute::<*mut Error, TraitObject>(raw);
|
|
|
|
|
|
|
|
// Extract the data pointer
|
|
|
|
Ok(Box::from_raw(to.data as *mut T))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error + Send {
|
|
|
|
#[inline]
|
2015-07-28 17:44:30 -05:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 16:34:57 -05:00
|
|
|
/// Attempt to downcast the box to a concrete type.
|
2015-07-28 17:44:30 -05:00
|
|
|
pub fn downcast<T: Error + 'static>(self: Box<Self>)
|
|
|
|
-> Result<Box<T>, Box<Error + Send>> {
|
2015-04-24 16:34:57 -05:00
|
|
|
let err: Box<Error> = self;
|
|
|
|
<Error>::downcast(err).map_err(|s| unsafe {
|
|
|
|
// reapply the Send marker
|
|
|
|
transmute::<Box<Error>, Box<Error + Send>>(s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 17:44:30 -05:00
|
|
|
|
|
|
|
impl Error + Send + Sync {
|
|
|
|
#[inline]
|
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
|
|
|
/// Attempt to downcast the box to a concrete type.
|
|
|
|
pub fn downcast<T: Error + 'static>(self: Box<Self>)
|
|
|
|
-> Result<Box<T>, Box<Self>> {
|
|
|
|
let err: Box<Error> = self;
|
|
|
|
<Error>::downcast(err).map_err(|s| unsafe {
|
|
|
|
// reapply the Send+Sync marker
|
|
|
|
transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use prelude::v1::*;
|
|
|
|
use super::Error;
|
|
|
|
use fmt;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
struct A;
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
struct B;
|
|
|
|
|
|
|
|
impl fmt::Display for A {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "A")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl fmt::Display for B {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "B")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for A {
|
|
|
|
fn description(&self) -> &str { "A-desc" }
|
|
|
|
}
|
|
|
|
impl Error for B {
|
|
|
|
fn description(&self) -> &str { "A-desc" }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downcasting() {
|
|
|
|
let mut a = A;
|
|
|
|
let mut a = &mut a as &mut (Error + 'static);
|
|
|
|
assert_eq!(a.downcast_ref::<A>(), Some(&A));
|
|
|
|
assert_eq!(a.downcast_ref::<B>(), None);
|
|
|
|
assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
|
|
|
|
assert_eq!(a.downcast_mut::<B>(), None);
|
|
|
|
|
|
|
|
let a: Box<Error> = Box::new(A);
|
|
|
|
match a.downcast::<B>() {
|
|
|
|
Ok(..) => panic!("expected error"),
|
|
|
|
Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|