eyre/
wrapper.rs

1use crate::StdError;
2use core::fmt::{self, Debug, Display};
3
4#[repr(transparent)]
5pub(crate) struct DisplayError<M>(pub(crate) M);
6
7#[repr(transparent)]
8/// Wraps a Debug + Display type as an error.
9///
10/// Its Debug and Display impls are the same as the wrapped type.
11pub(crate) struct MessageError<M>(pub(crate) M);
12
13pub(crate) struct NoneError;
14
15impl<M> Debug for DisplayError<M>
16where
17    M: Display,
18{
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        Display::fmt(&self.0, f)
21    }
22}
23
24impl<M> Display for DisplayError<M>
25where
26    M: Display,
27{
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        Display::fmt(&self.0, f)
30    }
31}
32
33impl<M> StdError for DisplayError<M> where M: Display + 'static {}
34
35impl<M> Debug for MessageError<M>
36where
37    M: Display + Debug,
38{
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        Debug::fmt(&self.0, f)
41    }
42}
43
44impl<M> Display for MessageError<M>
45where
46    M: Display + Debug,
47{
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        Display::fmt(&self.0, f)
50    }
51}
52
53impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}
54
55impl Debug for NoneError {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        Debug::fmt("Option was None", f)
58    }
59}
60
61impl Display for NoneError {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        Display::fmt("Option was None", f)
64    }
65}
66
67impl StdError for NoneError {}
68
69#[repr(transparent)]
70pub(crate) struct BoxedError(pub(crate) Box<dyn StdError + Send + Sync>);
71
72impl Debug for BoxedError {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        Debug::fmt(&self.0, f)
75    }
76}
77
78impl Display for BoxedError {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        Display::fmt(&self.0, f)
81    }
82}
83
84impl StdError for BoxedError {
85    #[cfg(backtrace)]
86    fn backtrace(&self) -> Option<&crate::backtrace::Backtrace> {
87        self.0.backtrace()
88    }
89
90    fn source(&self) -> Option<&(dyn StdError + 'static)> {
91        self.0.source()
92    }
93}