eyre/
kind.rs

1#![allow(missing_debug_implementations, missing_docs)]
2// Tagged dispatch mechanism for resolving the behavior of `eyre!($expr)`.
3//
4// When eyre! is given a single expr argument to turn into eyre::Report, we
5// want the resulting Report to pick up the input's implementation of source()
6// and backtrace() if it has a std::error::Error impl, otherwise require nothing
7// more than Display and Debug.
8//
9// Expressed in terms of specialization, we want something like:
10//
11//     trait EyreNew {
12//         fn new(self) -> Report;
13//     }
14//
15//     impl<T> EyreNew for T
16//     where
17//         T: Display + Debug + Send + Sync + 'static,
18//     {
19//         default fn new(self) -> Report {
20//             /* no std error impl */
21//         }
22//     }
23//
24//     impl<T> EyreNew for T
25//     where
26//         T: std::error::Error + Send + Sync + 'static,
27//     {
28//         fn new(self) -> Report {
29//             /* use std error's source() and backtrace() */
30//         }
31//     }
32//
33// Since specialization is not stable yet, instead we rely on autoref behavior
34// of method resolution to perform tagged dispatch. Here we have two traits
35// AdhocKind and TraitKind that both have an eyre_kind() method. AdhocKind is
36// implemented whether or not the caller's type has a std error impl, while
37// TraitKind is implemented only when a std error impl does exist. The ambiguity
38// is resolved by AdhocKind requiring an extra autoref so that it has lower
39// precedence.
40//
41// The eyre! macro will set up the call in this form:
42//
43//     #[allow(unused_imports)]
44//     use $crate::private::{AdhocKind, TraitKind};
45//     let error = $msg;
46//     (&error).eyre_kind().new(error)
47
48use crate::Report;
49use core::fmt::{Debug, Display};
50
51use crate::StdError;
52
53pub struct Adhoc;
54
55pub trait AdhocKind: Sized {
56    #[inline]
57    fn eyre_kind(&self) -> Adhoc {
58        Adhoc
59    }
60}
61
62impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}
63
64impl Adhoc {
65    #[cfg_attr(track_caller, track_caller)]
66    pub fn new<M>(self, message: M) -> Report
67    where
68        M: Display + Debug + Send + Sync + 'static,
69    {
70        Report::from_adhoc(message)
71    }
72}
73
74pub struct Trait;
75
76pub trait TraitKind: Sized {
77    #[inline]
78    fn eyre_kind(&self) -> Trait {
79        Trait
80    }
81}
82
83impl<E> TraitKind for E where E: Into<Report> {}
84
85impl Trait {
86    #[cfg_attr(track_caller, track_caller)]
87    pub fn new<E>(self, error: E) -> Report
88    where
89        E: Into<Report>,
90    {
91        error.into()
92    }
93}
94
95pub struct Boxed;
96
97pub trait BoxedKind: Sized {
98    #[inline]
99    fn eyre_kind(&self) -> Boxed {
100        Boxed
101    }
102}
103
104impl BoxedKind for Box<dyn StdError + Send + Sync> {}
105
106impl Boxed {
107    #[cfg_attr(track_caller, track_caller)]
108    pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Report {
109        Report::from_boxed(error)
110    }
111}