diff --git a/hashes/src/error.rs b/hashes/src/error.rs index a2eaa67a7..2c6941f9c 100644 --- a/hashes/src/error.rs +++ b/hashes/src/error.rs @@ -6,22 +6,26 @@ use core::fmt; /// Attempted to create a hash from an invalid length slice. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct FromSliceError { +pub struct FromSliceError(pub(crate) FromSliceErrorInner); + +impl FromSliceError { + /// Returns the expected slice length. + pub fn expected_length(&self) -> usize { self.0.expected } + + /// Returns the invalid slice length. + pub fn invalid_length(&self) -> usize { self.0.got } +} + +/// Attempted to create a hash from an invalid length slice. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct FromSliceErrorInner { pub(crate) expected: usize, pub(crate) got: usize, } -impl FromSliceError { - /// Returns the expected slice length. - pub fn expected_length(&self) -> usize { self.expected } - - /// Returns the invalid slice length. - pub fn invalid_length(&self) -> usize { self.got } -} - impl fmt::Display for FromSliceError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "invalid slice length {} (expected {})", self.got, self.expected) + write!(f, "invalid slice length {} (expected {})", self.0.got, self.0.expected) } } diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index 2e4701b38..46480863c 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -129,7 +129,10 @@ macro_rules! hash_type_no_default { sl: &[u8], ) -> $crate::_export::_core::result::Result { if sl.len() != $bits / 8 { - Err($crate::FromSliceError { expected: $bits / 8, got: sl.len() }) + Err($crate::FromSliceError($crate::error::FromSliceErrorInner { + expected: $bits / 8, + got: sl.len(), + })) } else { let mut ret = [0; $bits / 8]; ret.copy_from_slice(sl); diff --git a/hashes/src/sha256t.rs b/hashes/src/sha256t.rs index 8e6faab73..99ba729c6 100644 --- a/hashes/src/sha256t.rs +++ b/hashes/src/sha256t.rs @@ -45,8 +45,10 @@ where /// Copies a byte slice into a hash object. #[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] pub fn from_slice(sl: &[u8]) -> Result, FromSliceError> { + use crate::error::FromSliceErrorInner; + if sl.len() != 32 { - Err(FromSliceError { expected: 32, got: sl.len() }) + Err(FromSliceError(FromSliceErrorInner { expected: 32, got: sl.len() })) } else { let mut ret = [0; 32]; ret.copy_from_slice(sl);