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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use alloc::boxed::Box;
use core::{
    fmt,
    ops::{Deref, DerefMut},
};

/// A pair of values, one of which is expected and one of which is actual.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GotExpected<T> {
    /// The actual value.
    pub got: T,
    /// The expected value.
    pub expected: T,
}

impl<T: fmt::Display> fmt::Display for GotExpected<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "got {}, expected {}", self.got, self.expected)
    }
}

#[cfg(feature = "std")]
impl<T: fmt::Debug + fmt::Display> std::error::Error for GotExpected<T> {}

impl<T> From<(T, T)> for GotExpected<T> {
    #[inline]
    fn from((got, expected): (T, T)) -> Self {
        Self::new(got, expected)
    }
}

impl<T> GotExpected<T> {
    /// Creates a new error from a pair of values.
    #[inline]
    pub const fn new(got: T, expected: T) -> Self {
        Self { got, expected }
    }
}

/// A pair of values, one of which is expected and one of which is actual.
///
/// Same as [`GotExpected`], but [`Box`]ed for smaller size.
///
/// Prefer instantiating using [`GotExpected`], and then using `.into()` to convert to this type.
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GotExpectedBoxed<T>(pub Box<GotExpected<T>>);

impl<T: fmt::Debug> fmt::Debug for GotExpectedBoxed<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<T: fmt::Display> fmt::Display for GotExpectedBoxed<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(feature = "std")]
impl<T: fmt::Debug + fmt::Display> std::error::Error for GotExpectedBoxed<T> {}

impl<T> Deref for GotExpectedBoxed<T> {
    type Target = GotExpected<T>;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for GotExpectedBoxed<T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T> From<(T, T)> for GotExpectedBoxed<T> {
    #[inline]
    fn from(value: (T, T)) -> Self {
        Self(Box::new(GotExpected::from(value)))
    }
}

impl<T> From<GotExpected<T>> for GotExpectedBoxed<T> {
    #[inline]
    fn from(value: GotExpected<T>) -> Self {
        Self(Box::new(value))
    }
}