Trait rustc_stable_hash::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.

Object Safety§

This trait is not object safe.

Implementors§