A2YH3UCN3AEAXWW7IHTJBBQY4XRBSV2TBOX3N47ZZGKE6L7P7L5AC
ZZNRZYYTQW73EH2MNFK4IUQ4WY67GZNENHTDQ2MWKD3GZBLNPSMQC
BXFHQQPRDN25MCKA4CWNQ6YJEHMMRYOVIXCKA36WE4I4B4ZIZE5AC
45AUHCDJX5A54AYFMJRGDHKMBQ3WYBIPAVPDPY7TCLK5WN5TSROQC
CITEDKPB6MKVZUEYEDE5ZKTNVY35HCOAXKDPYG7YLLEOVFNMSRXQC
VSLQNAQBAGOJC2IV55V3W755UIPN47J4RTCCLCABRFFFUU5LT2JQC
T7TIYYVIKIUTGJZQDO47KN4643XSYSGC4LDE2FJDQ47GG5GCVWPAC
MBJSV73XEUIBF7LU6MUYQJQLCRXYY7LXD7KZZFLLOCEDOKU2EAQAC
7JZIPWVKXY3QA5QMTP24WPR7KKMMVXIHAQ4EGHNGCE7H4CRKROCAC
YMUCHF4MBM4KQ2TBSJKGN5JFPZLTTI43CQETAMAC5K6KKLLVE73AC
BZSTVYDVHJA3ZMCM3YTU4U5JKJC2JVUDM5APSBIGMSS54J2C4WKQC
pub(crate) fn from_boxed(
error: Box<dyn StdError + Send + Sync>,
backtrace: Option<Backtrace>,
) -> Self {
let error = BoxedError(error);
let vtable = &ErrorVTable {
object_drop: object_drop::<BoxedError>,
object_ref: object_ref::<BoxedError>,
object_mut: object_mut::<BoxedError>,
object_boxed: object_boxed::<BoxedError>,
object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,
object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,
};
// Safety: BoxedError is repr(transparent) so it is okay for the vtable
// to allow casting to Box<dyn StdError + Send + Sync>.
unsafe { Error::construct(error, vtable, backtrace) }
}
impl BoxedKind for Box<dyn StdError + Send + Sync> {}
impl Boxed {
pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {
let backtrace = backtrace_if_absent!(error);
Error::from_boxed(error, backtrace)
}
}
#[repr(transparent)]
pub struct BoxedError(pub Box<dyn StdError + Send + Sync>);
impl Debug for BoxedError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl Display for BoxedError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl StdError for BoxedError {
#[cfg(backtrace)]
fn backtrace(&self) -> Option<&Backtrace> {
self.0.backtrace()
}
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.0.source()
}
}
use anyhow::anyhow;
use std::error::Error as StdError;
use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
#[error("outer")]
struct MyError {
source: io::Error,
}
#[test]
fn test_boxed_str() {
let error = Box::<dyn StdError + Send + Sync>::from("oh no!");
let error = anyhow!(error);
assert_eq!("oh no!", error.to_string());
assert_eq!(
"oh no!",
error
.downcast_ref::<Box<dyn StdError + Send + Sync>>()
.unwrap()
.to_string()
);
}
#[test]
fn test_boxed_thiserror() {
let error = MyError {
source: io::Error::new(io::ErrorKind::Other, "oh no!"),
};
let error = anyhow!(error);
assert_eq!("oh no!", error.source().unwrap().to_string());
}
#[test]
fn test_boxed_anyhow() {
let error = anyhow!("oh no!").context("it failed");
let error = anyhow!(error);
assert_eq!("oh no!", error.source().unwrap().to_string());
}