hashes: constify a bunch of constructors
This commit is contained in:
parent
c155cbf8b2
commit
154e91af8c
|
@ -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.
|
||||||
///
|
///
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue