1use crate::FxHasher;
23/// Type alias for a hashmap using the `fx` hash algorithm with [`FxSeededState`].
4#[cfg(feature = "std")]
5pub type FxHashMapSeed<K, V> = std::collections::HashMap<K, V, FxSeededState>;
67/// Type alias for a hashmap using the `fx` hash algorithm with [`FxSeededState`].
8#[cfg(feature = "std")]
9pub type FxHashSetSeed<V> = std::collections::HashSet<V, FxSeededState>;
1011/// [`FxSeededState`] is an alternative state for `HashMap` types, allowing to use [`FxHasher`] with a set seed.
12///
13/// ```
14/// # use std::collections::HashMap;
15/// use rustc_hash::FxSeededState;
16///
17/// let mut map = HashMap::with_hasher(FxSeededState::with_seed(12));
18/// map.insert(15, 610);
19/// assert_eq!(map[&15], 610);
20/// ```
21#[derive(Clone)]
22pub struct FxSeededState {
23 seed: usize,
24}
2526impl FxSeededState {
27/// Constructs a new `FxSeededState` that is initialized with a `seed`.
28pub const fn with_seed(seed: usize) -> FxSeededState {
29Self { seed }
30 }
31}
3233impl core::hash::BuildHasher for FxSeededState {
34type Hasher = FxHasher;
3536fn build_hasher(&self) -> Self::Hasher {
37 FxHasher::with_seed(self.seed)
38 }
39}
4041#[cfg(test)]
42mod tests {
43use core::hash::BuildHasher;
4445use crate::FxSeededState;
4647#[test]
48fn cloned_seeded_states_are_equal() {
49let seed = 2;
50let a = FxSeededState::with_seed(seed);
51let b = a.clone();
5253assert_eq!(a.seed, b.seed);
54assert_eq!(a.seed, seed);
5556assert_eq!(a.build_hasher().hash, b.build_hasher().hash);
57 }
5859#[test]
60fn same_seed_produces_same_hasher() {
61let seed = 1;
62let a = FxSeededState::with_seed(seed);
63let b = FxSeededState::with_seed(seed);
6465// The hashers should be the same, as they have the same seed.
66assert_eq!(a.build_hasher().hash, b.build_hasher().hash);
67 }
6869#[test]
70fn different_states_are_different() {
71let a = FxSeededState::with_seed(1);
72let b = FxSeededState::with_seed(2);
7374assert_ne!(a.build_hasher().hash, b.build_hasher().hash);
75 }
76}