Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/primitives/src/map/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ macro_rules! assert_eq_unchecked {
/// Works best with `fxhash`, enabled by default with the "map-fxhash" feature.
///
/// **NOTE:** this hasher accepts only `N`-length byte arrays! It is invalid to hash anything else.
#[derive(Default)]
#[derive(Clone, Default)]
pub struct FbBuildHasher<const N: usize> {
inner: DefaultHashBuilder,
_marker: core::marker::PhantomData<[(); N]>,
Expand All @@ -98,7 +98,7 @@ impl<const N: usize> BuildHasher for FbBuildHasher<N> {
/// Works best with `fxhash`, enabled by default with the "map-fxhash" feature.
///
/// **NOTE:** this hasher accepts only `N`-length byte arrays! It is invalid to hash anything else.
#[derive(Default)]
#[derive(Clone, Default)]
pub struct FbHasher<const N: usize> {
inner: DefaultHasher,
_marker: core::marker::PhantomData<[(); N]>,
Expand Down Expand Up @@ -201,5 +201,11 @@ mod tests {
map.insert(Address::ZERO, true);
assert_eq!(map.get(&Address::ZERO), Some(&true));
assert_eq!(map.get(&Address::with_last_byte(1)), None);

let map2 = map.clone();
assert_eq!(map.len(), map2.len());
assert_eq!(map.len(), 1);
assert_eq!(map2.get(&Address::ZERO), Some(&true));
assert_eq!(map2.get(&Address::with_last_byte(1)), None);
}
}
51 changes: 50 additions & 1 deletion crates/primitives/src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,40 @@ cfg_if! {

cfg_if! {
if #[cfg(all(feature = "std", feature = "rand"))] {
use rustc_hash::FxRandomState as FxBuildHasherInner;
// use rustc_hash::FxRandomState as FxBuildHasherInner;

// TODO: Polyfill for https://github.com/rust-lang/rustc-hash/pull/52/
#[allow(missing_debug_implementations, missing_copy_implementations)]
#[doc(hidden)]
#[derive(Clone)]
pub struct FxBuildHasherInner(usize);

impl Default for FxBuildHasherInner {
// Copied from `FxRandomState::new`.
fn default() -> Self {
use rand::Rng;
use std::{cell::Cell, thread_local};

thread_local!(static SEED: Cell<usize> = {
Cell::new(rand::thread_rng().gen())
});

SEED.with(|seed| {
let s = seed.get();
seed.set(s.wrapping_add(1));
Self(s)
})
}
}

impl core::hash::BuildHasher for FxBuildHasherInner {
type Hasher = rustc_hash::FxHasher;

#[inline]
fn build_hasher(&self) -> Self::Hasher {
rustc_hash::FxHasher::with_seed(self.0)
}
}
} else {
use rustc_hash::FxBuildHasher as FxBuildHasherInner;
}
Expand Down Expand Up @@ -127,3 +160,19 @@ cfg_if! {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn default_hasher_builder_traits() {
let hash_builder = <DefaultHashBuilder as Default>::default();
let _hash_builder2 = <DefaultHashBuilder as Clone>::clone(&hash_builder);
let mut hasher =
<DefaultHashBuilder as core::hash::BuildHasher>::build_hasher(&hash_builder);

<DefaultHasher as core::hash::Hasher>::write_u8(&mut hasher, 0);
let _hasher2 = <DefaultHasher as Clone>::clone(&hasher);
}
}