use core::{ fmt, ops::{Deref, DerefMut}, }; #[cfg(not(feature = "std"))] use alloc::boxed::Box; #[cfg(not(feature = "std"))] extern crate alloc; /// 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 { /// The actual value. pub got: T, /// The expected value. pub expected: T, } impl fmt::Display for GotExpected { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "got {}, expected {}", self.got, self.expected) } } #[cfg(feature = "std")] impl std::error::Error for GotExpected {} impl From<(T, T)> for GotExpected { #[inline] fn from((got, expected): (T, T)) -> Self { Self::new(got, expected) } } impl GotExpected { /// 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(pub Box>); impl fmt::Debug for GotExpectedBoxed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl fmt::Display for GotExpectedBoxed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } #[cfg(feature = "std")] impl std::error::Error for GotExpectedBoxed {} impl Deref for GotExpectedBoxed { type Target = GotExpected; #[inline(always)] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for GotExpectedBoxed { #[inline(always)] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl From<(T, T)> for GotExpectedBoxed { #[inline] fn from(value: (T, T)) -> Self { Self(Box::new(GotExpected::from(value))) } } impl From> for GotExpectedBoxed { #[inline] fn from(value: GotExpected) -> Self { Self(Box::new(value)) } }