From ce1db3ea267c257bd56f54e74b3081db6072713d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 11 Jun 2024 12:19:57 +1000 Subject: [PATCH] hashes: Move non-trait functions The `hash_trait_impls` macro currently adds an impl block for `Hash` - this is not what the docs say since and `impl Hash` block is nothing to do with traits. Move the impl block and add a duplicate of the functions to the `sha256t::Hash` type. This is a refactor, no API or logic changes. Note that wrapper types currently do net get these functions - that will be discussed/implemented separately. --- hashes/src/internal_macros.rs | 30 ++++++++++++++---------------- hashes/src/sha256t.rs | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index 5635020ef..bbe40c0a4 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -70,22 +70,6 @@ pub(crate) use arr_newtype_fmt_impl; /// `internal_engine` is required to initialize the engine for given hash type. macro_rules! hash_trait_impls { ($bits:expr, $reverse:expr $(, $gen:ident: $gent:ident)*) => { - impl<$($gen: $gent),*> Hash<$($gen),*> { - /// Zero cost conversion between a fixed length byte array shared reference and - /// a shared reference to this Hash type. - pub fn from_bytes_ref(bytes: &[u8; $bits / 8]) -> &Self { - // Safety: Sound because Self is #[repr(transparent)] containing [u8; $bits / 8] - unsafe { &*(bytes as *const _ as *const Self) } - } - - /// Zero cost conversion between a fixed length byte array exclusive reference and - /// an exclusive reference to this Hash type. - pub fn from_bytes_mut(bytes: &mut [u8; $bits / 8]) -> &mut Self { - // Safety: Sound because Self is #[repr(transparent)] containing [u8; $bits / 8] - unsafe { &mut *(bytes as *mut _ as *mut Self) } - } - } - impl<$($gen: $gent),*> $crate::_export::_core::str::FromStr for Hash<$($gen),*> { type Err = $crate::hex::HexToArrayError; fn from_str(s: &str) -> $crate::_export::_core::result::Result { @@ -188,6 +172,20 @@ macro_rules! hash_type { fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) } fn internal_engine() -> HashEngine { Default::default() } + + /// Zero cost conversion between a fixed length byte array shared reference and + /// a shared reference to this Hash type. + pub fn from_bytes_ref(bytes: &[u8; $bits / 8]) -> &Self { + // Safety: Sound because Self is #[repr(transparent)] containing [u8; $bits / 8] + unsafe { &*(bytes as *const _ as *const Self) } + } + + /// Zero cost conversion between a fixed length byte array exclusive reference and + /// an exclusive reference to this Hash type. + pub fn from_bytes_mut(bytes: &mut [u8; $bits / 8]) -> &mut Self { + // Safety: Sound because Self is #[repr(transparent)] containing [u8; $bits / 8] + unsafe { &mut *(bytes as *mut _ as *mut Self) } + } } #[cfg(feature = "schemars")] diff --git a/hashes/src/sha256t.rs b/hashes/src/sha256t.rs index 0560bd3db..92f9ab3a5 100644 --- a/hashes/src/sha256t.rs +++ b/hashes/src/sha256t.rs @@ -40,6 +40,20 @@ impl Hash { fn internal_new(arr: [u8; 32]) -> Self { Hash(arr, Default::default()) } fn internal_engine() -> HashEngine { T::engine() } + + /// Zero cost conversion between a fixed length byte array shared reference and + /// a shared reference to this Hash type. + pub fn from_bytes_ref(bytes: &[u8; 32]) -> &Self { + // Safety: Sound because Self is #[repr(transparent)] containing [u8; 32] + unsafe { &*(bytes as *const _ as *const Self) } + } + + /// Zero cost conversion between a fixed length byte array exclusive reference and + /// an exclusive reference to this Hash type. + pub fn from_bytes_mut(bytes: &mut [u8; 32]) -> &mut Self { + // Safety: Sound because Self is #[repr(transparent)] containing [u8; 32] + unsafe { &mut *(bytes as *mut _ as *mut Self) } + } } impl Copy for Hash {}