Do not use stable hashing with all-zero seed.

This commit is contained in:
Stephen Chung 2022-09-27 16:39:19 +08:00
parent 2ecf44a48e
commit b141e8d0e1
4 changed files with 7 additions and 4 deletions

View File

@ -9,7 +9,8 @@ New features
### Stable hashing
* It is now possible to specify a fixed _seed_ for use with the `ahash` hasher in order to force stable (i.e. deterministic) hashes for function signatures.
* It is now possible to specify a fixed _seed_ for use with the `ahash` hasher, via an environment variable, in order to force stable (i.e. deterministic) hashes for function signatures. This is necessary when using Rhai across shared-library boundaries.
* A build script is now used to extract the environment variable (`RHAI_AHASH_SEED`) and splice it into the source code before compilation.
Enhancements
------------

View File

@ -10,7 +10,6 @@ fn main() {
f.write_fmt(format_args!(
"//! Configuration settings for this Rhai build
#![allow(dead_code)]
"
))

View File

@ -1,4 +1,3 @@
//! Configuration settings for this Rhai build
#![allow(dead_code)]
pub const AHASH_SEED: Option<[u64; 4]> = None;

View File

@ -76,10 +76,14 @@ impl BuildHasher for StraightHasherBuilder {
#[must_use]
pub fn get_hasher() -> ahash::AHasher {
if let Some([seed1, seed2, seed3, seed4]) = config::AHASH_SEED {
if seed1 | seed2 | seed3 | seed4 != 0 {
ahash::RandomState::with_seeds(seed1, seed2, seed3, seed4).build_hasher()
} else {
ahash::AHasher::default()
}
} else {
ahash::AHasher::default()
}
}
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified variable name.