From f8846101ae285ea975a647a815ab5172865a20c2 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 9 Aug 2024 11:47:51 +1000 Subject: [PATCH] siphash: Make functions inherent In recent work making functions on hash types inherent it seems we missed `siphash`. Add inherent getters/setters to the `siphash::Hash` type and call through to them from the `Hash` trait impl. --- hashes/src/siphash24.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/hashes/src/siphash24.rs b/hashes/src/siphash24.rs index 9895ee0f5..d0258a06c 100644 --- a/hashes/src/siphash24.rs +++ b/hashes/src/siphash24.rs @@ -226,6 +226,15 @@ impl Hash { /// Creates a hash from its (little endian) 64-bit integer representation. pub fn from_u64(hash: u64) -> Hash { Hash(hash.to_le_bytes()) } + /// Returns the underlying byte array. + pub const fn to_byte_array(self) -> [u8; 8] { self.0 } + + /// Returns a reference to the underlying byte array. + pub const fn as_byte_array(&self) -> &[u8; 8] { &self.0 } + + /// Constructs a hash from the underlying byte array. + pub const fn from_byte_array(bytes: [u8; 8]) -> Self { Self(bytes) } + fn from_slice(sl: &[u8]) -> Result { let mut ret = [0; 8]; ret.copy_from_slice(sl); @@ -241,11 +250,11 @@ impl crate::Hash for Hash { fn from_slice(sl: &[u8]) -> Result { Self::from_slice(sl) } - fn to_byte_array(self) -> Self::Bytes { self.0 } + fn to_byte_array(self) -> Self::Bytes { self.to_byte_array() } - fn as_byte_array(&self) -> &Self::Bytes { &self.0 } + fn as_byte_array(&self) -> &Self::Bytes { self.as_byte_array() } - fn from_byte_array(bytes: Self::Bytes) -> Self { Hash(bytes) } + fn from_byte_array(bytes: Self::Bytes) -> Self { Self::from_byte_array(bytes) } } impl> Index for Hash {