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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hashbrown"
version = "0.15.5"
version = "0.16.0"
authors = ["Amanieu d'Antras <[email protected]>"]
description = "A Rust port of Google's SwissTable hash map"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -30,6 +30,7 @@ allocator-api2 = { version = "0.2.9", optional = true, default-features = false,
] }

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

[dev-dependencies]
Expand All @@ -46,7 +47,7 @@ default = ["default-hasher", "inline-more", "allocator-api2", "equivalent", "raw

# Enables use of nightly features. This is only guaranteed to work on the latest
# version of nightly Rust.
nightly = ["bumpalo/allocator_api"]
nightly = ["foldhash?/nightly", "bumpalo/allocator_api"]

# Enables the RustcEntry API used to provide the standard library's Entry API.
rustc-internal-api = []
Expand Down
77 changes: 77 additions & 0 deletions src/hasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#[cfg(feature = "default-hasher")]
use {
core::hash::{BuildHasher, Hasher},
foldhash::fast::RandomState,
};

/// Default hash builder for the `S` type parameter of
/// [`HashMap`](crate::HashMap) and [`HashSet`](crate::HashSet).
///
/// This only implements `BuildHasher` when the "default-hasher" crate feature
/// is enabled; otherwise it just serves as a placeholder, and a custom `S` type
/// must be used to have a fully functional `HashMap` or `HashSet`.
#[derive(Clone, Debug, Default)]
pub struct DefaultHashBuilder {
#[cfg(feature = "default-hasher")]
inner: RandomState,
}

#[cfg(feature = "default-hasher")]
impl BuildHasher for DefaultHashBuilder {
type Hasher = DefaultHasher;

#[inline(always)]
fn build_hasher(&self) -> Self::Hasher {
DefaultHasher {
inner: self.inner.build_hasher(),
}
}
}

/// Default hasher for [`HashMap`](crate::HashMap) and [`HashSet`](crate::HashSet).
#[cfg(feature = "default-hasher")]
#[derive(Clone)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary for this to implement Clone? I don't think it every makes sense to clone a Hasher.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not necessary, but FWIW both FoldHasher and std::hash::DefaultHasher do implement Clone.

(std also implements Debug and Default, but FoldHasher does not.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std added Clone in rust-lang/rust#42799, without much discussion.

pub struct DefaultHasher {
inner: <RandomState as BuildHasher>::Hasher,
}

#[cfg(feature = "default-hasher")]
macro_rules! forward_writes {
($( $write:ident ( $ty:ty ) , )*) => {$(
#[inline(always)]
fn $write(&mut self, arg: $ty) {
self.inner.$write(arg);
}
)*}
}

#[cfg(feature = "default-hasher")]
impl Hasher for DefaultHasher {
forward_writes! {
write(&[u8]),
write_u8(u8),
write_u16(u16),
write_u32(u32),
write_u64(u64),
write_u128(u128),
write_usize(usize),
write_i8(i8),
write_i16(i16),
write_i32(i32),
write_i64(i64),
write_i128(i128),
write_isize(isize),
}

// feature(hasher_prefixfree_extras)
#[cfg(feature = "nightly")]
forward_writes! {
write_length_prefix(usize),
write_str(&str),
}

#[inline(always)]
fn finish(&self) -> u64 {
self.inner.finish()
}
}
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,10 @@
all(feature = "nightly", target_arch = "loongarch64"),
feature(stdarch_loongarch)
)]

/// Default hasher for [`HashMap`] and [`HashSet`].
#[cfg(feature = "default-hasher")]
pub type DefaultHashBuilder = foldhash::fast::RandomState;

/// Dummy default hasher for [`HashMap`] and [`HashSet`].
#[cfg(not(feature = "default-hasher"))]
pub enum DefaultHashBuilder {}
#![cfg_attr(
all(feature = "nightly", feature = "default-hasher"),
feature(hasher_prefixfree_extras)
)]

#[cfg(test)]
#[macro_use]
Expand All @@ -71,6 +67,7 @@ doc_comment::doctest!("../README.md");
mod macros;

mod control;
mod hasher;
mod raw;
mod util;

Expand All @@ -84,6 +81,10 @@ mod scopeguard;
mod set;
mod table;

pub use crate::hasher::DefaultHashBuilder;
#[cfg(feature = "default-hasher")]
pub use crate::hasher::DefaultHasher;

pub mod hash_map {
//! A hash map implemented with quadratic probing and SIMD lookup.
pub use crate::map::*;
Expand Down
Loading