Skip to content

Commit 95ffed3

Browse files
committed
Change DefaultHashBuilder to an opaque newtype
This hides the underlying hasher as an implementation detail, so it's no longer a public dependency. A newtype `DefaultHasher` is also added here to wrap the underlying `<_ as BuildHasher>::Hasher` type. This is a breaking change now, requiring a semver bump, but it will allow later changes to the hasher without breaking.
1 parent bf35a11 commit 95ffed3

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hashbrown"
3-
version = "0.15.5"
3+
version = "0.16.0"
44
authors = ["Amanieu d'Antras <[email protected]>"]
55
description = "A Rust port of Google's SwissTable hash map"
66
license = "MIT OR Apache-2.0"
@@ -30,6 +30,7 @@ allocator-api2 = { version = "0.2.9", optional = true, default-features = false,
3030
] }
3131

3232
# Equivalent trait which can be shared with other hash table implementations.
33+
# NB: this is a public dependency because `Equivalent` is re-exported!
3334
equivalent = { version = "1.0", optional = true, default-features = false }
3435

3536
[dev-dependencies]

src/hasher.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#[cfg(feature = "default-hasher")]
2+
use {
3+
core::hash::{BuildHasher, Hasher},
4+
foldhash::fast::RandomState,
5+
};
6+
7+
/// Default hash builder for the `S` type parameter of
8+
/// [`HashMap`](crate::HashMap) and [`HashSet`](crate::HashSet).
9+
///
10+
/// This only implements `BuildHasher` when the "default-hasher" crate feature
11+
/// is enabled; otherwise it just serves as a placeholder, and a custom `S` type
12+
/// must be used to have a fully functional `HashMap` or `HashSet`.
13+
#[derive(Clone, Debug, Default)]
14+
pub struct DefaultHashBuilder {
15+
#[cfg(feature = "default-hasher")]
16+
inner: RandomState,
17+
}
18+
19+
#[cfg(feature = "default-hasher")]
20+
impl BuildHasher for DefaultHashBuilder {
21+
type Hasher = DefaultHasher;
22+
23+
#[inline(always)]
24+
fn build_hasher(&self) -> Self::Hasher {
25+
DefaultHasher {
26+
inner: self.inner.build_hasher(),
27+
}
28+
}
29+
}
30+
31+
/// Default hasher for [`HashMap`](crate::HashMap) and [`HashSet`](crate::HashSet).
32+
#[cfg(feature = "default-hasher")]
33+
#[derive(Clone)]
34+
pub struct DefaultHasher {
35+
inner: <RandomState as BuildHasher>::Hasher,
36+
}
37+
38+
#[cfg(feature = "default-hasher")]
39+
macro_rules! forward_writes {
40+
($( $write:ident ( $ty:ty ) , )*) => {$(
41+
#[inline(always)]
42+
fn $write(&mut self, arg: $ty) {
43+
self.inner.$write(arg);
44+
}
45+
)*}
46+
}
47+
48+
#[cfg(feature = "default-hasher")]
49+
impl Hasher for DefaultHasher {
50+
forward_writes! {
51+
write(&[u8]),
52+
write_u8(u8),
53+
write_u16(u16),
54+
write_u32(u32),
55+
write_u64(u64),
56+
write_u128(u128),
57+
write_usize(usize),
58+
write_i8(i8),
59+
write_i16(i16),
60+
write_i32(i32),
61+
write_i64(i64),
62+
write_i128(i128),
63+
write_isize(isize),
64+
}
65+
66+
#[inline(always)]
67+
fn finish(&self) -> u64 {
68+
self.inner.finish()
69+
}
70+
}

src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@
4747
feature(stdarch_loongarch)
4848
)]
4949

50-
/// Default hasher for [`HashMap`] and [`HashSet`].
51-
#[cfg(feature = "default-hasher")]
52-
pub type DefaultHashBuilder = foldhash::fast::RandomState;
53-
54-
/// Dummy default hasher for [`HashMap`] and [`HashSet`].
55-
#[cfg(not(feature = "default-hasher"))]
56-
pub enum DefaultHashBuilder {}
57-
5850
#[cfg(test)]
5951
#[macro_use]
6052
extern crate std;
@@ -71,6 +63,7 @@ doc_comment::doctest!("../README.md");
7163
mod macros;
7264

7365
mod control;
66+
mod hasher;
7467
mod raw;
7568
mod util;
7669

@@ -84,6 +77,10 @@ mod scopeguard;
8477
mod set;
8578
mod table;
8679

80+
pub use crate::hasher::DefaultHashBuilder;
81+
#[cfg(feature = "default-hasher")]
82+
pub use crate::hasher::DefaultHasher;
83+
8784
pub mod hash_map {
8885
//! A hash map implemented with quadratic probing and SIMD lookup.
8986
pub use crate::map::*;

0 commit comments

Comments
 (0)