use crate::chain::Chain;
use crate::ptr::{MutPtr, OwnedPtr, RefPtr};
use crate::EyreHandler;
use crate::{Report, StdError};
use core::any::TypeId;
use core::fmt::{self, Debug, Display};
use core::mem::{self, ManuallyDrop};
use core::ptr::{self, NonNull};
use core::ops::{Deref, DerefMut};
impl Report {
#[cfg_attr(track_caller, track_caller)]
pub fn new<E>(error: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
Report::from_std(error)
}
#[cfg_attr(track_caller, track_caller)]
pub fn msg<M>(message: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
{
Report::from_adhoc(message)
}
#[cfg_attr(track_caller, track_caller)]
pub(crate) fn from_std<E>(error: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
let vtable = &ErrorVTable {
object_drop: object_drop::<E>,
object_ref: object_ref::<E>,
object_mut: object_mut::<E>,
object_boxed: object_boxed::<E>,
object_downcast: object_downcast::<E>,
object_downcast_mut: object_downcast_mut::<E>,
object_drop_rest: object_drop_front::<E>,
};
let handler = Some(crate::capture_handler(&error));
unsafe { Report::construct(error, vtable, handler) }
}
#[cfg_attr(track_caller, track_caller)]
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>>,
object_ref: object_ref::<MessageError<M>>,
object_mut: object_mut::<MessageError<M>>,
object_boxed: object_boxed::<MessageError<M>>,
object_downcast: object_downcast::<M>,
object_downcast_mut: object_downcast_mut::<M>,
object_drop_rest: object_drop_front::<M>,
};
let handler = Some(crate::capture_handler(&error));
unsafe { Report::construct(error, vtable, handler) }
}
#[cfg_attr(track_caller, track_caller)]
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>>,
object_ref: object_ref::<DisplayError<M>>,
object_mut: object_mut::<DisplayError<M>>,
object_boxed: object_boxed::<DisplayError<M>>,
object_downcast: object_downcast::<M>,
object_downcast_mut: object_downcast_mut::<M>,
object_drop_rest: object_drop_front::<M>,
};
let handler = Some(crate::capture_handler(&NoneError));
unsafe { Report::construct(error, vtable, handler) }
}
#[cfg_attr(track_caller, track_caller)]
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>>,
object_ref: object_ref::<ContextError<D, E>>,
object_mut: object_mut::<ContextError<D, E>>,
object_boxed: object_boxed::<ContextError<D, E>>,
object_downcast: context_downcast::<D, E>,
object_downcast_mut: context_downcast_mut::<D, E>,
object_drop_rest: context_drop_rest::<D, E>,
};
let handler = Some(crate::capture_handler(&error));
unsafe { Report::construct(error, vtable, handler) }
}
#[cfg_attr(track_caller, track_caller)]
pub(crate) fn from_boxed(error: Box<dyn StdError + Send + Sync>) -> Self {
use crate::wrapper::BoxedError;
let error = BoxedError(error);
let handler = Some(crate::capture_handler(&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_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,
object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,
};
unsafe { Report::construct(error, vtable, handler) }
}
unsafe fn construct<E>(
error: E,
vtable: &'static ErrorVTable,
handler: Option<Box<dyn EyreHandler>>,
) -> Self
where
E: StdError + Send + Sync + 'static,
{
let inner = ErrorImpl {
header: ErrorHeader { vtable, handler },
_object: error,
};
let ptr = OwnedPtr::<ErrorImpl<E>>::new(inner);
let ptr = ptr.cast::<ErrorImpl<()>>();
Report { inner: ptr }
}
pub fn wrap_err<D>(mut self, msg: D) -> Self
where
D: Display + Send + Sync + 'static,
{
let handler = header_mut(self.inner.as_mut()).handler.take();
let error: ContextError<D, Report> = ContextError { msg, error: self };
let vtable = &ErrorVTable {
object_drop: object_drop::<ContextError<D, Report>>,
object_ref: object_ref::<ContextError<D, Report>>,
object_mut: object_mut::<ContextError<D, Report>>,
object_boxed: object_boxed::<ContextError<D, Report>>,
object_downcast: context_chain_downcast::<D>,
object_downcast_mut: context_chain_downcast_mut::<D>,
object_drop_rest: context_chain_drop_rest::<D>,
};
unsafe { Report::construct(error, vtable, handler) }
}
fn vtable(&self) -> &'static ErrorVTable {
header(self.inner.as_ref()).vtable
}
pub fn chain(&self) -> Chain<'_> {
ErrorImpl::chain(self.inner.as_ref())
}
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.vtable().object_downcast)(self.inner.as_ref(), 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);
(outer.vtable().object_drop_rest)(inner, 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.vtable().object_downcast)(self.inner.as_ref(), target)?;
Some(addr.cast::<E>().as_ref())
}
}
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.vtable().object_downcast_mut)(self.inner.as_mut(), target)?;
Some(addr.cast::<E>().as_mut())
}
}
pub fn handler(&self) -> &dyn EyreHandler {
header(self.inner.as_ref())
.handler
.as_ref()
.unwrap()
.as_ref()
}
pub fn handler_mut(&mut self) -> &mut dyn EyreHandler {
header_mut(self.inner.as_mut())
.handler
.as_mut()
.unwrap()
.as_mut()
}
#[doc(hidden)]
pub fn context(&self) -> &dyn EyreHandler {
header(self.inner.as_ref())
.handler
.as_ref()
.unwrap()
.as_ref()
}
#[doc(hidden)]
pub fn context_mut(&mut self) -> &mut dyn EyreHandler {
header_mut(self.inner.as_mut())
.handler
.as_mut()
.unwrap()
.as_mut()
}
}
impl<E> From<E> for Report
where
E: StdError + Send + Sync + 'static,
{
#[cfg_attr(track_caller, track_caller)]
fn from(error: E) -> Self {
Report::from_std(error)
}
}
impl Deref for Report {
type Target = dyn StdError + Send + Sync + 'static;
fn deref(&self) -> &Self::Target {
ErrorImpl::error(self.inner.as_ref())
}
}
impl DerefMut for Report {
fn deref_mut(&mut self) -> &mut Self::Target {
ErrorImpl::error_mut(self.inner.as_mut())
}
}
impl Display for Report {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
ErrorImpl::display(self.inner.as_ref(), formatter)
}
}
impl Debug for Report {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
ErrorImpl::debug(self.inner.as_ref(), formatter)
}
}
impl Drop for Report {
fn drop(&mut self) {
unsafe {
(self.vtable().object_drop)(self.inner);
}
}
}
struct ErrorVTable {
object_drop: unsafe fn(OwnedPtr<ErrorImpl<()>>),
object_ref: unsafe fn(RefPtr<'_, ErrorImpl<()>>) -> &(dyn StdError + Send + Sync + 'static),
object_mut: unsafe fn(MutPtr<'_, ErrorImpl<()>>) -> &mut (dyn StdError + Send + Sync + 'static),
#[allow(clippy::type_complexity)]
object_boxed: unsafe fn(OwnedPtr<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>,
object_downcast: unsafe fn(RefPtr<'_, ErrorImpl<()>>, TypeId) -> Option<NonNull<()>>,
object_downcast_mut: unsafe fn(MutPtr<'_, ErrorImpl<()>>, TypeId) -> Option<NonNull<()>>,
object_drop_rest: unsafe fn(OwnedPtr<ErrorImpl<()>>, TypeId),
}
unsafe fn object_drop<E>(e: OwnedPtr<ErrorImpl<()>>) {
let unerased = unsafe { e.cast::<ErrorImpl<E>>().into_box() };
drop(unerased);
}
unsafe fn object_drop_front<E>(e: OwnedPtr<ErrorImpl<()>>, target: TypeId) {
let _ = target;
let unerased = unsafe { e.cast::<ErrorImpl<E>>().into_box() };
mem::forget(unerased._object)
}
unsafe fn object_ref<E>(e: RefPtr<'_, ErrorImpl<()>>) -> &(dyn StdError + Send + Sync + 'static)
where
E: StdError + Send + Sync + 'static,
{
&unsafe { e.cast::<ErrorImpl<E>>().as_ref() }._object
}
unsafe fn object_mut<E>(e: MutPtr<'_, ErrorImpl<()>>) -> &mut (dyn StdError + Send + Sync + 'static)
where
E: StdError + Send + Sync + 'static,
{
&mut unsafe { e.cast::<ErrorImpl<E>>().into_mut() }._object
}
unsafe fn object_boxed<E>(e: OwnedPtr<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>
where
E: StdError + Send + Sync + 'static,
{
unsafe { e.cast::<ErrorImpl<E>>().into_box() }
}
unsafe fn object_downcast<E>(e: RefPtr<'_, ErrorImpl<()>>, target: TypeId) -> Option<NonNull<()>>
where
E: 'static,
{
if TypeId::of::<E>() == target {
let unerased = unsafe { e.cast::<ErrorImpl<E>>().as_ref() };
Some(NonNull::from(&(unerased._object)).cast::<()>())
} else {
None
}
}
unsafe fn object_downcast_mut<E>(
e: MutPtr<'_, ErrorImpl<()>>,
target: TypeId,
) -> Option<NonNull<()>>
where
E: 'static,
{
if TypeId::of::<E>() == target {
let unerased = unsafe { e.cast::<ErrorImpl<E>>().into_mut() };
Some(NonNull::from(&mut (unerased._object)).cast::<()>())
} else {
None
}
}
unsafe fn context_downcast<D, E>(
e: RefPtr<'_, ErrorImpl<()>>,
target: TypeId,
) -> Option<NonNull<()>>
where
D: 'static,
E: 'static,
{
if TypeId::of::<D>() == target {
let unerased = unsafe { e.cast::<ErrorImpl<ContextError<D, E>>>().as_ref() };
let addr = NonNull::from(&unerased._object.msg).cast::<()>();
Some(addr)
} else if TypeId::of::<E>() == target {
let unerased = unsafe { e.cast::<ErrorImpl<ContextError<D, E>>>().as_ref() };
let addr = NonNull::from(&unerased._object.error).cast::<()>();
Some(addr)
} else {
None
}
}
unsafe fn context_downcast_mut<D, E>(
e: MutPtr<'_, ErrorImpl<()>>,
target: TypeId,
) -> Option<NonNull<()>>
where
D: 'static,
E: 'static,
{
if TypeId::of::<D>() == target {
let unerased = unsafe { e.cast::<ErrorImpl<ContextError<D, E>>>().into_mut() };
let addr = NonNull::from(&unerased._object.msg).cast::<()>();
Some(addr)
} else if TypeId::of::<E>() == target {
let unerased = unsafe { e.cast::<ErrorImpl<ContextError<D, E>>>().into_mut() };
let addr = NonNull::from(&mut unerased._object.error).cast::<()>();
Some(addr)
} else {
None
}
}
unsafe fn context_drop_rest<D, E>(e: OwnedPtr<ErrorImpl<()>>, target: TypeId)
where
D: 'static,
E: 'static,
{
if TypeId::of::<D>() == target {
unsafe {
e.cast::<ErrorImpl<ContextError<ManuallyDrop<D>, E>>>()
.into_box()
};
} else {
debug_assert_eq!(TypeId::of::<E>(), target);
unsafe {
e.cast::<ErrorImpl<ContextError<D, ManuallyDrop<E>>>>()
.into_box()
};
}
}
unsafe fn context_chain_downcast<D>(
e: RefPtr<'_, ErrorImpl<()>>,
target: TypeId,
) -> Option<NonNull<()>>
where
D: 'static,
{
let unerased = unsafe { e.cast::<ErrorImpl<ContextError<D, Report>>>().as_ref() };
if TypeId::of::<D>() == target {
let addr = NonNull::from(&unerased._object.msg).cast::<()>();
Some(addr)
} else {
let source = &unerased._object.error;
unsafe { (source.vtable().object_downcast)(source.inner.as_ref(), target) }
}
}
unsafe fn context_chain_downcast_mut<D>(
e: MutPtr<'_, ErrorImpl<()>>,
target: TypeId,
) -> Option<NonNull<()>>
where
D: 'static,
{
let unerased = unsafe { e.cast::<ErrorImpl<ContextError<D, Report>>>().into_mut() };
if TypeId::of::<D>() == target {
let addr = NonNull::from(&unerased._object.msg).cast::<()>();
Some(addr)
} else {
let source = &mut unerased._object.error;
unsafe { (source.vtable().object_downcast_mut)(source.inner.as_mut(), target) }
}
}
unsafe fn context_chain_drop_rest<D>(e: OwnedPtr<ErrorImpl<()>>, target: TypeId)
where
D: 'static,
{
if TypeId::of::<D>() == target {
let unerased = unsafe {
e.cast::<ErrorImpl<ContextError<ManuallyDrop<D>, Report>>>()
.into_box()
};
drop(unerased);
} else {
unsafe {
let unerased = e
.cast::<ErrorImpl<ContextError<D, ManuallyDrop<Report>>>>()
.into_box();
let inner = ptr::read(&unerased.as_ref()._object.error.inner);
drop(unerased);
(header(inner.as_ref()).vtable.object_drop_rest)(inner, target);
}
}
}
#[repr(C)]
pub(crate) struct ErrorHeader {
vtable: &'static ErrorVTable,
pub(crate) handler: Option<Box<dyn EyreHandler>>,
}
#[repr(C)]
pub(crate) struct ErrorImpl<E = ()> {
header: ErrorHeader,
_object: E,
}
#[repr(C)]
pub(crate) struct ContextError<D, E> {
pub(crate) msg: D,
pub(crate) error: E,
}
impl<E> ErrorImpl<E> {
fn erase(&self) -> RefPtr<'_, ErrorImpl<()>> {
RefPtr::new(self).cast()
}
}
fn header(p: RefPtr<'_, ErrorImpl<()>>) -> &'_ ErrorHeader {
unsafe { p.cast().as_ref() }
}
fn header_mut(p: MutPtr<'_, ErrorImpl<()>>) -> &mut ErrorHeader {
unsafe { p.cast().into_mut() }
}
impl ErrorImpl<()> {
pub(crate) fn error(this: RefPtr<'_, Self>) -> &(dyn StdError + Send + Sync + 'static) {
unsafe { (header(this).vtable.object_ref)(this) }
}
pub(crate) fn error_mut(this: MutPtr<'_, Self>) -> &mut (dyn StdError + Send + Sync + 'static) {
unsafe { (header_mut(this).vtable.object_mut)(this) }
}
pub(crate) fn chain(this: RefPtr<'_, Self>) -> Chain<'_> {
Chain::new(Self::error(this))
}
pub(crate) fn header(this: RefPtr<'_, ErrorImpl>) -> &ErrorHeader {
header(this)
}
}
impl<E> StdError for ErrorImpl<E>
where
E: StdError,
{
fn source(&self) -> Option<&(dyn StdError + 'static)> {
ErrorImpl::<()>::error(self.erase()).source()
}
}
impl<E> Debug for ErrorImpl<E>
where
E: Debug,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
ErrorImpl::debug(self.erase(), formatter)
}
}
impl<E> Display for ErrorImpl<E>
where
E: Display,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(ErrorImpl::error(self.erase()), formatter)
}
}
impl From<Report> for Box<dyn StdError + Send + Sync + 'static> {
fn from(error: Report) -> Self {
let outer = ManuallyDrop::new(error);
unsafe {
(header(outer.inner.as_ref()).vtable.object_boxed)(outer.inner)
}
}
}
impl From<Report> for Box<dyn StdError + 'static> {
fn from(error: Report) -> Self {
Box::<dyn StdError + Send + Sync>::from(error)
}
}
impl AsRef<dyn StdError + Send + Sync> for Report {
fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {
&**self
}
}
impl AsRef<dyn StdError> for Report {
fn as_ref(&self) -> &(dyn StdError + 'static) {
&**self
}
}
#[cfg(feature = "pyo3")]
mod pyo3_compat;