From aed61bd2d42aee5de1909daebcd5eed105628a51 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 21 Aug 2024 15:58:29 +1000 Subject: [PATCH] Implement FromStr and serde impls for siphash The `serde` impls and `FromStr` are missing from `siphash`, add them. --- hashes/src/siphash24.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hashes/src/siphash24.rs b/hashes/src/siphash24.rs index d0258a06c..6aaed346a 100644 --- a/hashes/src/siphash24.rs +++ b/hashes/src/siphash24.rs @@ -4,8 +4,11 @@ use core::ops::Index; use core::slice::SliceIndex; +use core::str::FromStr; use core::{cmp, mem, ptr}; +use hex::FromHex; + use crate::internal_macros::arr_newtype_fmt_impl; use crate::HashEngine as _; @@ -264,7 +267,16 @@ impl> Index for Hash { fn index(&self, index: I) -> &Self::Output { &self.0[index] } } +impl FromStr for Hash { + type Err = hex::HexToArrayError; + fn from_str(s: &str) -> Result { + let bytes = <[u8; 8]>::from_hex(s)?; + Ok(Self::from_byte_array(bytes)) + } +} + arr_newtype_fmt_impl!(Hash, 8); +serde_impl!(Hash, 8); borrow_slice_impl!(Hash); /// Load an u64 using up to 7 bytes of a byte slice.