From 1437931142f4e35f6c8d6463a485e44f83aea306 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 9 Sep 2021 19:16:59 -0700 Subject: [PATCH] Use `BuildHasher::hash_one` when `feature = "nightly"` is enabled. --- src/lib.rs | 3 ++- src/map.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b2d6584ff3..0137b50f46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,8 @@ allocator_api, slice_ptr_get, nonnull_slice_from_raw_parts, - maybe_uninit_array_assume_init + maybe_uninit_array_assume_init, + build_hasher_simple_hash_one ) )] #![allow( diff --git a/src/map.rs b/src/map.rs index b61a907161..032096439f 100644 --- a/src/map.rs +++ b/src/map.rs @@ -242,6 +242,7 @@ where move |x| k.eq(x.borrow()) } +#[cfg(not(feature = "nightly"))] #[cfg_attr(feature = "inline-more", inline)] pub(crate) fn make_hash(hash_builder: &S, val: &Q) -> u64 where @@ -255,6 +256,18 @@ where state.finish() } +#[cfg(feature = "nightly")] +#[cfg_attr(feature = "inline-more", inline)] +pub(crate) fn make_hash(hash_builder: &S, val: &Q) -> u64 +where + K: Borrow, + Q: Hash + ?Sized, + S: BuildHasher, +{ + hash_builder.hash_one(val) +} + +#[cfg(not(feature = "nightly"))] #[cfg_attr(feature = "inline-more", inline)] pub(crate) fn make_insert_hash(hash_builder: &S, val: &K) -> u64 where @@ -267,6 +280,16 @@ where state.finish() } +#[cfg(feature = "nightly")] +#[cfg_attr(feature = "inline-more", inline)] +pub(crate) fn make_insert_hash(hash_builder: &S, val: &K) -> u64 +where + K: Hash, + S: BuildHasher, +{ + hash_builder.hash_one(val) +} + #[cfg(feature = "ahash")] impl HashMap { /// Creates an empty `HashMap`. @@ -4793,8 +4816,6 @@ mod test_map { #[test] #[cfg(feature = "raw")] fn test_into_iter_refresh() { - use core::hash::{BuildHasher, Hash, Hasher}; - #[cfg(miri)] const N: usize = 32; #[cfg(not(miri))] @@ -4817,9 +4838,7 @@ mod test_map { loop { // occasionally remove some elements if i < n && rng.gen_bool(0.1) { - let mut hasher = hash_builder.build_hasher(); - i.hash(&mut hasher); - let hash_value = hasher.finish(); + let hash_value = super::make_insert_hash(&hash_builder, &i); unsafe { let e = map.table.find(hash_value, |q| q.0.eq(&i));