pub trait IdentityHashable { }
Expand description

Types which are safe to use with IdentityHasher.

This marker trait is an option for types to enable themselves for use with IdentityHasher. 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 new type 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 identity_hash::IdentityHashable for SomeType {}

let mut m = identity_hash::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)));

Implementations on Foreign Types§

source§

impl IdentityHashable for u16

source§

impl IdentityHashable for u8

source§

impl IdentityHashable for u32

source§

impl IdentityHashable for u64

source§

impl IdentityHashable for isize

source§

impl IdentityHashable for i16

source§

impl IdentityHashable for i32

source§

impl IdentityHashable for usize

source§

impl IdentityHashable for i8

source§

impl IdentityHashable for i64

Implementors§