2015-04-01 19:25:47 +00:00
|
|
|
//! Traits for working with Errors.
|
|
|
|
|
|
|
|
#![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.
|
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2017-09-29 15:15:05 +00:00
|
|
|
use core::array;
|
2020-04-07 12:51:47 +00:00
|
|
|
use core::convert::Infallible;
|
2019-02-10 19:23:21 +00:00
|
|
|
|
2020-09-25 03:48:26 +00:00
|
|
|
use crate::alloc::{AllocError, LayoutError};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::any::TypeId;
|
2019-09-04 20:00:14 +00:00
|
|
|
use crate::backtrace::Backtrace;
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::borrow::Cow;
|
|
|
|
use crate::cell;
|
|
|
|
use crate::char;
|
|
|
|
use crate::fmt::{self, Debug, Display};
|
|
|
|
use crate::mem::transmute;
|
|
|
|
use crate::num;
|
|
|
|
use crate::str;
|
|
|
|
use crate::string;
|
2021-02-22 12:49:42 +00:00
|
|
|
use crate::sync::Arc;
|
2021-09-21 08:02:18 +00:00
|
|
|
use crate::time;
|
2015-04-01 19:25:47 +00:00
|
|
|
|
2018-03-31 10:09:10 +00:00
|
|
|
/// `Error` is a trait representing the basic expectations for error values,
|
2021-04-02 12:11:49 +00:00
|
|
|
/// i.e., values of type `E` in [`Result<T, E>`].
|
2018-03-31 10:09:10 +00:00
|
|
|
///
|
2021-04-02 12:11:49 +00:00
|
|
|
/// Errors must describe themselves through the [`Display`] and [`Debug`]
|
|
|
|
/// traits. Error messages are typically concise lowercase sentences without
|
|
|
|
/// trailing punctuation:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let err = "NaN".parse::<u32>().unwrap_err();
|
|
|
|
/// assert_eq!(err.to_string(), "invalid digit found in string");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Errors may provide cause chain information. [`Error::source()`] is generally
|
|
|
|
/// used when errors cross "abstraction boundaries". If one module must report
|
|
|
|
/// an error that is caused by an error from a lower-level module, it can allow
|
|
|
|
/// accessing that error via [`Error::source()`]. This makes it possible for the
|
|
|
|
/// high-level module to provide its own errors while also revealing some of the
|
2020-08-27 15:30:15 +00:00
|
|
|
/// implementation for debugging via `source` chains.
|
2015-04-01 19:25:47 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-10-06 05:28:27 +00:00
|
|
|
pub trait Error: Debug + Display {
|
2018-08-20 18:18:29 +00:00
|
|
|
/// The lower-level source of this error, if any.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::fmt;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct SuperError {
|
|
|
|
/// side: SuperErrorSideKick,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for SuperError {
|
2019-03-01 08:34:11 +00:00
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-20 18:18:29 +00:00
|
|
|
/// write!(f, "SuperError is here!")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Error for SuperError {
|
2018-08-20 22:58:52 +00:00
|
|
|
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
|
2018-08-20 18:18:29 +00:00
|
|
|
/// Some(&self.side)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct SuperErrorSideKick;
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for SuperErrorSideKick {
|
2019-03-01 08:34:11 +00:00
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-20 18:18:29 +00:00
|
|
|
/// write!(f, "SuperErrorSideKick is here!")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2019-12-23 15:47:28 +00:00
|
|
|
/// impl Error for SuperErrorSideKick {}
|
2018-08-20 18:18:29 +00:00
|
|
|
///
|
|
|
|
/// fn get_super_error() -> Result<(), SuperError> {
|
|
|
|
/// Err(SuperError { side: SuperErrorSideKick })
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// match get_super_error() {
|
|
|
|
/// Err(e) => {
|
2020-03-21 10:33:52 +00:00
|
|
|
/// println!("Error: {}", e);
|
2018-08-20 18:18:29 +00:00
|
|
|
/// println!("Caused by: {}", e.source().unwrap());
|
|
|
|
/// }
|
|
|
|
/// _ => println!("No error"),
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-08-20 21:27:25 +00:00
|
|
|
#[stable(feature = "error_source", since = "1.30.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
None
|
|
|
|
}
|
2015-04-24 21:34:57 +00:00
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Gets the `TypeId` of `self`.
|
2019-05-13 15:14:02 +00:00
|
|
|
#[doc(hidden)]
|
2019-12-22 22:42:04 +00:00
|
|
|
#[unstable(
|
|
|
|
feature = "error_type_id",
|
|
|
|
reason = "this is memory-unsafe to override in user code",
|
|
|
|
issue = "60784"
|
|
|
|
)]
|
|
|
|
fn type_id(&self, _: private::Internal) -> TypeId
|
|
|
|
where
|
|
|
|
Self: 'static,
|
|
|
|
{
|
2015-04-24 21:34:57 +00:00
|
|
|
TypeId::of::<Self>()
|
|
|
|
}
|
2019-09-04 20:00:14 +00:00
|
|
|
|
2019-11-27 03:19:54 +00:00
|
|
|
/// Returns a stack backtrace, if available, of where this error occurred.
|
2019-09-04 20:00:14 +00:00
|
|
|
///
|
|
|
|
/// This function allows inspecting the location, in code, of where an error
|
|
|
|
/// happened. The returned `Backtrace` contains information about the stack
|
|
|
|
/// trace of the OS thread of execution of where the error originated from.
|
|
|
|
///
|
|
|
|
/// Note that not all errors contain a `Backtrace`. Also note that a
|
|
|
|
/// `Backtrace` may actually be empty. For more information consult the
|
|
|
|
/// `Backtrace` type itself.
|
|
|
|
#[unstable(feature = "backtrace", issue = "53487")]
|
|
|
|
fn backtrace(&self) -> Option<&Backtrace> {
|
|
|
|
None
|
|
|
|
}
|
2019-12-01 04:01:48 +00:00
|
|
|
|
|
|
|
/// ```
|
|
|
|
/// if let Err(e) = "xc".parse::<u32>() {
|
|
|
|
/// // Print `e` itself, no need for description().
|
|
|
|
/// eprintln!("Error: {}", e);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-03-06 02:55:36 +00:00
|
|
|
#[rustc_deprecated(since = "1.42.0", reason = "use the Display impl or to_string()")]
|
2019-12-01 04:01:48 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"description() is deprecated; use Display"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[rustc_deprecated(
|
|
|
|
since = "1.33.0",
|
|
|
|
reason = "replaced by Error::source, which can support downcasting"
|
|
|
|
)]
|
|
|
|
#[allow(missing_docs)]
|
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
|
|
|
self.source()
|
|
|
|
}
|
2015-04-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 02:48:13 +00:00
|
|
|
mod private {
|
2019-05-17 03:18:29 +00:00
|
|
|
// This is a hack to prevent `type_id` from being overridden by `Error`
|
2019-05-17 02:48:13 +00:00
|
|
|
// implementations, since that can enable unsound downcasting.
|
|
|
|
#[unstable(feature = "error_type_id", issue = "60784")]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Internal;
|
|
|
|
}
|
|
|
|
|
2015-04-01 19:25:47 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-07-10 18:35:36 +00:00
|
|
|
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
|
2018-09-05 07:03:00 +00:00
|
|
|
/// Converts a type of [`Error`] into a box of dyn [`Error`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::fmt;
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct AnError;
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for AnError {
|
2019-03-01 08:34:11 +00:00
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-09-18 21:39:56 +00:00
|
|
|
/// write!(f, "An error")
|
2018-09-25 18:45:41 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2019-12-23 15:47:28 +00:00
|
|
|
/// impl Error for AnError {}
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let an_error = AnError;
|
|
|
|
/// assert!(0 == mem::size_of_val(&an_error));
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error>::from(an_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2018-07-10 18:35:36 +00:00
|
|
|
fn from(err: E) -> Box<dyn Error + 'a> {
|
2015-04-01 19:25:47 +00:00
|
|
|
Box::new(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-07-10 18:35:36 +00:00
|
|
|
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
|
2019-10-13 10:12:46 +00:00
|
|
|
/// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of
|
|
|
|
/// dyn [`Error`] + [`Send`] + [`Sync`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::fmt;
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct AnError;
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for AnError {
|
2019-03-01 08:34:11 +00:00
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-09-18 21:39:56 +00:00
|
|
|
/// write!(f, "An error")
|
2018-09-25 18:45:41 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2019-12-23 15:47:28 +00:00
|
|
|
/// impl Error for AnError {}
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
|
|
|
/// unsafe impl Send for AnError {}
|
|
|
|
///
|
|
|
|
/// unsafe impl Sync for AnError {}
|
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let an_error = AnError;
|
|
|
|
/// assert!(0 == mem::size_of_val(&an_error));
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(an_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2018-07-10 18:35:36 +00:00
|
|
|
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
|
2015-04-01 19:25:47 +00:00
|
|
|
Box::new(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-07-10 18:35:36 +00:00
|
|
|
impl From<String> for Box<dyn Error + Send + Sync> {
|
2019-10-13 10:12:46 +00:00
|
|
|
/// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let a_string_error = "a string error".to_string();
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_string_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2020-01-10 19:27:02 +00:00
|
|
|
#[inline]
|
2018-07-10 18:35:36 +00:00
|
|
|
fn from(err: String) -> Box<dyn Error + Send + Sync> {
|
2015-04-01 19:25:47 +00:00
|
|
|
struct StringError(String);
|
|
|
|
|
|
|
|
impl Error for StringError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2019-12-22 22:42:04 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
2015-04-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for StringError {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-04-01 19:25:47 +00:00
|
|
|
Display::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 00:09:04 +00:00
|
|
|
// Purposefully skip printing "StringError(..)"
|
|
|
|
impl Debug for StringError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
Debug::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 20:10:25 +00:00
|
|
|
Box::new(StringError(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 07:38:39 +00:00
|
|
|
#[stable(feature = "string_box_error", since = "1.6.0")]
|
2018-07-10 18:35:36 +00:00
|
|
|
impl From<String> for Box<dyn Error> {
|
2018-09-05 07:03:00 +00:00
|
|
|
/// Converts a [`String`] into a box of dyn [`Error`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let a_string_error = "a string error".to_string();
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error>::from(a_string_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2018-07-10 18:35:36 +00:00
|
|
|
fn from(str_err: String) -> Box<dyn Error> {
|
|
|
|
let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
|
|
|
|
let err2: Box<dyn Error> = err1;
|
2015-12-21 12:21:08 +00:00
|
|
|
err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 20:10:25 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-03-10 03:10:28 +00:00
|
|
|
impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
|
2019-10-13 10:12:46 +00:00
|
|
|
/// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
2020-08-22 20:29:55 +00:00
|
|
|
/// [`str`]: prim@str
|
|
|
|
///
|
2018-09-25 18:45:41 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let a_str_error = "a str error";
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_str_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2020-01-10 19:27:02 +00:00
|
|
|
#[inline]
|
2019-03-10 03:10:28 +00:00
|
|
|
fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
|
2015-06-08 14:55:35 +00:00
|
|
|
From::from(String::from(err))
|
2015-04-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 07:38:39 +00:00
|
|
|
#[stable(feature = "string_box_error", since = "1.6.0")]
|
2019-03-10 03:10:28 +00:00
|
|
|
impl From<&str> for Box<dyn Error> {
|
2018-09-05 07:03:00 +00:00
|
|
|
/// Converts a [`str`] into a box of dyn [`Error`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
2020-08-22 20:29:55 +00:00
|
|
|
/// [`str`]: prim@str
|
|
|
|
///
|
2018-09-25 18:45:41 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let a_str_error = "a str error";
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error>::from(a_str_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2019-03-10 03:10:28 +00:00
|
|
|
fn from(err: &str) -> Box<dyn Error> {
|
2016-01-12 23:38:25 +00:00
|
|
|
From::from(String::from(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 20:48:40 +00:00
|
|
|
#[stable(feature = "cow_box_error", since = "1.22.0")]
|
2018-07-10 18:35:36 +00:00
|
|
|
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
|
2019-10-13 10:12:46 +00:00
|
|
|
/// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::mem;
|
|
|
|
/// use std::borrow::Cow;
|
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let a_cow_str_error = Cow::from("a str error");
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2018-07-10 18:35:36 +00:00
|
|
|
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
|
2017-09-09 20:48:40 +00:00
|
|
|
From::from(String::from(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "cow_box_error", since = "1.22.0")]
|
2018-07-10 18:35:36 +00:00
|
|
|
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
|
2018-09-05 07:03:00 +00:00
|
|
|
/// Converts a [`Cow`] into a box of dyn [`Error`].
|
2018-09-25 18:45:41 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::mem;
|
|
|
|
/// use std::borrow::Cow;
|
|
|
|
///
|
2018-10-02 22:21:51 +00:00
|
|
|
/// let a_cow_str_error = Cow::from("a str error");
|
2019-05-28 18:48:04 +00:00
|
|
|
/// let a_boxed_error = Box::<dyn Error>::from(a_cow_str_error);
|
2018-10-02 22:21:51 +00:00
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 18:45:41 +00:00
|
|
|
/// ```
|
2018-07-10 18:35:36 +00:00
|
|
|
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
|
2017-09-09 20:48:40 +00:00
|
|
|
From::from(String::from(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:55:29 +00:00
|
|
|
#[unstable(feature = "never_type", issue = "35121")]
|
2019-12-01 04:01:48 +00:00
|
|
|
impl Error for ! {}
|
2017-03-16 03:07:28 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
#[unstable(
|
|
|
|
feature = "allocator_api",
|
|
|
|
reason = "the precise API and guarantees it provides may be tweaked.",
|
|
|
|
issue = "32838"
|
|
|
|
)]
|
2020-09-24 22:10:56 +00:00
|
|
|
impl Error for AllocError {}
|
2017-06-13 19:57:49 +00:00
|
|
|
|
2020-09-10 07:40:40 +00:00
|
|
|
#[stable(feature = "alloc_layout", since = "1.28.0")]
|
2020-09-25 03:48:26 +00:00
|
|
|
impl Error for LayoutError {}
|
2018-04-04 14:03:46 +00:00
|
|
|
|
2015-04-01 19:25:47 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for str::ParseBoolError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2019-12-22 22:42:04 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"failed to parse bool"
|
|
|
|
}
|
2015-04-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for str::Utf8Error {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2015-04-01 19:25:47 +00:00
|
|
|
fn description(&self) -> &str {
|
2015-04-10 23:05:09 +00:00
|
|
|
"invalid utf-8: corrupt contents"
|
2015-04-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for num::ParseIntError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2015-04-01 19:25:47 +00:00
|
|
|
fn description(&self) -> &str {
|
2015-06-11 16:56:13 +00:00
|
|
|
self.__description()
|
2015-04-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:00:47 +00:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2016-05-05 05:42:14 +00:00
|
|
|
impl Error for num::TryFromIntError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2016-05-05 05:42:14 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
self.__description()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:00:47 +00:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2017-09-29 15:15:05 +00:00
|
|
|
impl Error for array::TryFromSliceError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2017-09-29 15:15:05 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
self.__description()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 19:25:47 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for num::ParseFloatError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2015-04-01 19:25:47 +00:00
|
|
|
fn description(&self) -> &str {
|
2015-04-24 22:58:27 +00:00
|
|
|
self.__description()
|
2015-04-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for string::FromUtf8Error {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2015-04-01 19:25:47 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"invalid utf-8"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Error for string::FromUtf16Error {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2015-04-01 19:25:47 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"invalid utf-16"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:49:29 +00:00
|
|
|
#[stable(feature = "str_parse_error2", since = "1.8.0")]
|
2020-04-07 12:51:47 +00:00
|
|
|
impl Error for Infallible {
|
2019-12-11 14:49:29 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:42:53 +00:00
|
|
|
#[stable(feature = "decode_utf16", since = "1.9.0")]
|
|
|
|
impl Error for char::DecodeUtf16Error {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2016-04-07 17:42:53 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"unpaired surrogate found"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 15:54:28 +00:00
|
|
|
#[unstable(feature = "map_try_insert", issue = "82766")]
|
2021-03-04 14:57:26 +00:00
|
|
|
impl<'a, K: Debug + Ord, V: Debug> Error
|
|
|
|
for crate::collections::btree_map::OccupiedError<'a, K, V>
|
|
|
|
{
|
|
|
|
#[allow(deprecated)]
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"key already exists"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 15:54:28 +00:00
|
|
|
#[unstable(feature = "map_try_insert", issue = "82766")]
|
2021-03-04 14:57:26 +00:00
|
|
|
impl<'a, K: Debug, V: Debug> Error for crate::collections::hash_map::OccupiedError<'a, K, V> {
|
|
|
|
#[allow(deprecated)]
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"key already exists"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 07:38:39 +00:00
|
|
|
#[stable(feature = "box_error", since = "1.8.0")]
|
2016-01-09 17:19:20 +00:00
|
|
|
impl<T: Error> Error for Box<T> {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated, deprecated_in_future)]
|
2016-01-09 17:19:20 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
Error::description(&**self)
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:42:56 +00:00
|
|
|
#[allow(deprecated)]
|
2018-07-10 18:35:36 +00:00
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
2016-01-09 17:19:20 +00:00
|
|
|
Error::cause(&**self)
|
|
|
|
}
|
2019-06-16 19:59:12 +00:00
|
|
|
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
Error::source(&**self)
|
|
|
|
}
|
2016-01-09 17:19:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 08:40:34 +00:00
|
|
|
#[stable(feature = "error_by_ref", since = "1.51.0")]
|
2020-08-05 06:49:48 +00:00
|
|
|
impl<'a, T: Error + ?Sized> Error for &'a T {
|
|
|
|
#[allow(deprecated, deprecated_in_future)]
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
Error::description(&**self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
|
|
|
Error::cause(&**self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
Error::source(&**self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn backtrace(&self) -> Option<&Backtrace> {
|
|
|
|
Error::backtrace(&**self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 12:49:42 +00:00
|
|
|
#[stable(feature = "arc_error", since = "1.52.0")]
|
|
|
|
impl<T: Error + ?Sized> Error for Arc<T> {
|
|
|
|
#[allow(deprecated, deprecated_in_future)]
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
Error::description(&**self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
|
|
|
Error::cause(&**self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
Error::source(&**self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn backtrace(&self) -> Option<&Backtrace> {
|
|
|
|
Error::backtrace(&**self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 22:49:49 +00:00
|
|
|
#[stable(feature = "fmt_error", since = "1.11.0")]
|
|
|
|
impl Error for fmt::Error {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2016-05-24 22:49:49 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"an error occurred when formatting an argument"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:23:36 +00:00
|
|
|
#[stable(feature = "try_borrow", since = "1.13.0")]
|
|
|
|
impl Error for cell::BorrowError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2016-08-06 17:48:59 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"already mutably borrowed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:23:36 +00:00
|
|
|
#[stable(feature = "try_borrow", since = "1.13.0")]
|
|
|
|
impl Error for cell::BorrowMutError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2016-08-06 17:48:59 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"already borrowed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:00:47 +00:00
|
|
|
#[stable(feature = "try_from", since = "1.34.0")]
|
2016-08-17 16:01:41 +00:00
|
|
|
impl Error for char::CharTryFromError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2016-08-17 16:01:41 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"converted integer out of range for `char`"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 01:07:29 +00:00
|
|
|
#[stable(feature = "char_from_str", since = "1.20.0")]
|
2017-05-27 22:12:16 +00:00
|
|
|
impl Error for char::ParseCharError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2017-05-27 22:12:16 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
self.__description()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 22:21:33 +00:00
|
|
|
#[stable(feature = "try_reserve", since = "1.57.0")]
|
2020-03-10 10:19:40 +00:00
|
|
|
impl Error for alloc::collections::TryReserveError {}
|
2020-03-06 09:29:11 +00:00
|
|
|
|
2021-06-14 12:16:13 +00:00
|
|
|
#[unstable(feature = "duration_checked_float", issue = "83400")]
|
2021-09-21 08:02:18 +00:00
|
|
|
impl Error for time::FromSecsError {}
|
2021-06-14 12:16:13 +00:00
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
// Copied from `any.rs`.
|
2018-07-10 18:35:36 +00:00
|
|
|
impl dyn Error + 'static {
|
2019-02-09 22:16:58 +00:00
|
|
|
/// Returns `true` if the boxed type is the same as `T`
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn is<T: Error + 'static>(&self) -> bool {
|
2019-09-05 16:15:28 +00:00
|
|
|
// Get `TypeId` of the type this function is instantiated with.
|
2015-04-24 21:34:57 +00:00
|
|
|
let t = TypeId::of::<T>();
|
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
// Get `TypeId` of the type in the trait object.
|
2019-05-17 02:48:13 +00:00
|
|
|
let boxed = self.type_id(private::Internal);
|
2015-04-24 21:34:57 +00:00
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
// Compare both `TypeId`s on equality.
|
2015-04-24 21:34:57 +00:00
|
|
|
t == boxed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns some reference to the boxed value if it is of type `T`, or
|
|
|
|
/// `None` if it isn't.
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
|
|
|
if self.is::<T>() {
|
2019-12-22 22:42:04 +00:00
|
|
|
unsafe { Some(&*(self as *const dyn Error as *const T)) }
|
2015-04-24 21:34:57 +00: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 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
|
|
|
if self.is::<T>() {
|
2019-12-22 22:42:04 +00:00
|
|
|
unsafe { Some(&mut *(self as *mut dyn Error as *mut T)) }
|
2015-04-24 21:34:57 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 18:35:36 +00:00
|
|
|
impl dyn Error + 'static + Send {
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Forwards to the method defined on the type `dyn Error`.
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn is<T: Error + 'static>(&self) -> bool {
|
2018-07-10 18:35:36 +00:00
|
|
|
<dyn Error + 'static>::is::<T>(self)
|
2015-04-24 21:34:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Forwards to the method defined on the type `dyn Error`.
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
2018-07-10 18:35:36 +00:00
|
|
|
<dyn Error + 'static>::downcast_ref::<T>(self)
|
2015-04-24 21:34:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Forwards to the method defined on the type `dyn Error`.
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
|
|
|
#[inline]
|
|
|
|
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
2018-07-10 18:35:36 +00:00
|
|
|
<dyn Error + 'static>::downcast_mut::<T>(self)
|
2015-07-28 22:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 18:35:36 +00:00
|
|
|
impl dyn Error + 'static + Send + Sync {
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Forwards to the method defined on the type `dyn Error`.
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
|
|
|
#[inline]
|
|
|
|
pub fn is<T: Error + 'static>(&self) -> bool {
|
2018-07-10 18:35:36 +00:00
|
|
|
<dyn Error + 'static>::is::<T>(self)
|
2015-07-28 22:44:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Forwards to the method defined on the type `dyn Error`.
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
|
|
|
#[inline]
|
|
|
|
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
2018-07-10 18:35:36 +00:00
|
|
|
<dyn Error + 'static>::downcast_ref::<T>(self)
|
2015-07-28 22:44:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Forwards to the method defined on the type `dyn Error`.
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
2018-07-10 18:35:36 +00:00
|
|
|
<dyn Error + 'static>::downcast_mut::<T>(self)
|
2015-04-24 21:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 18:35:36 +00:00
|
|
|
impl dyn Error {
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Attempts to downcast the box to a concrete type.
|
2018-07-10 18:35:36 +00:00
|
|
|
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
|
2015-04-24 21:34:57 +00:00
|
|
|
if self.is::<T>() {
|
|
|
|
unsafe {
|
2018-07-10 18:35:36 +00:00
|
|
|
let raw: *mut dyn Error = Box::into_raw(self);
|
2016-08-26 00:56:47 +00:00
|
|
|
Ok(Box::from_raw(raw as *mut T))
|
2015-04-24 21:34:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(self)
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 15:10:34 +00:00
|
|
|
|
|
|
|
/// Returns an iterator starting with the current error and continuing with
|
2020-08-26 14:41:56 +00:00
|
|
|
/// recursively calling [`Error::source`].
|
2019-02-07 15:10:34 +00:00
|
|
|
///
|
2019-10-15 08:34:53 +00:00
|
|
|
/// If you want to omit the current error and only use its sources,
|
|
|
|
/// use `skip(1)`.
|
|
|
|
///
|
2019-02-07 15:10:34 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(error_iter)]
|
|
|
|
/// use std::error::Error;
|
|
|
|
/// use std::fmt;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct A;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// struct B(Option<Box<dyn Error + 'static>>);
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for A {
|
2019-03-01 08:34:11 +00:00
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-02-07 15:10:34 +00:00
|
|
|
/// write!(f, "A")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for B {
|
2019-03-01 08:34:11 +00:00
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-02-07 15:10:34 +00:00
|
|
|
/// write!(f, "B")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Error for A {}
|
|
|
|
///
|
|
|
|
/// impl Error for B {
|
|
|
|
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
/// self.0.as_ref().map(|e| e.as_ref())
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let b = B(Some(Box::new(A)));
|
|
|
|
///
|
|
|
|
/// // let err : Box<Error> = b.into(); // or
|
|
|
|
/// let err = &b as &(dyn Error);
|
|
|
|
///
|
2019-10-15 08:34:53 +00:00
|
|
|
/// let mut iter = err.chain();
|
2019-02-07 15:10:34 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
|
|
|
|
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
|
|
|
|
/// assert!(iter.next().is_none());
|
|
|
|
/// assert!(iter.next().is_none());
|
|
|
|
/// ```
|
2019-02-16 20:16:18 +00:00
|
|
|
#[unstable(feature = "error_iter", issue = "58520")]
|
2019-02-07 15:10:34 +00:00
|
|
|
#[inline]
|
2019-10-15 08:34:53 +00:00
|
|
|
pub fn chain(&self) -> Chain<'_> {
|
2019-12-22 22:42:04 +00:00
|
|
|
Chain { current: Some(self) }
|
2019-02-07 15:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 08:34:53 +00:00
|
|
|
/// An iterator over an [`Error`] and its sources.
|
|
|
|
///
|
|
|
|
/// If you want to omit the initial error and only process
|
|
|
|
/// its sources, use `skip(1)`.
|
2019-02-16 20:16:18 +00:00
|
|
|
#[unstable(feature = "error_iter", issue = "58520")]
|
2019-11-15 13:29:35 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2019-10-15 08:34:53 +00:00
|
|
|
pub struct Chain<'a> {
|
2019-02-07 15:10:34 +00:00
|
|
|
current: Option<&'a (dyn Error + 'static)>,
|
|
|
|
}
|
|
|
|
|
2019-02-16 20:16:18 +00:00
|
|
|
#[unstable(feature = "error_iter", issue = "58520")]
|
2019-10-15 08:34:53 +00:00
|
|
|
impl<'a> Iterator for Chain<'a> {
|
2019-02-07 15:10:34 +00:00
|
|
|
type Item = &'a (dyn Error + 'static);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let current = self.current;
|
|
|
|
self.current = self.current.and_then(Error::source);
|
|
|
|
current
|
|
|
|
}
|
2015-04-24 21:34:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 18:35:36 +00:00
|
|
|
impl dyn Error + Send {
|
2015-04-24 21:34:57 +00:00
|
|
|
#[inline]
|
2015-07-28 22:44:30 +00:00
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Attempts to downcast the box to a concrete type.
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error + Send>> {
|
2018-07-10 18:35:36 +00:00
|
|
|
let err: Box<dyn Error> = self;
|
|
|
|
<dyn Error>::downcast(err).map_err(|s| unsafe {
|
2019-09-05 16:15:28 +00:00
|
|
|
// Reapply the `Send` marker.
|
2018-07-10 18:35:36 +00:00
|
|
|
transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
|
2015-04-24 21:34:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 22:44:30 +00:00
|
|
|
|
2018-07-10 18:35:36 +00:00
|
|
|
impl dyn Error + Send + Sync {
|
2015-07-28 22:44:30 +00:00
|
|
|
#[inline]
|
|
|
|
#[stable(feature = "error_downcast", since = "1.3.0")]
|
2019-09-05 16:15:28 +00:00
|
|
|
/// Attempts to downcast the box to a concrete type.
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
|
2018-07-10 18:35:36 +00:00
|
|
|
let err: Box<dyn Error> = self;
|
|
|
|
<dyn Error>::downcast(err).map_err(|s| unsafe {
|
2019-09-05 16:15:28 +00:00
|
|
|
// Reapply the `Send + Sync` marker.
|
2018-07-10 18:35:36 +00:00
|
|
|
transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
|
2015-07-28 22:44:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|