reth_primitives_traits/
error.rs

1use alloc::boxed::Box;
2use core::ops::{Deref, DerefMut};
3
4/// A pair of values, one of which is expected and one of which is actual.
5#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
6#[error("got {got}, expected {expected}")]
7pub struct GotExpected<T> {
8    /// The actual value.
9    pub got: T,
10    /// The expected value.
11    pub expected: T,
12}
13
14impl<T> From<(T, T)> for GotExpected<T> {
15    #[inline]
16    fn from((got, expected): (T, T)) -> Self {
17        Self::new(got, expected)
18    }
19}
20
21impl<T> GotExpected<T> {
22    /// Creates a new error from a pair of values.
23    #[inline]
24    pub const fn new(got: T, expected: T) -> Self {
25        Self { got, expected }
26    }
27}
28
29/// A pair of values, one of which is expected and one of which is actual.
30///
31/// Same as [`GotExpected`], but [`Box`]ed for smaller size.
32///
33/// Prefer instantiating using [`GotExpected`], and then using `.into()` to convert to this type.
34#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error, Debug)]
35#[error(transparent)]
36pub struct GotExpectedBoxed<T>(pub Box<GotExpected<T>>);
37
38impl<T> Deref for GotExpectedBoxed<T> {
39    type Target = GotExpected<T>;
40
41    #[inline(always)]
42    fn deref(&self) -> &Self::Target {
43        &self.0
44    }
45}
46
47impl<T> DerefMut for GotExpectedBoxed<T> {
48    #[inline(always)]
49    fn deref_mut(&mut self) -> &mut Self::Target {
50        &mut self.0
51    }
52}
53
54impl<T> From<(T, T)> for GotExpectedBoxed<T> {
55    #[inline]
56    fn from(value: (T, T)) -> Self {
57        Self(Box::new(GotExpected::from(value)))
58    }
59}
60
61impl<T> From<GotExpected<T>> for GotExpectedBoxed<T> {
62    #[inline]
63    fn from(value: GotExpected<T>) -> Self {
64        Self(Box::new(value))
65    }
66}