error_stack/
error.rs

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