Create a noop Hasher impl?

Is there any way to get Hasher::write to just pass my u64 through.

pub struct Trie<T> {
    // in order to avoid lifetime headaches I pre-hash a tuple of 
    // (preceding_seq: &[T}, current_value: &T) so I don't have to 
    // store that same tuple
    children: HashMap<u64, Node<T>>
}

this is the original definition for FnvHasher

impl Hasher for NoopHasher {
    #[inline]
    fn finish(&self) -> u64 {
        self.0
    }

    #[inline]
    fn write(&mut self, bytes: &[u8]) {
        let NoopHasher(mut hash) = *self;

        for byte in bytes.iter() {
            hash = hash ^ (*byte as u64);
            hash = hash.wrapping_mul(0x100000001b3);
        }

        *self = NoopHasher(hash);
    }
}

any ideas appreciated
thanks

Hasher also has optional methods you can implement in addition to the required write -- in this case I guess you'd want to handle write_u64 in particular. By default all those integer methods just forward to self.write(&i.to_ne_bytes()).

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.