Expand description
Rust implementation of the wyhash algorithm by Wang Yi.
The hashing algorithm passes SMHasher and the random number generator passes BigCrush and practrand. As of now it is the fastest algorithm in the SMHasher benchmark (faster than t1ha and XXH3). See here.
Furthermore, this algorithm is solid, simple, portable (does not need
hardware support, can be used in no_std
environments) and has
no dependencies.
The generated hashes are equal (see tests) as of the version stated here although the speed varies (PRs are welcome).
§Crate features
By default this crate uses 128-bit integer multiplications.
To restrict that to 64 bits you can enable the feature mum32bit
. This offers better
performance on 32-bit architectures.
Beware that this feature produces different the results.
§Usage (see also examples folder)
For the hashing function you can use either the free function or the
Hasher
trait.
§wyhash
function usage
use wyhash::wyhash;
let data = [0, 1, 2];
let seed = 3;
let hash = wyhash(&data, seed);
println!("{:x}", hash); // prints b0f941520b1ad95d
§Hasher
trait usage
You can also use std::hash::Hasher
, it is the same.
use core::hash::Hasher;
use wyhash::WyHash;
let mut hasher = WyHash::with_seed(3);
hasher.write(&[0, 1, 2]);
println!("{:x}", hasher.finish()); // prints b0f941520b1ad95d
§wyrng
function usage
Note that the seed parameter is updated so that it is possible to generate a sequence of random numbers.
use wyhash::wyrng;
let mut seed = 3;
let random_number = wyrng(&mut seed);
println!("{:x}", random_number); // prints 3e99a772750dcbe
println!("{:x}", seed); //prints a0761d6478bd6432
§RngCore
trait usage
You can also use rand::Rng
, it is the same.
use rand_core::RngCore;
use wyhash::WyRng;
let mut rng = WyRng::default();
println!("{:x}", rng.next_u64()); // prints 111cb3a78f59a58e
§SeedableRng
trait usage
You can also use rand::SeedableRng
, it is the same.
use rand_core::{RngCore, SeedableRng};
use wyhash::WyRng;
// Seeds are 8-byte long.
let seed = [0, 1, 2, 3, 4, 5, 6, 7];
let mut rng1 = WyRng::from_seed(seed);
println!("{:x}", rng1.next_u64()); // prints d730135774c6ae31
// Alternatively you can also use this convenience method:
let mut rng2 = WyRng::seed_from_u64(3);
println!("{:x}", rng2.next_u64()); // prints 3e99a772750dcbe
Re-exports§
pub use crate::v1::wyhash;
pub use crate::v1::wyrng;
pub use crate::v1::WyHash;
pub use crate::v1::WyHasherBuilder;
pub use crate::v1::WyRng;