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.
|
|
|
|
|
|
|
|
|
|
#![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.
|
|
|
|
|
|
2018-05-30 13:46:59 -05:00
|
|
|
|
use alloc::{AllocErr, LayoutErr, CannotReallocInPlace};
|
2015-04-24 16:34:57 -05:00
|
|
|
|
use any::TypeId;
|
2017-09-09 15:48:40 -05:00
|
|
|
|
use borrow::Cow;
|
2016-08-06 12:48:59 -05:00
|
|
|
|
use cell;
|
2016-04-07 12:42:53 -05:00
|
|
|
|
use char;
|
2017-09-29 10:15:05 -05:00
|
|
|
|
use core::array;
|
2015-04-01 14:25:47 -05:00
|
|
|
|
use fmt::{self, Debug, Display};
|
2015-04-24 16:34:57 -05:00
|
|
|
|
use mem::transmute;
|
2015-04-01 14:25:47 -05:00
|
|
|
|
use num;
|
|
|
|
|
use str;
|
2016-08-22 14:47:38 -05:00
|
|
|
|
use string;
|
2015-04-01 14:25:47 -05:00
|
|
|
|
|
2018-03-31 05:09:10 -05:00
|
|
|
|
/// `Error` is a trait representing the basic expectations for error values,
|
2018-11-26 20:59:49 -06:00
|
|
|
|
/// i.e., values of type `E` in [`Result<T, E>`]. Errors must describe
|
2018-03-31 05:09:10 -05:00
|
|
|
|
/// themselves through the [`Display`] and [`Debug`] traits, and may provide
|
|
|
|
|
/// cause chain information:
|
|
|
|
|
///
|
|
|
|
|
/// The [`cause`] method is generally used when errors cross "abstraction
|
2018-11-26 20:59:49 -06:00
|
|
|
|
/// boundaries", i.e., when a one module must report an error that is "caused"
|
2018-03-31 05:09:10 -05:00
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
/// [`Result<T, E>`]: ../result/enum.Result.html
|
|
|
|
|
/// [`Display`]: ../fmt/trait.Display.html
|
2018-06-27 13:15:24 -05:00
|
|
|
|
/// [`Debug`]: ../fmt/trait.Debug.html
|
2018-03-31 05:09:10 -05:00
|
|
|
|
/// [`cause`]: trait.Error.html#method.cause
|
2015-04-01 14:25:47 -05:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-10-06 00:28:27 -05:00
|
|
|
|
pub trait Error: Debug + Display {
|
2018-03-31 05:09:10 -05:00
|
|
|
|
/// **This method is soft-deprecated.**
|
2015-04-01 14:25:47 -05:00
|
|
|
|
///
|
2018-03-31 05:09:10 -05:00
|
|
|
|
/// Although using it won’t cause compilation warning,
|
|
|
|
|
/// new code should use [`Display`] instead
|
|
|
|
|
/// and new `impl`s can omit it.
|
2016-10-20 18:49:47 -05:00
|
|
|
|
///
|
2018-04-22 13:17:08 -05:00
|
|
|
|
/// To obtain error description as a string, use `to_string()`.
|
|
|
|
|
///
|
2016-10-20 18:49:47 -05:00
|
|
|
|
/// [`Display`]: ../fmt/trait.Display.html
|
2016-07-10 09:02:26 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// match "xc".parse::<u32>() {
|
|
|
|
|
/// Err(e) => {
|
2018-03-31 05:09:10 -05:00
|
|
|
|
/// // Print `e` itself, not `e.description()`.
|
|
|
|
|
/// println!("Error: {}", e);
|
2016-07-10 09:02:26 -05:00
|
|
|
|
/// }
|
|
|
|
|
/// _ => println!("No error"),
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-04-01 14:25:47 -05:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-31 05:09:10 -05:00
|
|
|
|
fn description(&self) -> &str {
|
2018-04-22 13:17:08 -05:00
|
|
|
|
"description() is deprecated; use Display"
|
2018-03-31 05:09:10 -05:00
|
|
|
|
}
|
2015-04-01 14:25:47 -05:00
|
|
|
|
|
|
|
|
|
/// The lower-level cause of this error, if any.
|
2016-07-10 09:02:26 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::fmt;
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
|
/// struct SuperError {
|
|
|
|
|
/// side: SuperErrorSideKick,
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl fmt::Display for SuperError {
|
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
/// write!(f, "SuperError is here!")
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl Error for SuperError {
|
|
|
|
|
/// fn description(&self) -> &str {
|
2016-11-25 10:59:04 -06:00
|
|
|
|
/// "I'm the superhero of errors"
|
2016-07-10 09:02:26 -05:00
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn cause(&self) -> Option<&Error> {
|
|
|
|
|
/// Some(&self.side)
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
|
/// struct SuperErrorSideKick;
|
|
|
|
|
///
|
|
|
|
|
/// impl fmt::Display for SuperErrorSideKick {
|
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
/// write!(f, "SuperErrorSideKick is here!")
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl Error for SuperErrorSideKick {
|
|
|
|
|
/// fn description(&self) -> &str {
|
2016-11-25 10:59:04 -06:00
|
|
|
|
/// "I'm SuperError side kick"
|
2016-07-10 09:02:26 -05:00
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn get_super_error() -> Result<(), SuperError> {
|
|
|
|
|
/// Err(SuperError { side: SuperErrorSideKick })
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
|
|
|
|
/// match get_super_error() {
|
|
|
|
|
/// Err(e) => {
|
|
|
|
|
/// println!("Error: {}", e.description());
|
|
|
|
|
/// println!("Caused by: {}", e.cause().unwrap());
|
|
|
|
|
/// }
|
|
|
|
|
/// _ => println!("No error"),
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-04-01 14:25:47 -05:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-20 13:18:29 -05:00
|
|
|
|
#[rustc_deprecated(since = "1.33.0", reason = "replaced by Error::source, which can support \
|
|
|
|
|
downcasting")]
|
|
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
|
|
|
|
self.source()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
/// write!(f, "SuperError is here!")
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl Error for SuperError {
|
|
|
|
|
/// fn description(&self) -> &str {
|
|
|
|
|
/// "I'm the superhero of errors"
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2018-08-20 17:58:52 -05:00
|
|
|
|
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
|
2018-08-20 13:18:29 -05:00
|
|
|
|
/// Some(&self.side)
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
|
/// struct SuperErrorSideKick;
|
|
|
|
|
///
|
|
|
|
|
/// impl fmt::Display for SuperErrorSideKick {
|
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
/// write!(f, "SuperErrorSideKick is here!")
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl Error for SuperErrorSideKick {
|
|
|
|
|
/// fn description(&self) -> &str {
|
|
|
|
|
/// "I'm SuperError side kick"
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn get_super_error() -> Result<(), SuperError> {
|
|
|
|
|
/// Err(SuperError { side: SuperErrorSideKick })
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
|
|
|
|
/// match get_super_error() {
|
|
|
|
|
/// Err(e) => {
|
|
|
|
|
/// println!("Error: {}", e.description());
|
|
|
|
|
/// println!("Caused by: {}", e.source().unwrap());
|
|
|
|
|
/// }
|
|
|
|
|
/// _ => println!("No error"),
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2018-08-20 16:27:25 -05:00
|
|
|
|
#[stable(feature = "error_source", since = "1.30.0")]
|
2018-08-20 13:18:29 -05:00
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> { 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")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a type of [`Error`] into a box of dyn [`Error`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::fmt;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
|
/// struct AnError;
|
|
|
|
|
///
|
|
|
|
|
/// impl fmt::Display for AnError {
|
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
/// write!(f , "An error")
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl Error for AnError {
|
|
|
|
|
/// fn description(&self) -> &str {
|
|
|
|
|
/// "Description of an error"
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let an_error = AnError;
|
|
|
|
|
/// assert!(0 == mem::size_of_val(&an_error));
|
|
|
|
|
/// let a_boxed_error = Box::<Error>::from(an_error);
|
|
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn from(err: E) -> Box<dyn Error + 'a> {
|
2015-04-01 14:25:47 -05:00
|
|
|
|
Box::new(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of dyn [`Error`] +
|
|
|
|
|
/// [`Send`] + [`Sync`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::fmt;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
|
/// struct AnError;
|
|
|
|
|
///
|
|
|
|
|
/// impl fmt::Display for AnError {
|
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
/// write!(f , "An error")
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl Error for AnError {
|
|
|
|
|
/// fn description(&self) -> &str {
|
|
|
|
|
/// "Description of an error"
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// unsafe impl Send for AnError {}
|
|
|
|
|
///
|
|
|
|
|
/// unsafe impl Sync for AnError {}
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let an_error = AnError;
|
|
|
|
|
/// assert!(0 == mem::size_of_val(&an_error));
|
|
|
|
|
/// let a_boxed_error = Box::<Error + Send + Sync>::from(an_error);
|
|
|
|
|
/// assert!(
|
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
|
2015-04-01 14:25:47 -05:00
|
|
|
|
Box::new(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl From<String> for Box<dyn Error + Send + Sync> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let a_string_error = "a string error".to_string();
|
|
|
|
|
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_string_error);
|
|
|
|
|
/// assert!(
|
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn from(err: String) -> Box<dyn 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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 02:38:39 -05:00
|
|
|
|
#[stable(feature = "string_box_error", since = "1.6.0")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl From<String> for Box<dyn Error> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a [`String`] into a box of dyn [`Error`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let a_string_error = "a string error".to_string();
|
|
|
|
|
/// let a_boxed_error = Box::<Error>::from(a_string_error);
|
|
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05: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 06:21:08 -06:00
|
|
|
|
err2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 15:10:25 -05:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let a_str_error = "a str error";
|
|
|
|
|
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_str_error);
|
|
|
|
|
/// assert!(
|
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn from(err: &'b str) -> Box<dyn 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 02:38:39 -05:00
|
|
|
|
#[stable(feature = "string_box_error", since = "1.6.0")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl<'a> From<&'a str> for Box<dyn Error> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a [`str`] into a box of dyn [`Error`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let a_str_error = "a str error";
|
|
|
|
|
/// let a_boxed_error = Box::<Error>::from(a_str_error);
|
|
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn from(err: &'a str) -> Box<dyn Error> {
|
2016-01-12 17:38:25 -06:00
|
|
|
|
From::from(String::from(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-09 15:48:40 -05:00
|
|
|
|
#[stable(feature = "cow_box_error", since = "1.22.0")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
/// use std::borrow::Cow;
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let a_cow_str_error = Cow::from("a str error");
|
|
|
|
|
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_cow_str_error);
|
|
|
|
|
/// assert!(
|
|
|
|
|
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
|
2017-09-09 15:48:40 -05:00
|
|
|
|
From::from(String::from(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "cow_box_error", since = "1.22.0")]
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
|
2018-09-05 02:03:00 -05:00
|
|
|
|
/// Converts a [`Cow`] into a box of dyn [`Error`].
|
2018-09-25 13:45:41 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::error::Error;
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
/// use std::borrow::Cow;
|
|
|
|
|
///
|
2018-10-02 17:21:51 -05:00
|
|
|
|
/// let a_cow_str_error = Cow::from("a str error");
|
|
|
|
|
/// let a_boxed_error = Box::<Error>::from(a_cow_str_error);
|
|
|
|
|
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
2018-09-25 13:45:41 -05:00
|
|
|
|
/// ```
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
|
2017-09-09 15:48:40 -05:00
|
|
|
|
From::from(String::from(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 10:07:58 -05:00
|
|
|
|
#[unstable(feature = "never_type", issue = "35121")]
|
2017-03-15 22:07:28 -05:00
|
|
|
|
impl Error for ! {
|
|
|
|
|
fn description(&self) -> &str { *self }
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 14:57:49 -05:00
|
|
|
|
#[unstable(feature = "allocator_api",
|
|
|
|
|
reason = "the precise API and guarantees it provides may be tweaked.",
|
2017-06-03 16:54:08 -05:00
|
|
|
|
issue = "32838")]
|
2018-04-03 07:36:57 -05:00
|
|
|
|
impl Error for AllocErr {
|
2017-06-13 14:57:49 -05:00
|
|
|
|
fn description(&self) -> &str {
|
2018-04-03 08:41:09 -05:00
|
|
|
|
"memory allocation failed"
|
2017-06-13 14:57:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 09:03:46 -05:00
|
|
|
|
#[unstable(feature = "allocator_api",
|
|
|
|
|
reason = "the precise API and guarantees it provides may be tweaked.",
|
|
|
|
|
issue = "32838")]
|
|
|
|
|
impl Error for LayoutErr {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"invalid parameters to Layout::from_size_align"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 14:57:49 -05:00
|
|
|
|
#[unstable(feature = "allocator_api",
|
|
|
|
|
reason = "the precise API and guarantees it provides may be tweaked.",
|
2017-06-03 16:54:08 -05:00
|
|
|
|
issue = "32838")]
|
2018-04-03 07:36:57 -05:00
|
|
|
|
impl Error for CannotReallocInPlace {
|
2017-06-13 14:57:49 -05:00
|
|
|
|
fn description(&self) -> &str {
|
2018-04-03 07:36:57 -05:00
|
|
|
|
CannotReallocInPlace::description(self)
|
2017-06-13 14:57:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 10:41:31 -05:00
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
2016-05-05 00:42:14 -05:00
|
|
|
|
impl Error for num::TryFromIntError {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
self.__description()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 10:41:31 -05:00
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
2017-09-29 10:15:05 -05:00
|
|
|
|
impl Error for array::TryFromSliceError {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
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-04-07 12:42:53 -05:00
|
|
|
|
#[stable(feature = "decode_utf16", since = "1.9.0")]
|
|
|
|
|
impl Error for char::DecodeUtf16Error {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"unpaired surrogate found"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 02:38:39 -05:00
|
|
|
|
#[stable(feature = "box_error", since = "1.8.0")]
|
2016-01-09 11:19:20 -06:00
|
|
|
|
impl<T: Error> Error for Box<T> {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
Error::description(&**self)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:35:36 -05:00
|
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
2016-01-09 11:19:20 -06:00
|
|
|
|
Error::cause(&**self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 17:49:49 -05:00
|
|
|
|
#[stable(feature = "fmt_error", since = "1.11.0")]
|
|
|
|
|
impl Error for fmt::Error {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"an error occurred when formatting an argument"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 19:23:36 -05:00
|
|
|
|
#[stable(feature = "try_borrow", since = "1.13.0")]
|
|
|
|
|
impl Error for cell::BorrowError {
|
2016-08-06 12:48:59 -05:00
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"already mutably borrowed"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 19:23:36 -05:00
|
|
|
|
#[stable(feature = "try_borrow", since = "1.13.0")]
|
|
|
|
|
impl Error for cell::BorrowMutError {
|
2016-08-06 12:48:59 -05:00
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"already borrowed"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 10:41:31 -05:00
|
|
|
|
#[unstable(feature = "try_from", issue = "33417")]
|
2016-08-17 11:01:41 -05:00
|
|
|
|
impl Error for char::CharTryFromError {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
"converted integer out of range for `char`"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-09 20:07:29 -05:00
|
|
|
|
#[stable(feature = "char_from_str", since = "1.20.0")]
|
2017-05-27 17:12:16 -05:00
|
|
|
|
impl Error for char::ParseCharError {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
self.__description()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 16:34:57 -05:00
|
|
|
|
// copied from any.rs
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl dyn Error + 'static {
|
2015-04-24 16:34:57 -05:00
|
|
|
|
/// 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 {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
Some(&*(self as *const dyn Error 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 {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
Some(&mut *(self as *mut dyn Error as *mut T))
|
2015-04-24 16:34:57 -05:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl dyn Error + 'static + Send {
|
2015-04-24 16:34:57 -05:00
|
|
|
|
/// 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 {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
<dyn Error + 'static>::is::<T>(self)
|
2015-04-24 16:34:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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> {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
<dyn Error + 'static>::downcast_ref::<T>(self)
|
2015-04-24 16:34:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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> {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
<dyn Error + 'static>::downcast_mut::<T>(self)
|
2015-07-28 17:44:30 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl dyn Error + 'static + Send + Sync {
|
2015-07-28 17:44:30 -05:00
|
|
|
|
/// 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 {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
<dyn Error + 'static>::is::<T>(self)
|
2015-07-28 17:44:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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> {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
<dyn Error + 'static>::downcast_ref::<T>(self)
|
2015-07-28 17:44:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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> {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
<dyn Error + 'static>::downcast_mut::<T>(self)
|
2015-04-24 16:34:57 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl dyn Error {
|
2015-04-24 16:34:57 -05:00
|
|
|
|
#[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.
|
2018-07-10 13:35:36 -05:00
|
|
|
|
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
|
2015-04-24 16:34:57 -05:00
|
|
|
|
if self.is::<T>() {
|
|
|
|
|
unsafe {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
let raw: *mut dyn Error = Box::into_raw(self);
|
2016-08-25 19:56:47 -05:00
|
|
|
|
Ok(Box::from_raw(raw as *mut T))
|
2015-04-24 16:34:57 -05:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl dyn Error + Send {
|
2015-04-24 16:34:57 -05:00
|
|
|
|
#[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>)
|
2018-07-10 13:35:36 -05:00
|
|
|
|
-> Result<Box<T>, Box<dyn Error + Send>> {
|
|
|
|
|
let err: Box<dyn Error> = self;
|
|
|
|
|
<dyn Error>::downcast(err).map_err(|s| unsafe {
|
2015-04-24 16:34:57 -05:00
|
|
|
|
// reapply the Send marker
|
2018-07-10 13:35:36 -05:00
|
|
|
|
transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
|
2015-04-24 16:34:57 -05:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-28 17:44:30 -05:00
|
|
|
|
|
2018-07-10 13:35:36 -05:00
|
|
|
|
impl dyn Error + Send + Sync {
|
2015-07-28 17:44:30 -05:00
|
|
|
|
#[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>> {
|
2018-07-10 13:35:36 -05:00
|
|
|
|
let err: Box<dyn Error> = self;
|
|
|
|
|
<dyn Error>::downcast(err).map_err(|s| unsafe {
|
2015-07-28 17:44:30 -05:00
|
|
|
|
// reapply the Send+Sync marker
|
2018-07-10 13:35:36 -05:00
|
|
|
|
transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
|
2015-07-28 17:44:30 -05:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
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;
|
2018-07-11 02:11:39 -05:00
|
|
|
|
let a = &mut a as &mut (dyn Error + 'static);
|
2015-07-28 17:44:30 -05:00
|
|
|
|
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);
|
|
|
|
|
|
2018-07-11 02:11:39 -05:00
|
|
|
|
let a: Box<dyn Error> = Box::new(A);
|
2015-07-28 17:44:30 -05:00
|
|
|
|
match a.downcast::<B>() {
|
|
|
|
|
Ok(..) => panic!("expected error"),
|
|
|
|
|
Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|