diff --git a/hashes/src/error.rs b/hashes/src/error.rs index 2c6941f9c..bac9ba649 100644 --- a/hashes/src/error.rs +++ b/hashes/src/error.rs @@ -8,6 +8,8 @@ use core::fmt; #[derive(Debug, Clone, PartialEq, Eq)] pub struct FromSliceError(pub(crate) FromSliceErrorInner); +impl_from_infallible!(FromSliceError); + impl FromSliceError { /// Returns the expected slice length. pub fn expected_length(&self) -> usize { self.0.expected } @@ -23,6 +25,8 @@ pub(crate) struct FromSliceErrorInner { pub(crate) got: usize, } +impl_from_infallible!(FromSliceErrorInner); + impl fmt::Display for FromSliceError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "invalid slice length {} (expected {})", self.0.got, self.0.expected) @@ -31,3 +35,19 @@ impl fmt::Display for FromSliceError { #[cfg(feature = "std")] impl std::error::Error for FromSliceError {} + +/// Derives `From` for the given type. +// This is a duplicate of `internals::impl_from_infallible`, see there for complete docs. +#[doc(hidden)] +macro_rules! impl_from_infallible { + ( $name:ident $(< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? ) => { + impl $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? + From + for $name + $(< $( $lt ),+ >)? + { + fn from(never: core::convert::Infallible) -> Self { match never {} } + } + } +} +pub(crate) use impl_from_infallible;