use std::hash::{Hash, Hasher}; use std::convert::Infallible; /// Analagous to [`PartialEq`], but for fallible conversions. pub trait TryPartialEq { /// The error to return. type Error; /// Try to compare `self` to `rhs`, returning an [`Error`](Self::Error) if it's not possible. fn try_eq(&self, rhs: &Rhs) -> Result; } impl, Rhs> TryPartialEq for T { type Error = Infallible; #[inline] fn try_eq(&self, rhs: &Rhs) -> Result { Ok(self == rhs) } } /// Analogous to [`Eq`], but for fallible conversions. pub trait TryEq : TryPartialEq {} impl TryEq for T {} /// Try to hash something, returning an error if it's not possible. pub trait TryHash { /// The error to return. type Error; /// Try to hash `self`, returning an [`Error`](Self::Error) if it's not possible. fn try_hash(&self, hasher: &mut H) -> Result<(), Self::Error>; } impl TryHash for T { type Error = Infallible; #[inline] fn try_hash(&self, hasher: &mut H) -> Result<(), Self::Error> { self.hash(hasher); Ok(()) } }