kast_try_hash/
lib.rs

1pub use try_hash_derive::TryHash;
2
3pub trait TryHash {
4    type Error;
5    fn try_hash(&self, hasher: &mut impl std::hash::Hasher) -> Result<(), Self::Error>;
6}
7
8impl<T: ?Sized + TryHash> TryHash for Box<T> {
9    type Error = T::Error;
10    fn try_hash(&self, hasher: &mut impl std::hash::Hasher) -> Result<(), Self::Error> {
11        T::try_hash(self, hasher)
12    }
13}
14
15impl<T: TryHash> TryHash for Option<T> {
16    type Error = T::Error;
17    fn try_hash(&self, hasher: &mut impl std::hash::Hasher) -> Result<(), Self::Error> {
18        std::hash::Hash::hash(&std::mem::discriminant(self), hasher);
19        if let Some(value) = self {
20            value.try_hash(hasher)?;
21        }
22        Ok(())
23    }
24}
25
26impl<T: TryHash> TryHash for Vec<T> {
27    type Error = T::Error;
28    fn try_hash(&self, hasher: &mut impl std::hash::Hasher) -> Result<(), Self::Error> {
29        for elem in self {
30            elem.try_hash(hasher)?;
31        }
32        Ok(())
33    }
34}
35
36impl<T: TryHash> TryHash for std::sync::Arc<T> {
37    type Error = T::Error;
38    fn try_hash(&self, hasher: &mut impl std::hash::Hasher) -> Result<(), Self::Error> {
39        T::try_hash(self, hasher)
40    }
41}