From 12f261c00958c918b1c991332ede64153ded0bb6 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 16 Oct 2024 12:13:27 +1100 Subject: [PATCH] hashes: Re-order from_byte_array The `hashes` crate has a bunch of similar types defined by a bunch of similar macros and impl blocks, all of which makes it difficult to tell exactly what is implemented where. In an effort to make the code easier to read order the `from_byte_array` constructor in the same place across the crate. Note also we typically put constructors up the top, also `from_byte_array` is the likely most used constructor so put it first. FWIW I discovered this while polishing the HTML docs. Internal change only. --- hashes/src/hmac.rs | 4 ++-- hashes/src/internal_macros.rs | 14 +++++++------- hashes/src/lib.rs | 6 +++--- hashes/src/sha256t.rs | 6 +++--- hashes/src/util.rs | 14 +++++++------- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/hashes/src/hmac.rs b/hashes/src/hmac.rs index ecd5bc673..2e30277f7 100644 --- a/hashes/src/hmac.rs +++ b/hashes/src/hmac.rs @@ -127,14 +127,14 @@ impl GeneralHash for Hmac { impl Hash for Hmac { type Bytes = T::Bytes; + fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) } + #[allow(deprecated_in_future)] fn from_slice(sl: &[u8]) -> Result, FromSliceError> { T::from_slice(sl).map(Hmac) } fn to_byte_array(self) -> Self::Bytes { self.0.to_byte_array() } fn as_byte_array(&self) -> &Self::Bytes { self.0.as_byte_array() } - - fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) } } #[cfg(feature = "serde")] diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index f8272cd09..d8a73ad18 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -97,6 +97,8 @@ macro_rules! hash_trait_impls { const DISPLAY_BACKWARD: bool = $reverse; + fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } + #[allow(deprecated_in_future)] fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result, $crate::FromSliceError> { Self::from_slice(sl) @@ -105,8 +107,6 @@ macro_rules! hash_trait_impls { fn to_byte_array(self) -> Self::Bytes { self.to_byte_array() } fn as_byte_array(&self) -> &Self::Bytes { self.as_byte_array() } - - fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } } } } @@ -169,6 +169,11 @@ macro_rules! hash_type_no_default { impl Hash { const fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) } + /// Constructs a hash from the underlying byte array. + pub const fn from_byte_array(bytes: [u8; $bits / 8]) -> Self { + Self::internal_new(bytes) + } + /// 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 { @@ -201,11 +206,6 @@ macro_rules! hash_type_no_default { /// Returns a reference to the underlying byte array. pub const fn as_byte_array(&self) -> &[u8; $bits / 8] { &self.0 } - - /// Constructs a hash from the underlying byte array. - pub const fn from_byte_array(bytes: [u8; $bits / 8]) -> Self { - Self::internal_new(bytes) - } } $crate::internal_macros::hash_trait_impls!($bits, $reverse); diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 8759693cd..255239fe9 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -270,6 +270,9 @@ pub trait Hash: /// For some reason Satoshi decided this should be true for `Sha256dHash`, so here we are. const DISPLAY_BACKWARD: bool = false; + /// Constructs a hash from the underlying byte array. + fn from_byte_array(bytes: Self::Bytes) -> Self; + /// Copies a byte slice into a hash object. #[deprecated(since = "TBD", note = "use `from_byte_array` instead")] fn from_slice(sl: &[u8]) -> Result; @@ -279,9 +282,6 @@ pub trait Hash: /// Returns a reference to the underlying byte array. fn as_byte_array(&self) -> &Self::Bytes; - - /// Constructs a hash from the underlying byte array. - fn from_byte_array(bytes: Self::Bytes) -> Self; } /// Ensures that a type is an array. diff --git a/hashes/src/sha256t.rs b/hashes/src/sha256t.rs index 4df7c4bd9..20faa9e06 100644 --- a/hashes/src/sha256t.rs +++ b/hashes/src/sha256t.rs @@ -25,6 +25,9 @@ where { const fn internal_new(arr: [u8; 32]) -> Self { Hash(arr, PhantomData) } + /// Constructs a hash from the underlying byte array. + pub const fn from_byte_array(bytes: [u8; 32]) -> Self { Self::internal_new(bytes) } + /// 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 { @@ -104,9 +107,6 @@ where /// Returns a reference to the underlying byte array. pub const fn as_byte_array(&self) -> &[u8; 32] { &self.0 } - - /// Constructs a hash from the underlying byte array. - pub const fn from_byte_array(bytes: [u8; 32]) -> Self { Self::internal_new(bytes) } } impl Copy for Hash {} diff --git a/hashes/src/util.rs b/hashes/src/util.rs index 80ca72af7..d0da798f3 100644 --- a/hashes/src/util.rs +++ b/hashes/src/util.rs @@ -195,6 +195,11 @@ macro_rules! hash_newtype { #[allow(unused)] // Private wrapper types may not need all functions. impl $newtype { + /// Constructs a hash from the underlying byte array. + pub const fn from_byte_array(bytes: <$hash as $crate::Hash>::Bytes) -> Self { + $newtype(<$hash>::from_byte_array(bytes)) + } + /// Copies a byte slice into a hash object. #[deprecated(since = "TBD", note = "use `from_byte_array` instead")] #[allow(deprecated_in_future)] @@ -211,11 +216,6 @@ macro_rules! hash_newtype { pub const fn as_byte_array(&self) -> &<$hash as $crate::Hash>::Bytes { self.0.as_byte_array() } - - /// Constructs a hash from the underlying byte array. - pub const fn from_byte_array(bytes: <$hash as $crate::Hash>::Bytes) -> Self { - $newtype(<$hash>::from_byte_array(bytes)) - } } impl $crate::_export::_core::convert::From<$hash> for $newtype { @@ -236,6 +236,8 @@ macro_rules! hash_newtype { const DISPLAY_BACKWARD: bool = $crate::hash_newtype_get_direction!($hash, $(#[$($type_attrs)*])*); + fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } + #[inline] #[allow(deprecated_in_future)] fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<$newtype, $crate::FromSliceError> { @@ -245,8 +247,6 @@ macro_rules! hash_newtype { fn to_byte_array(self) -> Self::Bytes { self.to_byte_array() } fn as_byte_array(&self) -> &Self::Bytes { self.as_byte_array() } - - fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } } impl $crate::_export::_core::str::FromStr for $newtype {