//!todo: move to core/src/hash.rs use std::{ hash::{Hash, Hasher}, ops::Deref, }; use crate::hash::item_hash128; pub trait StaticHash128 { fn get_hash(&self) -> u128; } impl Hash for dyn StaticHash128 { #[inline] fn hash(&self, state: &mut H) { state.write_u128(self.get_hash()); } } pub struct HashedTrait { hash: u128, t: Box, } impl HashedTrait { pub fn new(hash: u128, t: Box) -> Self { Self { hash, t } } } impl Deref for HashedTrait { type Target = T; fn deref(&self) -> &Self::Target { &self.t } } impl Hash for HashedTrait { #[inline] fn hash(&self, state: &mut H) { state.write_u128(self.hash); } } impl Default for HashedTrait { fn default() -> Self { let t = T::default(); Self { hash: item_hash128(&t), t: Box::new(t), } } } impl StaticHash128 for HashedTrait { fn get_hash(&self) -> u128 { self.hash } }