Skip to content

Commit

Permalink
Auto merge of rust-lang#292 - thomcc:nightly-hash-one, r=Amanieu
Browse files Browse the repository at this point in the history
Use `BuildHasher::hash_one` when `feature = "nightly"` is enabled.

I describe why this is good for more than just convenience here: rust-lang/rust#86161 (comment)
  • Loading branch information
bors committed Sep 10, 2021
2 parents dbd6dbe + 1437931 commit 728f9e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
29 changes: 24 additions & 5 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, Q, S>(hash_builder: &S, val: &Q) -> u64
where
Expand All @@ -255,6 +256,18 @@ where
state.finish()
}

#[cfg(feature = "nightly")]
#[cfg_attr(feature = "inline-more", inline)]
pub(crate) fn make_hash<K, Q, S>(hash_builder: &S, val: &Q) -> u64
where
K: Borrow<Q>,
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<K, S>(hash_builder: &S, val: &K) -> u64
where
Expand All @@ -267,6 +280,16 @@ where
state.finish()
}

#[cfg(feature = "nightly")]
#[cfg_attr(feature = "inline-more", inline)]
pub(crate) fn make_insert_hash<K, S>(hash_builder: &S, val: &K) -> u64
where
K: Hash,
S: BuildHasher,
{
hash_builder.hash_one(val)
}

#[cfg(feature = "ahash")]
impl<K, V> HashMap<K, V, DefaultHashBuilder> {
/// Creates an empty `HashMap`.
Expand Down Expand Up @@ -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))]
Expand All @@ -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));
Expand Down

0 comments on commit 728f9e8

Please sign in to comment.