Add a deterministic constructor for RandomState
by bkragl · Pull Request #135578 · rust-lang/rust (original) (raw)
RandomState
is the default hasher for std::collections::{HashMap, HashSet}
. Today, the only way to construct a RandomState
is via RandomState:new()
, which generates fresh random keys on every call. That's a good default for security, but it makes testing challenging, because test failures might not be reproducible due to nondeterministic behavior.
Although in principle it is possible to use other deterministic hashers, there are libraries which have hash collections with RandomState
hardcoded in their public interface. For example, see aws_sdk_dynamodb (and this discussion).
This change adds a new constructor for RandomState
that returns a fixed value, which makes it possible for programs to behave deterministically even if they are locked in to RandomState
for hashing.