Skip to content
Myr

Conversion from Box<dyn StdError + Send + Sync> to Error

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent ba0b307 · Browse files · 6 files+108 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+108 added−3 removed

  • tests:+40 added−0 removed
  • other:+68 added−3 removed

6 files+108 added−3 removed

modified:src/error.rs+20 added-1 removed
1use crate::backtrace::Backtrace;2use crate::wrapper::{DisplayError, MessageError};2use crate::wrapper::{BoxedError, DisplayError, MessageError};3use crate::{Chain, Error};4use std::any::TypeId;5use std::error::Error as StdError;6use std::fmt::{self, Debug, Display};
135 unmodified lines
142 // Safety: passing vtable that operates on the right type.143 unsafe { Error::construct(error, vtable, backtrace) }144 }145146 pub(crate) fn from_boxed(147 error: Box<dyn StdError + Send + Sync>,148 backtrace: Option<Backtrace>,149 ) -> Self {150 let error = BoxedError(error);151 let vtable = &ErrorVTable {152 object_drop: object_drop::<BoxedError>,153 object_ref: object_ref::<BoxedError>,154 object_mut: object_mut::<BoxedError>,155 object_boxed: object_boxed::<BoxedError>,156 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,157 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,158 };159160 // Safety: BoxedError is repr(transparent) so it is okay for the vtable161 // to allow casting to Box<dyn StdError + Send + Sync>.162 unsafe { Error::construct(error, vtable, backtrace) }163 }164165 // Takes backtrace as argument rather than capturing it here so that the166 // user sees one fewer layer of wrapping noise in the backtrace.167 //168 // Unsafe because the given vtable must have sensible behavior on the error
598 unmodified lines
modified:src/kind.rs+19 added
43 unmodified lines
44// let error = $msg;45// (&error).anyhow_kind().new(error)4647use crate::Error;48use std::error::Error as StdError;49use std::fmt::{Debug, Display};5051#[cfg(backtrace)]52use std::backtrace::Backtrace;
36 unmodified lines
89 {90 error.into()91 }92}9394pub struct Boxed;9596pub trait BoxedKind: Sized {97 #[inline]98 fn anyhow_kind(&self) -> Boxed {99 Boxed100 }101}102103impl BoxedKind for Box<dyn StdError + Send + Sync> {}104105impl Boxed {106 pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {107 let backtrace = backtrace_if_absent!(error);108 Error::from_boxed(error, backtrace)109 }110}
modified:src/lib.rs+1 added-1 removed
520 unmodified lines
521522 #[cfg(backtrace)]523 use std::backtrace::Backtrace;524525 pub use crate::kind::{AdhocKind, TraitKind};525 pub use crate::kind::{AdhocKind, BoxedKind, TraitKind};526527 pub fn new_adhoc<M>(message: M) -> Error528 where529 M: Display + Debug + Send + Sync + 'static,
4 unmodified lines
modified:src/macros.rs+1 added-1 removed
152 unmodified lines
153 $crate::private::new_adhoc($msg)154 };155 ($err:expr $(,)?) => ({156 #[allow(unused_imports)]157 use $crate::private::{AdhocKind, TraitKind};157 use $crate::private::{AdhocKind, BoxedKind, TraitKind};158 let error = $err;159 (&error).anyhow_kind().new(error)160 });161 ($fmt:expr, $($arg:tt)*) => {
3 unmodified lines
modified:src/wrapper.rs+27 added
1use crate::backtrace::Backtrace;2use std::error::Error as StdError;3use std::fmt::{self, Debug, Display};45#[repr(transparent)]
40 unmodified lines
46 }47}4849impl<M> StdError for DisplayError<M> where M: Display + 'static {}5051#[repr(transparent)]52pub struct BoxedError(pub Box<dyn StdError + Send + Sync>);5354impl Debug for BoxedError {55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {56 Debug::fmt(&self.0, f)57 }58}5960impl Display for BoxedError {61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {62 Display::fmt(&self.0, f)63 }64}6566impl StdError for BoxedError {67 #[cfg(backtrace)]68 fn backtrace(&self) -> Option<&Backtrace> {69 self.0.backtrace()70 }7172 fn source(&self) -> Option<&(dyn StdError + 'static)> {73 self.0.source()74 }75}
added:tests/test_boxed.rs+40 added
1use anyhow::anyhow;2use std::error::Error as StdError;3use std::io;4use thiserror::Error;56#[derive(Error, Debug)]7#[error("outer")]8struct MyError {9 source: io::Error,10}1112#[test]13fn test_boxed_str() {14 let error = Box::<dyn StdError + Send + Sync>::from("oh no!");15 let error = anyhow!(error);16 assert_eq!("oh no!", error.to_string());17 assert_eq!(18 "oh no!",19 error20 .downcast_ref::<Box<dyn StdError + Send + Sync>>()21 .unwrap()22 .to_string()23 );24}2526#[test]27fn test_boxed_thiserror() {28 let error = MyError {29 source: io::Error::new(io::ErrorKind::Other, "oh no!"),30 };31 let error = anyhow!(error);32 assert_eq!("oh no!", error.source().unwrap().to_string());33}3435#[test]36fn test_boxed_anyhow() {37 let error = anyhow!("oh no!").context("it failed");38 let error = anyhow!(error);39 assert_eq!("oh no!", error.source().unwrap().to_string());40}