Implement FromStr and serde impls for siphash

The `serde` impls and `FromStr` are missing from `siphash`, add them.
This commit is contained in:
Tobin C. Harding 2024-08-21 15:58:29 +10:00
parent f8846101ae
commit aed61bd2d4
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 12 additions and 0 deletions

View File

@ -4,8 +4,11 @@
use core::ops::Index; use core::ops::Index;
use core::slice::SliceIndex; use core::slice::SliceIndex;
use core::str::FromStr;
use core::{cmp, mem, ptr}; use core::{cmp, mem, ptr};
use hex::FromHex;
use crate::internal_macros::arr_newtype_fmt_impl; use crate::internal_macros::arr_newtype_fmt_impl;
use crate::HashEngine as _; use crate::HashEngine as _;
@ -264,7 +267,16 @@ impl<I: SliceIndex<[u8]>> Index<I> for Hash {
fn index(&self, index: I) -> &Self::Output { &self.0[index] } fn index(&self, index: I) -> &Self::Output { &self.0[index] }
} }
impl FromStr for Hash {
type Err = hex::HexToArrayError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = <[u8; 8]>::from_hex(s)?;
Ok(Self::from_byte_array(bytes))
}
}
arr_newtype_fmt_impl!(Hash, 8); arr_newtype_fmt_impl!(Hash, 8);
serde_impl!(Hash, 8);
borrow_slice_impl!(Hash); borrow_slice_impl!(Hash);
/// Load an u64 using up to 7 bytes of a byte slice. /// Load an u64 using up to 7 bytes of a byte slice.