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.
This commit is contained in:
Tobin C. Harding 2024-10-16 12:13:27 +11:00
parent c11587d60d
commit 12f261c009
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
5 changed files with 22 additions and 22 deletions

View File

@ -127,14 +127,14 @@ impl<T: GeneralHash> GeneralHash for Hmac<T> {
impl<T: GeneralHash> Hash for Hmac<T> {
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<Hmac<T>, 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")]

View File

@ -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<Hash<$($gen),*>, $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);

View File

@ -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<Self, FromSliceError>;
@ -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.

View File

@ -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<T: Tag> Copy for Hash<T> {}

View File

@ -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 {