From 20d4b715912a8e73c82c1b0bcbb5ecf09ff47016 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 26 Sep 2022 23:45:50 +0800 Subject: [PATCH] Add stable_hash. --- CHANGELOG.md | 5 +++++ Cargo.toml | 1 + src/func/hashing.rs | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d95bc3d0..9340630c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Rhai Release Notes Version 1.11.0 ============== +New features +------------ + +* A new feature flag, `stable_hash`, is added that forces hashing to be consistent using a fixed seed. + Enhancements ------------ diff --git a/Cargo.toml b/Cargo.toml index 1034f923..6451af51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,6 +62,7 @@ metadata = ["serde", "serde_json", "rhai_codegen/metadata", "smartstring/serde"] internals = [] # expose internal data structures debugging = ["internals"] # enable debugging serde = ["dep:serde", "smartstring/serde", "smallvec/serde"] # implement serde for rhai types +stable_hash = [] # perform all hashing with fixed seed value # compiling for no-std no_std = ["no-std-compat", "num-traits/libm", "core-error", "libm", "ahash/compile-time-rng", "hashbrown/ahash-compile-time-rng"] diff --git a/src/func/hashing.rs b/src/func/hashing.rs index bc29f67b..ae0a57bc 100644 --- a/src/func/hashing.rs +++ b/src/func/hashing.rs @@ -74,7 +74,11 @@ impl BuildHasher for StraightHasherBuilder { #[inline(always)] #[must_use] pub fn get_hasher() -> ahash::AHasher { - ahash::AHasher::default() + if cfg!(feature = "stable_hash") { + ahash::RandomState::with_seeds(42, 999, 123, 0).build_hasher() + } else { + ahash::AHasher::default() + } } /// Calculate a non-zero [`u64`] hash key from a namespace-qualified variable name.