1use core::hash::{Hash, Hasher};
9
10pub trait TryHash {
12 type Error;
14
15 fn try_hash<H: Hasher>(&self, state: &mut H) -> Result<(), Self::Error>;
17
18 fn try_hash_slice<H: Hasher>(data: &[Self], state: &mut H) -> Result<(), Self::Error>
20 where
21 Self: Sized
22 {
23 for piece in data {
24 piece.try_hash(state)?;
25 }
26
27 Ok(())
28 }
29}
30
31impl<T: Hash> TryHash for T {
32 type Error = crate::Infallible;
33
34 #[inline]
35 fn try_hash<H: Hasher>(&self, state: &mut H) -> Result<(), Self::Error> {
36 Ok(self.hash(state))
37 }
38
39 #[inline]
40 fn try_hash_slice<H: Hasher>(data: &[Self], state: &mut H) -> Result<(), Self::Error> {
41 Ok(Self::hash_slice(data, state))
42 }
43}