Skip to content

Commit

Permalink
fix(primitives): make sure DefaultHashBuilder implements Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 25, 2024
1 parent 6e24f46 commit c106571
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion crates/primitives/src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,27 @@ 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/
#[derive(Default)]
#[allow(missing_debug_implementations)]
#[doc(hidden)]
pub struct FxBuildHasherInner(rustc_hash::FxRandomState);

impl Clone for FxBuildHasherInner {
fn clone(&self) -> Self {
Self(Default::default())
}
}

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

fn build_hasher(&self) -> Self::Hasher {
self.0.build_hasher()
}
}
} else {
use rustc_hash::FxBuildHasher as FxBuildHasherInner;
}
Expand Down Expand Up @@ -127,3 +147,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);
}
}

0 comments on commit c106571

Please sign in to comment.