From ee7f783fc3f540cdce59ab10d9e4972a96e9e22e Mon Sep 17 00:00:00 2001 From: Bernhard Kragl <bernhard.kragl@gmail.com> Date: Thu, 16 Jan 2025 11:17:27 +0000 Subject: [PATCH] Add a deterministic constructor for `RandomState` --- library/std/src/hash/random.rs | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/library/std/src/hash/random.rs b/library/std/src/hash/random.rs index 236803b24a2ec..04382bd93cca8 100644 --- a/library/std/src/hash/random.rs +++ b/library/std/src/hash/random.rs @@ -75,6 +75,44 @@ impl RandomState { RandomState { k0, k1 } }) } + + /// Constructs a new `RandomState` that is initialized with fixed keys. + /// + /// There are no guarantees about the returned value other than it being + /// deterministic. The returned value may vary across releases. + /// + /// This constructor is meant for testing purposes, where it is important + /// that a program behaves deterministically in order to reproduce test + /// failures. Although it is possible to use other deterministic hashers, + /// there are libraries which have hash collections with `RandomState` + /// hardcoded in their public interface. + /// + /// # Examples + /// + /// Rerunning the following program always produces the same output, which + /// isn't the case if `s` is created with `RandomState::new`. + /// + /// ``` + /// #![feature(deterministic_random_state)] + /// use std::collections::HashSet; + /// use std::hash::RandomState; + /// + /// let s = RandomState::deterministic(); + /// let mut set = HashSet::with_hasher(s); + /// set.insert(0); + /// set.insert(1); + /// for v in set { + /// println!("{v}"); + /// } + /// ``` + #[inline] + #[allow(deprecated)] + // rand + #[must_use] + #[unstable(feature = "deterministic_random_state", issue = "none")] + pub fn deterministic() -> RandomState { + RandomState { k0: 0, k1: 0 } + } } #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]