Skip to content

Commit

Permalink
Replace "ahash" with "default-hasher" in Cargo features
Browse files Browse the repository at this point in the history
This allows us to change the default hasher in the future without it
being a breaking change.
  • Loading branch information
Amanieu committed Jun 18, 2024
1 parent 65c553d commit 369ad20
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
20 changes: 14 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { version = "1.0.25", default-features = false, optional = true }
rkyv = { version = "0.7.42", optional = true, default-features = false, features = [
"alloc",
] }
borsh = { version = "1.5.0", default-features = false, optional = true, features = ["derive"]}

# When built as part of libstd
core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" }
Expand All @@ -36,9 +37,6 @@ allocator-api2 = { version = "0.2.9", optional = true, default-features = false,
# Equivalent trait which can be shared with other hash table implementations.
equivalent = { version = "1.0", optional = true, default-features = false }

# borsh serde
borsh = { version = "1.5.0", default-features = false, optional = true, features = ["derive"]}

[dev-dependencies]
lazy_static = "1.4"
rand = { version = "0.8.3", features = ["small_rng"] }
Expand All @@ -50,27 +48,37 @@ bumpalo = { version = "3.13.0", features = ["allocator-api2"] }
rkyv = { version = "0.7.42", features = ["validation"] }

[features]
default = ["ahash", "inline-more", "allocator-api2", "equivalent"]
default = ["default-hasher", "inline-more", "allocator-api2", "equivalent"]

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

# Enables the RustcEntry API used to provide the standard library's Entry API.
rustc-internal-api = []

# Internal feature used when building as part of the standard library.
rustc-dep-of-std = [
"nightly",
"core",
"compiler_builtins",
"alloc",
"rustc-internal-api",
]

# Enables the RawTable API.
raw = []

# Provides a default hasher. Currently this is AHash but this is subject to
# change in the future. Note that the default hasher does *not* provide HashDoS
# resistance, unlike the one in the standard library.
default-hasher = ["dep:ahash"]

# Enables usage of `#[inline]` on far more functions than by default in this
# crate. This may lead to a performance increase but often comes at a compile
# time cost.
inline-more = []

borsh = ["dep:borsh"]

[package.metadata.docs.rs]
features = ["nightly", "rayon", "serde", "raw"]
rustdoc-args = ["--generate-link-to-definition"]
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ This crate has the following Cargo features:

- `nightly`: Enables nightly-only features including: `#[may_dangle]`.
- `serde`: Enables serde serialization support.
- `borsh`: Enables borsh serialization support.
- `rkyv`: Enables rkyv serialization support.
- `rayon`: Enables rayon parallel iterator support.
- `equivalent`: Allows comparisons to be customized with the `Equivalent` trait.
- `raw`: Enables access to the experimental and unsafe `RawTable` API.
- `inline-more`: Adds inline hints to most functions, improving run-time performance at the cost
of compilation time. (enabled by default)
- `ahash`: Compiles with ahash as default hasher. (enabled by default)
- `default-hasher`: Compiles with ahash as default hasher. (enabled by default)
- `allocator-api2`: Enables support for allocators that support `allocator-api2`. (enabled by default)

## License
Expand Down
10 changes: 5 additions & 5 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use core::mem;
use core::ops::Index;

/// Default hasher for `HashMap`.
#[cfg(feature = "ahash")]
#[cfg(feature = "default-hasher")]
pub type DefaultHashBuilder = core::hash::BuildHasherDefault<ahash::AHasher>;

/// Dummy default hasher for `HashMap`.
#[cfg(not(feature = "ahash"))]
#[cfg(not(feature = "default-hasher"))]
pub enum DefaultHashBuilder {}

/// A hash map implemented with quadratic probing and SIMD lookup.
Expand Down Expand Up @@ -262,7 +262,7 @@ where
hash_builder.hash_one(val)
}

#[cfg(feature = "ahash")]
#[cfg(feature = "default-hasher")]
impl<K, V> HashMap<K, V, DefaultHashBuilder> {
/// Creates an empty `HashMap`.
///
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<K, V> HashMap<K, V, DefaultHashBuilder> {
}
}

#[cfg(feature = "ahash")]
#[cfg(feature = "default-hasher")]
impl<K, V, A: Allocator> HashMap<K, V, DefaultHashBuilder, A> {
/// Creates an empty `HashMap` using the given allocator.
///
Expand Down Expand Up @@ -2258,7 +2258,7 @@ where
}

// The default hasher is used to match the std implementation signature
#[cfg(feature = "ahash")]
#[cfg(feature = "default-hasher")]
impl<K, V, A, const N: usize> From<[(K, V); N]> for HashMap<K, V, DefaultHashBuilder, A>
where
K: Eq + Hash,
Expand Down
6 changes: 3 additions & 3 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<T: Clone, S: Clone, A: Allocator + Clone> Clone for HashSet<T, S, A> {
}
}

#[cfg(feature = "ahash")]
#[cfg(feature = "default-hasher")]
impl<T> HashSet<T, DefaultHashBuilder> {
/// Creates an empty `HashSet`.
///
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<T> HashSet<T, DefaultHashBuilder> {
}
}

#[cfg(feature = "ahash")]
#[cfg(feature = "default-hasher")]
impl<T: Hash + Eq, A: Allocator> HashSet<T, DefaultHashBuilder, A> {
/// Creates an empty `HashSet`.
///
Expand Down Expand Up @@ -1324,7 +1324,7 @@ where
}

// The default hasher is used to match the std implementation signature
#[cfg(feature = "ahash")]
#[cfg(feature = "default-hasher")]
impl<T, A, const N: usize> From<[T; N]> for HashSet<T, DefaultHashBuilder, A>
where
T: Eq + Hash,
Expand Down

0 comments on commit 369ad20

Please sign in to comment.