Skip to main content

ExtendedHasher

Trait ExtendedHasher 

Source
pub trait ExtendedHasher: Hasher {
    type Hash;

    // Required method
    fn finish(self) -> Self::Hash;

    // Provided method
    fn short_write<const LEN: usize>(&mut self, bytes: [u8; LEN]) { ... }
}
Expand description

Extended Hasher trait for use with StableHasher.

It permits returning an arbitrary type as the Self::Hash type contrary to the Hasher trait which can only return u64. This is useful when the hasher uses a different representation.

§Example

use std::hash::Hasher;
use rustc_stable_hash::ExtendedHasher;

struct BogusHasher(u128);

impl Hasher for BogusHasher {
    fn write(&mut self, a: &[u8]) {
        // ...
    }

    fn finish(&self) -> u64 {
        self.0 as u64 // really bogus
    }
}

impl ExtendedHasher for BogusHasher {
    type Hash = u128;

    fn short_write<const LEN: usize>(&mut self, bytes: [u8; LEN]) {
        self.write(&bytes)
    }

    fn finish(self) -> Self::Hash {
        self.0
    }
}

Required Associated Types§

Source

type Hash

Type returned by the hasher.

Required Methods§

Source

fn finish(self) -> Self::Hash

Finalization method of the hasher to return the Hash.

Provided Methods§

Source

fn short_write<const LEN: usize>(&mut self, bytes: [u8; LEN])

Optimized version of Hasher::write but for small write.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§