From 5232bba62b4bd22057fe26780ba6256aad124181 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 5 Nov 2024 09:57:34 +1100 Subject: [PATCH 1/3] hashes: Move FromSliceError to submodule Add a public `error` module and move the one error for the `hashes` crate into it. Re-export at the crate root. Done in preparation for adding an additional error type. --- hashes/src/error.rs | 29 +++++++++++++++++++++++++++++ hashes/src/lib.rs | 26 ++------------------------ 2 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 hashes/src/error.rs diff --git a/hashes/src/error.rs b/hashes/src/error.rs new file mode 100644 index 000000000..a2eaa67a7 --- /dev/null +++ b/hashes/src/error.rs @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: CC0-1.0 + +//! Error code for the `hashes` crate. + +use core::fmt; + +/// Attempted to create a hash from an invalid length slice. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FromSliceError { + 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) + } +} + +#[cfg(feature = "std")] +impl std::error::Error for FromSliceError {} diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 00ca7fd70..c3dda3892 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -96,6 +96,7 @@ pub mod _export { mod internal_macros; pub mod cmp; +pub mod error; pub mod hash160; pub mod hkdf; pub mod hmac; @@ -127,6 +128,7 @@ use core::{convert, fmt, hash}; #[rustfmt::skip] // Keep public re-exports separate. #[doc(inline)] pub use self::{ + error::FromSliceError, hkdf::Hkdf, hmac::{Hmac, HmacEngine}, }; @@ -317,30 +319,6 @@ fn incomplete_block_len(eng: &H) -> usize { (eng.n_bytes_hashed() % block_size) as usize } -/// Attempted to create a hash from an invalid length slice. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FromSliceError { - expected: usize, - 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) - } -} - -#[cfg(feature = "std")] -impl std::error::Error for FromSliceError {} - #[cfg(test)] mod tests { use super::*; From 7652d0ddfc620ab3b3b27be18df8bee33820fda7 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 5 Nov 2024 10:03:31 +1100 Subject: [PATCH 2/3] hashes: Hide innards of FromSliceError In preparation for releasing 1.0 we want to hide all error internals. Do so for the single error type in the `hashes` crate. --- hashes/src/error.rs | 24 ++++++++++++++---------- hashes/src/internal_macros.rs | 5 ++++- hashes/src/sha256t.rs | 4 +++- 3 files changed, 21 insertions(+), 12 deletions(-) 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); From fe8ca21ec282cfe50e3f39b7f86c619d30d7f542 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 5 Nov 2024 10:44:58 +1100 Subject: [PATCH 3/3] hashes: Duplicate impl_from_infallible We don't want a dependency on `internals` for `hashes` unless its really needed. Duplicated the `impl_from_infallible` macro into `hashes::error` and use it to implement from infallible for `FromSliceError`. --- hashes/src/error.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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;