Trait solana_nohash_hasher::IsEnabled
source · pub trait IsEnabled { }
Expand description
Types which are safe to use with NoHashHasher
.
This marker trait is an option for types to enable themselves for use
with NoHashHasher
. In order to be safe, the Hash
impl needs to
satisfy the following constraint:
One of the
Hasher::write_{u8,u16,u32,u64,usize,i8,i16,i32,i64,isize}
methods is invoked exactly once.
The best way to ensure this is to write a custom Hash
impl even when
deriving Hash
for a simple newtype of a single type which itself
implements IsEnabled
may work as well.
Example
#[derive(PartialEq, Eq)]
struct SomeType(u32);
impl std::hash::Hash for SomeType {
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
hasher.write_u32(self.0)
}
}
impl solana_nohash_hasher::IsEnabled for SomeType {}
let mut m = solana_nohash_hasher::IntMap::default();
m.insert(SomeType(1), 't');
m.insert(SomeType(0), 'f');
assert_eq!(Some(&'t'), m.get(&SomeType(1)));
assert_eq!(Some(&'f'), m.get(&SomeType(0)));