use crate::alloc::Box;
use crate::chain::Chain;
use crate::EyreContext;
use crate::{Report, StdError};
use core::any::TypeId;
use core::fmt::{self, Debug, Display};
use core::mem::{self, ManuallyDrop};
use core::ptr::{self, NonNull};
#[cfg(feature = "std")]
use core::ops::{Deref, DerefMut};
impl<C> Report<C>
where
C: EyreContext,
{
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn new<E>(error: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
Report::from_std(error)
}
pub fn msg<M>(message: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
{
Report::from_adhoc(message)
}
#[cfg(feature = "std")]
pub(crate) fn from_std<E>(error: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
let vtable = &ErrorVTable {
object_drop: object_drop::<E, C>,
object_ref: object_ref::<E, C>,
#[cfg(feature = "std")]
object_mut: object_mut::<E, C>,
object_boxed: object_boxed::<E, C>,
object_downcast: object_downcast::<E, C>,
object_drop_rest: object_drop_front::<E, C>,
};
let context = Some(C::default(&error));
unsafe { Report::construct(error, vtable, context) }
}
pub(crate) fn from_adhoc<M>(message: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
{
use crate::wrapper::MessageError;
let error: MessageError<M> = MessageError(message);
let vtable = &ErrorVTable {
object_drop: object_drop::<MessageError<M>, C>,
object_ref: object_ref::<MessageError<M>, C>,
#[cfg(feature = "std")]
object_mut: object_mut::<MessageError<M>, C>,
object_boxed: object_boxed::<MessageError<M>, C>,
object_downcast: object_downcast::<M, C>,
object_drop_rest: object_drop_front::<M, C>,
};
let context = Some(C::default(&error));
unsafe { Report::construct(error, vtable, context) }
}
pub(crate) fn from_display<M>(message: M) -> Self
where
M: Display + Send + Sync + 'static,
{
use crate::wrapper::{DisplayError, NoneError};
let error: DisplayError<M> = DisplayError(message);
let vtable = &ErrorVTable {
object_drop: object_drop::<DisplayError<M>, C>,
object_ref: object_ref::<DisplayError<M>, C>,
#[cfg(feature = "std")]
object_mut: object_mut::<DisplayError<M>, C>,
object_boxed: object_boxed::<DisplayError<M>, C>,
object_downcast: object_downcast::<M, C>,
object_drop_rest: object_drop_front::<M, C>,
};
let context = Some(C::default(&NoneError));
unsafe { Report::construct(error, vtable, context) }
}
#[cfg(feature = "std")]
pub(crate) fn from_msg<D, E>(msg: D, error: E) -> Self
where
D: Display + Send + Sync + 'static,
E: StdError + Send + Sync + 'static,
{
let error: ContextError<D, E> = ContextError { msg, error };
let vtable = &ErrorVTable {
object_drop: object_drop::<ContextError<D, E>, C>,
object_ref: object_ref::<ContextError<D, E>, C>,
#[cfg(feature = "std")]
object_mut: object_mut::<ContextError<D, E>, C>,
object_boxed: object_boxed::<ContextError<D, E>, C>,
object_downcast: context_downcast::<D, E, C>,
object_drop_rest: context_drop_rest::<D, E, C>,
};
let context = Some(C::default(&error));
unsafe { Report::construct(error, vtable, context) }
}
#[cfg(feature = "std")]
pub(crate) fn from_boxed(error: Box<dyn StdError + Send + Sync>) -> Self {
use crate::wrapper::BoxedError;
let error = BoxedError(error);
let context = Some(C::default(&error));
let vtable = &ErrorVTable {
object_drop: object_drop::<BoxedError, C>,
object_ref: object_ref::<BoxedError, C>,
#[cfg(feature = "std")]
object_mut: object_mut::<BoxedError, C>,
object_boxed: object_boxed::<BoxedError, C>,
object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>, C>,
object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>, C>,
};
unsafe { Report::construct(error, vtable, context) }
}
unsafe fn construct<E>(error: E, vtable: &'static ErrorVTable<C>, context: Option<C>) -> Self
where
E: StdError + Send + Sync + 'static,
{
let inner = Box::new(ErrorImpl {
vtable,
context,
_object: error,
});
let erased = mem::transmute::<Box<ErrorImpl<E, C>>, Box<ErrorImpl<(), C>>>(inner);
let inner = ManuallyDrop::new(erased);
Report { inner }
}
pub fn wrap_err<D>(mut self, msg: D) -> Self
where
D: Display + Send + Sync + 'static,
{
let context = self.inner.context.take();
let error: ContextError<D, Report<C>> = ContextError { msg, error: self };
let vtable = &ErrorVTable {
object_drop: object_drop::<ContextError<D, Report<C>>, C>,
object_ref: object_ref::<ContextError<D, Report<C>>, C>,
#[cfg(feature = "std")]
object_mut: object_mut::<ContextError<D, Report<C>>, C>,
object_boxed: object_boxed::<ContextError<D, Report<C>>, C>,
object_downcast: context_chain_downcast::<D, C>,
object_drop_rest: context_chain_drop_rest::<D, C>,
};
unsafe { Report::construct(error, vtable, context) }
}
#[cfg(feature = "std")]
pub fn chain(&self) -> Chain<'_> {
self.inner.chain()
}
#[cfg(feature = "std")]
pub fn root_cause(&self) -> &(dyn StdError + 'static) {
let mut chain = self.chain();
let mut root_cause = chain.next().unwrap();
for cause in chain {
root_cause = cause;
}
root_cause
}
pub fn is<E>(&self) -> bool
where
E: Display + Debug + Send + Sync + 'static,
{
self.downcast_ref::<E>().is_some()
}
pub fn downcast<E>(self) -> Result<E, Self>
where
E: Display + Debug + Send + Sync + 'static,
{
let target = TypeId::of::<E>();
unsafe {
let addr = match (self.inner.vtable.object_downcast)(&self.inner, target) {
Some(addr) => addr,
None => return Err(self),
};
let outer = ManuallyDrop::new(self);
let error = ptr::read(addr.cast::<E>().as_ptr());
let inner = ptr::read(&outer.inner);
let erased = ManuallyDrop::into_inner(inner);
(erased.vtable.object_drop_rest)(erased, target);
Ok(error)
}
}
pub fn downcast_ref<E>(&self) -> Option<&E>
where
E: Display + Debug + Send + Sync + 'static,
{
let target = TypeId::of::<E>();
unsafe {
let addr = (self.inner.vtable.object_downcast)(&self.inner, target)?;
Some(&*addr.cast::<E>().as_ptr())
}
}
pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
where
E: Display + Debug + Send + Sync + 'static,
{
let target = TypeId::of::<E>();
unsafe {
let addr = (self.inner.vtable.object_downcast)(&self.inner, target)?;
Some(&mut *addr.cast::<E>().as_ptr())
}
}
pub fn context(&self) -> &C {
self.inner.context.as_ref().unwrap()
}
pub fn context_mut(&mut self) -> &mut C {
self.inner.context.as_mut().unwrap()
}
}
#[cfg(feature = "std")]
impl<E, C> From<E> for Report<C>
where
C: EyreContext,
E: StdError + Send + Sync + 'static,
{
fn from(error: E) -> Self {
Report::from_std(error)
}
}
#[cfg(feature = "std")]
impl<C: EyreContext> Deref for Report<C> {
type Target = dyn StdError + Send + Sync + 'static;
fn deref(&self) -> &Self::Target {
self.inner.error()
}
}
#[cfg(feature = "std")]
impl<C: EyreContext> DerefMut for Report<C> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.inner.error_mut()
}
}
impl<C: EyreContext> Display for Report<C> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.display(formatter)
}
}
impl<C: EyreContext> Debug for Report<C> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.debug(formatter)
}
}
impl<C> Drop for Report<C>
where
C: EyreContext,
{
fn drop(&mut self) {
unsafe {
let inner = ptr::read(&self.inner);
let erased = ManuallyDrop::into_inner(inner);
(erased.vtable.object_drop)(erased);
}
}
}
struct ErrorVTable<C>
where
C: EyreContext,
{
object_drop: unsafe fn(Box<ErrorImpl<(), C>>),
object_ref: unsafe fn(&ErrorImpl<(), C>) -> &(dyn StdError + Send + Sync + 'static),
#[cfg(feature = "std")]
object_mut: unsafe fn(&mut ErrorImpl<(), C>) -> &mut (dyn StdError + Send + Sync + 'static),
#[allow(clippy::type_complexity)]
object_boxed: unsafe fn(Box<ErrorImpl<(), C>>) -> Box<dyn StdError + Send + Sync + 'static>,
object_downcast: unsafe fn(&ErrorImpl<(), C>, TypeId) -> Option<NonNull<()>>,
object_drop_rest: unsafe fn(Box<ErrorImpl<(), C>>, TypeId),
}
unsafe fn object_drop<E, C>(e: Box<ErrorImpl<(), C>>)
where
C: EyreContext,
{
let unerased = mem::transmute::<Box<ErrorImpl<(), C>>, Box<ErrorImpl<E, C>>>(e);
drop(unerased);
}
unsafe fn object_drop_front<E, C>(e: Box<ErrorImpl<(), C>>, target: TypeId)
where
C: EyreContext,
{
let _ = target;
let unerased = mem::transmute::<Box<ErrorImpl<(), C>>, Box<ErrorImpl<ManuallyDrop<E>, C>>>(e);
drop(unerased);
}
unsafe fn object_ref<E, C>(e: &ErrorImpl<(), C>) -> &(dyn StdError + Send + Sync + 'static)
where
C: EyreContext,
E: StdError + Send + Sync + 'static,
{
&(*(e as *const ErrorImpl<(), C> as *const ErrorImpl<E, C>))._object
}
#[cfg(feature = "std")]
unsafe fn object_mut<E, C>(e: &mut ErrorImpl<(), C>) -> &mut (dyn StdError + Send + Sync + 'static)
where
C: EyreContext,
E: StdError + Send + Sync + 'static,
{
&mut (*(e as *mut ErrorImpl<(), C> as *mut ErrorImpl<E, C>))._object
}
unsafe fn object_boxed<E, C>(e: Box<ErrorImpl<(), C>>) -> Box<dyn StdError + Send + Sync + 'static>
where
C: EyreContext,
E: StdError + Send + Sync + 'static,
C: Send + Sync + 'static,
{
mem::transmute::<Box<ErrorImpl<(), C>>, Box<ErrorImpl<E, C>>>(e)
}
unsafe fn object_downcast<E, C>(e: &ErrorImpl<(), C>, target: TypeId) -> Option<NonNull<()>>
where
C: EyreContext,
E: 'static,
{
if TypeId::of::<E>() == target {
let unerased = e as *const ErrorImpl<(), C> as *const ErrorImpl<E, C>;
let addr = &(*unerased)._object as *const E as *mut ();
Some(NonNull::new_unchecked(addr))
} else {
None
}
}
#[cfg(feature = "std")]
unsafe fn context_downcast<D, E, C>(e: &ErrorImpl<(), C>, target: TypeId) -> Option<NonNull<()>>
where
C: EyreContext,
D: 'static,
E: 'static,
{
if TypeId::of::<D>() == target {
let unerased = e as *const ErrorImpl<(), C> as *const ErrorImpl<ContextError<D, E>, C>;
let addr = &(*unerased)._object.msg as *const D as *mut ();
Some(NonNull::new_unchecked(addr))
} else if TypeId::of::<E>() == target {
let unerased = e as *const ErrorImpl<(), C> as *const ErrorImpl<ContextError<D, E>, C>;
let addr = &(*unerased)._object.error as *const E as *mut ();
Some(NonNull::new_unchecked(addr))
} else {
None
}
}
#[cfg(feature = "std")]
unsafe fn context_drop_rest<D, E, C>(e: Box<ErrorImpl<(), C>>, target: TypeId)
where
C: EyreContext,
D: 'static,
E: 'static,
{
if TypeId::of::<D>() == target {
let unerased = mem::transmute::<
Box<ErrorImpl<(), C>>,
Box<ErrorImpl<ContextError<ManuallyDrop<D>, E>, C>>,
>(e);
drop(unerased);
} else {
let unerased = mem::transmute::<
Box<ErrorImpl<(), C>>,
Box<ErrorImpl<ContextError<D, ManuallyDrop<E>>, C>>,
>(e);
drop(unerased);
}
}
unsafe fn context_chain_downcast<D, C>(e: &ErrorImpl<(), C>, target: TypeId) -> Option<NonNull<()>>
where
C: EyreContext,
D: 'static,
{
if TypeId::of::<D>() == target {
let unerased =
e as *const ErrorImpl<(), C> as *const ErrorImpl<ContextError<D, Report<C>>, C>;
let addr = &(*unerased)._object.msg as *const D as *mut ();
Some(NonNull::new_unchecked(addr))
} else {
let unerased =
e as *const ErrorImpl<(), C> as *const ErrorImpl<ContextError<D, Report<C>>, C>;
let source = &(*unerased)._object.error;
(source.inner.vtable.object_downcast)(&source.inner, target)
}
}
unsafe fn context_chain_drop_rest<D, C>(e: Box<ErrorImpl<(), C>>, target: TypeId)
where
C: EyreContext,
D: 'static,
{
if TypeId::of::<D>() == target {
let unerased = mem::transmute::<
Box<ErrorImpl<(), C>>,
Box<ErrorImpl<ContextError<ManuallyDrop<D>, Report<C>>, C>>,
>(e);
drop(unerased);
} else {
let unerased = mem::transmute::<
Box<ErrorImpl<(), C>>,
Box<ErrorImpl<ContextError<D, ManuallyDrop<Report<C>>>, C>>,
>(e);
let inner = ptr::read(&unerased._object.error.inner);
drop(unerased);
let erased = ManuallyDrop::into_inner(inner);
(erased.vtable.object_drop_rest)(erased, target);
}
}
#[repr(C)]
pub(crate) struct ErrorImpl<E, C>
where
C: EyreContext,
{
vtable: &'static ErrorVTable<C>,
pub(crate) context: Option<C>,
_object: E,
}
#[repr(C)]
pub(crate) struct ContextError<D, E> {
pub(crate) msg: D,
pub(crate) error: E,
}
impl<E, C> ErrorImpl<E, C>
where
C: EyreContext,
{
fn erase(&self) -> &ErrorImpl<(), C> {
unsafe { &*(self as *const ErrorImpl<E, C> as *const ErrorImpl<(), C>) }
}
}
impl<C> ErrorImpl<(), C>
where
C: EyreContext,
{
pub(crate) fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {
unsafe { &*(self.vtable.object_ref)(self) }
}
#[cfg(feature = "std")]
pub(crate) fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
unsafe { &mut *(self.vtable.object_mut)(self) }
}
pub(crate) fn chain(&self) -> Chain<'_> {
Chain::new(self.error())
}
}
impl<E, C> StdError for ErrorImpl<E, C>
where
C: EyreContext,
E: StdError,
{
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.erase().error().source()
}
}
impl<E, C> Debug for ErrorImpl<E, C>
where
C: EyreContext,
E: Debug,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.erase().debug(formatter)
}
}
impl<E, C> Display for ErrorImpl<E, C>
where
C: EyreContext,
E: Display,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.erase().error(), formatter)
}
}
impl<C: EyreContext> From<Report<C>> for Box<dyn StdError + Send + Sync + 'static> {
fn from(error: Report<C>) -> Self {
let outer = ManuallyDrop::new(error);
unsafe {
let inner = ptr::read(&outer.inner);
let erased = ManuallyDrop::into_inner(inner);
(erased.vtable.object_boxed)(erased)
}
}
}
impl<C: EyreContext> From<Report<C>> for Box<dyn StdError + 'static> {
fn from(error: Report<C>) -> Self {
Box::<dyn StdError + Send + Sync>::from(error)
}
}
#[cfg(feature = "std")]
impl<C: EyreContext> AsRef<dyn StdError + Send + Sync> for Report<C> {
fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {
&**self
}
}
#[cfg(feature = "std")]
impl<C: EyreContext> AsRef<dyn StdError> for Report<C> {
fn as_ref(&self) -> &(dyn StdError + 'static) {
&**self
}
}