Skip to content

Commit

Permalink
Merge pull request #52 from DaniPopes/random-state-clone
Browse files Browse the repository at this point in the history
Derive Clone for FxRandomState and FxSeededState
  • Loading branch information
WaffleLapkin authored Oct 17, 2024
2 parents eb049a8 + 23fcdff commit 6745258
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/random_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub type FxHashSetRand<V> = HashSet<V, FxRandomState>;
/// A particular instance `FxRandomState` will create the same instances of
/// [`Hasher`], but the hashers created by two different `FxRandomState`
/// instances are unlikely to produce the same result for the same values.
#[derive(Clone)]
pub struct FxRandomState {
seed: usize,
}
Expand Down Expand Up @@ -62,6 +63,14 @@ mod tests {

use crate::FxHashMapRand;

#[test]
fn cloned_random_states_are_equal() {
let a = FxHashMapRand::<&str, u32>::default();
let b = a.clone();

assert_eq!(a.hasher().seed, b.hasher().seed);
}

#[test]
fn random_states_are_different() {
let a = FxHashMapRand::<&str, u32>::default();
Expand Down
13 changes: 13 additions & 0 deletions src/seeded_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub type FxHashSetSeed<V> = std::collections::HashSet<V, FxSeededState>;
/// map.insert(15, 610);
/// assert_eq!(map[&15], 610);
/// ```
#[derive(Clone)]
pub struct FxSeededState {
seed: usize,
}
Expand All @@ -43,6 +44,18 @@ mod tests {

use crate::FxSeededState;

#[test]
fn cloned_seeded_states_are_equal() {
let seed = 2;
let a = FxSeededState::with_seed(seed);
let b = a.clone();

assert_eq!(a.seed, b.seed);
assert_eq!(a.seed, seed);

assert_eq!(a.build_hasher().hash, b.build_hasher().hash);
}

#[test]
fn same_seed_produces_same_hasher() {
let seed = 1;
Expand Down

0 comments on commit 6745258

Please sign in to comment.