error_stack/
error.rs

1#[cfg(nightly)]
2use core::error::Request;
3use core::{error::Error, fmt, ptr};
4
5use crate::Report;
6
7#[repr(transparent)]
8pub(crate) struct ReportError<C>(Report<C>);
9
10impl<C> ReportError<C> {
11    pub(crate) const fn new(report: Report<C>) -> Self {
12        Self(report)
13    }
14
15    pub(crate) const fn from_ref(report: &Report<C>) -> &Self {
16        // SAFETY: `ReportError` is a `repr(transparent)` wrapper around `Report`.
17        unsafe { &*ptr::from_ref(report).cast() }
18    }
19}
20
21impl<C> fmt::Debug for ReportError<C> {
22    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
23        fmt::Debug::fmt(&self.0, fmt)
24    }
25}
26
27impl<C> fmt::Display for ReportError<C> {
28    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
29        fmt::Display::fmt(&self.0, fmt)
30    }
31}
32
33impl<C> Error for ReportError<C> {
34    #[cfg(nightly)]
35    fn provide<'a>(&'a self, request: &mut Request<'a>) {
36        self.0
37            .frames()
38            .for_each(|frame| frame.as_error().provide(request));
39    }
40}