Crate hash32

source ·
Expand description

32-bit hashing machinery

§Why?

Because 32-bit architectures are a thing (e.g. ARM Cortex-M) and you don’t want your hashing function to pull in a bunch of slow 64-bit compiler intrinsics (software implementations of 64-bit operations).

§Relationship to core::hash

This crate exposes the same interfaces you’ll find in core::hash: Hash, Hasher, BuildHasher and BuildHasherDefault. The main difference is that hash32::Hasher::finish returns a u32 instead of u64, and the contract of hash32::Hasher forbids the implementer from performing 64-bit (or 128-bit) operations while computing the hash.

§#[derive(Hash32)]

The easiest way to implement hash32::Hash for a struct is to use the #[derive(Hash32)].

Note that you need to explicitly depend on both hash32 and hash32_derive; both crates must appear in your Cargo.toml.

use hash32_derive::Hash32;

#[derive(Hash32)]
struct Ipv4Addr([u8; 4]);

§Hashers

This crate provides implementations of the following 32-bit hashing algorithms:

§MSRV

This crate is guaranteed to compile on latest stable Rust. It might compile on older versions but that may change in any new patch release.

§Future

In the future we’d like to deprecate this crate in favor of making core::hash::Hasher generic over the size of the computed hash. Below is shown the planned change (but it doesn’t work due to limitations in the associated_type_defaults feature):

#![feature(associated_type_defaults)]

trait Hasher {
    type Hash = u64; // default type for backwards compatibility

    fn finish(&self) -> Self::Hash; // changed
    fn write(&mut self, bytes: &[u8]);
}

With this change a single #[derive(Hash)] would enough to make a type hashable with 32-bit and 64-bit hashers.

Structs§

Traits§