1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Error type which is generic around a `Kind`

use crate::{BoxError, Context};
use std::{
    fmt::{self, Debug, Display},
    ops::Deref,
};

/// Error type which is generic around a `Kind`.
///
/// Provides a `Box`-ed wrapper around a [`Context`], ensuring error
/// propagation is a cheap operation (pointer copy).
#[derive(Debug)]
pub struct Error<Kind>(Box<Context<Kind>>)
where
    Kind: Clone + Debug + Display + Into<BoxError>;

impl<Kind> Deref for Error<Kind>
where
    Kind: Clone + Debug + Display + Into<BoxError>,
{
    type Target = Context<Kind>;

    fn deref(&self) -> &Context<Kind> {
        &self.0
    }
}

impl<Kind> Display for Error<Kind>
where
    Kind: Clone + Debug + Display + Into<BoxError>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<Kind> std::error::Error for Error<Kind>
where
    Kind: Clone + Debug + Display + Into<BoxError>,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.0.source()
    }
}

impl<Kind> From<Kind> for Error<Kind>
where
    Kind: Clone + Debug + Display + Into<BoxError>,
{
    fn from(kind: Kind) -> Self {
        Context::new(kind, None).into()
    }
}

impl<Kind> From<Context<Kind>> for Error<Kind>
where
    Kind: Clone + Debug + Display + Into<BoxError>,
{
    fn from(context: Context<Kind>) -> Self {
        Error(Box::new(context))
    }
}