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:
parent
c11587d60d
commit
12f261c009
|
@ -127,14 +127,14 @@ impl<T: GeneralHash> GeneralHash for Hmac<T> {
|
||||||
impl<T: GeneralHash> Hash for Hmac<T> {
|
impl<T: GeneralHash> Hash for Hmac<T> {
|
||||||
type Bytes = T::Bytes;
|
type Bytes = T::Bytes;
|
||||||
|
|
||||||
|
fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) }
|
||||||
|
|
||||||
#[allow(deprecated_in_future)]
|
#[allow(deprecated_in_future)]
|
||||||
fn from_slice(sl: &[u8]) -> Result<Hmac<T>, FromSliceError> { T::from_slice(sl).map(Hmac) }
|
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 to_byte_array(self) -> Self::Bytes { self.0.to_byte_array() }
|
||||||
|
|
||||||
fn as_byte_array(&self) -> &Self::Bytes { self.0.as_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")]
|
#[cfg(feature = "serde")]
|
||||||
|
|
|
@ -97,6 +97,8 @@ macro_rules! hash_trait_impls {
|
||||||
|
|
||||||
const DISPLAY_BACKWARD: bool = $reverse;
|
const DISPLAY_BACKWARD: bool = $reverse;
|
||||||
|
|
||||||
|
fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) }
|
||||||
|
|
||||||
#[allow(deprecated_in_future)]
|
#[allow(deprecated_in_future)]
|
||||||
fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<Hash<$($gen),*>, $crate::FromSliceError> {
|
fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<Hash<$($gen),*>, $crate::FromSliceError> {
|
||||||
Self::from_slice(sl)
|
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 to_byte_array(self) -> Self::Bytes { self.to_byte_array() }
|
||||||
|
|
||||||
fn as_byte_array(&self) -> &Self::Bytes { self.as_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 {
|
impl Hash {
|
||||||
const fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) }
|
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
|
/// Zero cost conversion between a fixed length byte array shared reference and
|
||||||
/// a shared reference to this Hash type.
|
/// a shared reference to this Hash type.
|
||||||
pub fn from_bytes_ref(bytes: &[u8; $bits / 8]) -> &Self {
|
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.
|
/// Returns a reference to the underlying byte array.
|
||||||
pub const fn as_byte_array(&self) -> &[u8; $bits / 8] { &self.0 }
|
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);
|
$crate::internal_macros::hash_trait_impls!($bits, $reverse);
|
||||||
|
|
|
@ -270,6 +270,9 @@ pub trait Hash:
|
||||||
/// For some reason Satoshi decided this should be true for `Sha256dHash`, so here we are.
|
/// For some reason Satoshi decided this should be true for `Sha256dHash`, so here we are.
|
||||||
const DISPLAY_BACKWARD: bool = false;
|
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.
|
/// Copies a byte slice into a hash object.
|
||||||
#[deprecated(since = "TBD", note = "use `from_byte_array` instead")]
|
#[deprecated(since = "TBD", note = "use `from_byte_array` instead")]
|
||||||
fn from_slice(sl: &[u8]) -> Result<Self, FromSliceError>;
|
fn from_slice(sl: &[u8]) -> Result<Self, FromSliceError>;
|
||||||
|
@ -279,9 +282,6 @@ pub trait Hash:
|
||||||
|
|
||||||
/// Returns a reference to the underlying byte array.
|
/// Returns a reference to the underlying byte array.
|
||||||
fn as_byte_array(&self) -> &Self::Bytes;
|
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.
|
/// Ensures that a type is an array.
|
||||||
|
|
|
@ -25,6 +25,9 @@ where
|
||||||
{
|
{
|
||||||
const fn internal_new(arr: [u8; 32]) -> Self { Hash(arr, PhantomData) }
|
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
|
/// Zero cost conversion between a fixed length byte array shared reference and
|
||||||
/// a shared reference to this Hash type.
|
/// a shared reference to this Hash type.
|
||||||
pub fn from_bytes_ref(bytes: &[u8; 32]) -> &Self {
|
pub fn from_bytes_ref(bytes: &[u8; 32]) -> &Self {
|
||||||
|
@ -104,9 +107,6 @@ where
|
||||||
|
|
||||||
/// Returns a reference to the underlying byte array.
|
/// Returns a reference to the underlying byte array.
|
||||||
pub const fn as_byte_array(&self) -> &[u8; 32] { &self.0 }
|
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> {}
|
impl<T: Tag> Copy for Hash<T> {}
|
||||||
|
|
|
@ -195,6 +195,11 @@ macro_rules! hash_newtype {
|
||||||
|
|
||||||
#[allow(unused)] // Private wrapper types may not need all functions.
|
#[allow(unused)] // Private wrapper types may not need all functions.
|
||||||
impl $newtype {
|
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.
|
/// Copies a byte slice into a hash object.
|
||||||
#[deprecated(since = "TBD", note = "use `from_byte_array` instead")]
|
#[deprecated(since = "TBD", note = "use `from_byte_array` instead")]
|
||||||
#[allow(deprecated_in_future)]
|
#[allow(deprecated_in_future)]
|
||||||
|
@ -211,11 +216,6 @@ macro_rules! hash_newtype {
|
||||||
pub const fn as_byte_array(&self) -> &<$hash as $crate::Hash>::Bytes {
|
pub const fn as_byte_array(&self) -> &<$hash as $crate::Hash>::Bytes {
|
||||||
self.0.as_byte_array()
|
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 {
|
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)*])*);
|
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]
|
#[inline]
|
||||||
#[allow(deprecated_in_future)]
|
#[allow(deprecated_in_future)]
|
||||||
fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<$newtype, $crate::FromSliceError> {
|
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 to_byte_array(self) -> Self::Bytes { self.to_byte_array() }
|
||||||
|
|
||||||
fn as_byte_array(&self) -> &Self::Bytes { self.as_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 {
|
impl $crate::_export::_core::str::FromStr for $newtype {
|
||||||
|
|
Loading…
Reference in New Issue