hashes: constify a bunch of constructors

This commit is contained in:
Andrew Poelstra 2024-06-19 16:21:32 +00:00
parent c155cbf8b2
commit 154e91af8c
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 11 additions and 11 deletions

View File

@ -53,7 +53,7 @@ impl<T> Hash<T>
where where
(T,): Tag, (T,): Tag,
{ {
fn internal_new(arr: [u8; 32]) -> Self { Hash(arr, PhantomData) } const fn internal_new(arr: [u8; 32]) -> Self { Hash(arr, PhantomData) }
/// 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.
@ -110,13 +110,13 @@ where
} }
/// Returns the underlying byte array. /// Returns the underlying byte array.
pub fn to_byte_array(self) -> [u8; 32] { self.0 } pub const fn to_byte_array(self) -> [u8; 32] { self.0 }
/// Returns a reference to the underlying byte array. /// Returns a reference to the underlying byte array.
pub 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. /// Constructs a hash from the underlying byte array.
pub fn from_byte_array(bytes: [u8; 32]) -> Self { Self::internal_new(bytes) } pub const fn from_byte_array(bytes: [u8; 32]) -> Self { Self::internal_new(bytes) }
/// Returns an all zero hash. /// Returns an all zero hash.
/// ///

View File

@ -195,17 +195,17 @@ 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 {
/// Creates this wrapper type from the inner hash type. /// Creates this wrapper type from the inner hash type.
pub fn from_raw_hash(inner: $hash) -> $newtype { pub const fn from_raw_hash(inner: $hash) -> $newtype {
$newtype(inner) $newtype(inner)
} }
/// Returns the inner hash (sha256, sh256d etc.). /// Returns the inner hash (sha256, sh256d etc.).
pub fn to_raw_hash(self) -> $hash { pub const fn to_raw_hash(self) -> $hash {
self.0 self.0
} }
/// Returns a reference to the inner hash (sha256, sh256d etc.). /// Returns a reference to the inner hash (sha256, sh256d etc.).
pub fn as_raw_hash(&self) -> &$hash { pub const fn as_raw_hash(&self) -> &$hash {
&self.0 &self.0
} }
@ -250,18 +250,18 @@ macro_rules! hash_newtype {
} }
/// Returns the underlying byte array. /// Returns the underlying byte array.
pub fn to_byte_array(self) -> <$hash as $crate::Hash>::Bytes { pub const fn to_byte_array(self) -> <$hash as $crate::Hash>::Bytes {
self.0.to_byte_array() self.0.to_byte_array()
} }
/// Returns a reference to the underlying byte array. /// Returns a reference to the underlying byte array.
pub 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. /// Constructs a hash from the underlying byte array.
pub fn from_byte_array(bytes: <$hash as $crate::Hash>::Bytes) -> Self { pub const fn from_byte_array(bytes: <$hash as $crate::Hash>::Bytes) -> Self {
$newtype(<$hash as $crate::Hash>::from_byte_array(bytes)) $newtype(<$hash>::from_byte_array(bytes))
} }
/// Returns an all zero hash. /// Returns an all zero hash.