hashbrown/lib.rs
1//! This crate is a Rust port of Google's high-performance [SwissTable] hash
2//! map, adapted to make it a drop-in replacement for Rust's standard `HashMap`
3//! and `HashSet` types.
4//!
5//! The original C++ version of [SwissTable] can be found [here], and this
6//! [CppCon talk] gives an overview of how the algorithm works.
7//!
8//! [SwissTable]: https://abseil.io/blog/20180927-swisstables
9//! [here]: https://github.com/abseil/abseil-cpp/blob/master/absl/container/internal/raw_hash_set.h
10//! [CppCon talk]: https://www.youtube.com/watch?v=ncHmEUmJZf4
11
12#![no_std]
13#![cfg_attr(
14 feature = "nightly",
15 feature(
16 test,
17 core_intrinsics,
18 dropck_eyepatch,
19 min_specialization,
20 extend_one,
21 allocator_api,
22 slice_ptr_get,
23 maybe_uninit_array_assume_init,
24 )
25)]
26#![allow(
27 clippy::doc_markdown,
28 clippy::module_name_repetitions,
29 clippy::must_use_candidate,
30 clippy::option_if_let_else,
31 clippy::redundant_else,
32 clippy::manual_map,
33 clippy::missing_safety_doc,
34 clippy::missing_errors_doc
35)]
36#![warn(missing_docs)]
37#![warn(rust_2018_idioms)]
38#![cfg_attr(feature = "nightly", allow(internal_features))]
39
40/// Default hasher for [`HashMap`] and [`HashSet`].
41#[cfg(feature = "default-hasher")]
42pub type DefaultHashBuilder = foldhash::fast::RandomState;
43
44/// Dummy default hasher for [`HashMap`] and [`HashSet`].
45#[cfg(not(feature = "default-hasher"))]
46pub enum DefaultHashBuilder {}
47
48#[cfg(test)]
49#[macro_use]
50extern crate std;
51
52#[cfg_attr(test, macro_use)]
53extern crate alloc;
54
55#[cfg(feature = "nightly")]
56#[cfg(doctest)]
57doc_comment::doctest!("../README.md");
58
59#[macro_use]
60mod macros;
61
62mod raw;
63
64mod external_trait_impls;
65mod map;
66#[cfg(feature = "raw-entry")]
67mod raw_entry;
68#[cfg(feature = "rustc-internal-api")]
69mod rustc_entry;
70mod scopeguard;
71mod set;
72mod table;
73
74pub mod hash_map {
75 //! A hash map implemented with quadratic probing and SIMD lookup.
76 pub use crate::map::*;
77
78 #[cfg(feature = "rustc-internal-api")]
79 pub use crate::rustc_entry::*;
80
81 #[cfg(feature = "rayon")]
82 /// [rayon]-based parallel iterator types for hash maps.
83 /// You will rarely need to interact with it directly unless you have need
84 /// to name one of the iterator types.
85 ///
86 /// [rayon]: https://docs.rs/rayon/1.0/rayon
87 pub mod rayon {
88 pub use crate::external_trait_impls::rayon::map::*;
89 }
90}
91pub mod hash_set {
92 //! A hash set implemented as a `HashMap` where the value is `()`.
93 pub use crate::set::*;
94
95 #[cfg(feature = "rayon")]
96 /// [rayon]-based parallel iterator types for hash sets.
97 /// You will rarely need to interact with it directly unless you have need
98 /// to name one of the iterator types.
99 ///
100 /// [rayon]: https://docs.rs/rayon/1.0/rayon
101 pub mod rayon {
102 pub use crate::external_trait_impls::rayon::set::*;
103 }
104}
105pub mod hash_table {
106 //! A hash table implemented with quadratic probing and SIMD lookup.
107 pub use crate::table::*;
108
109 #[cfg(feature = "rayon")]
110 /// [rayon]-based parallel iterator types for hash tables.
111 /// You will rarely need to interact with it directly unless you have need
112 /// to name one of the iterator types.
113 ///
114 /// [rayon]: https://docs.rs/rayon/1.0/rayon
115 pub mod rayon {
116 pub use crate::external_trait_impls::rayon::table::*;
117 }
118}
119
120pub use crate::map::HashMap;
121pub use crate::set::HashSet;
122pub use crate::table::HashTable;
123
124#[cfg(feature = "equivalent")]
125pub use equivalent::Equivalent;
126
127// This is only used as a fallback when building as part of `std`.
128#[cfg(not(feature = "equivalent"))]
129/// Key equivalence trait.
130///
131/// This trait defines the function used to compare the input value with the
132/// map keys (or set values) during a lookup operation such as [`HashMap::get`]
133/// or [`HashSet::contains`].
134/// It is provided with a blanket implementation based on the
135/// [`Borrow`](core::borrow::Borrow) trait.
136///
137/// # Correctness
138///
139/// Equivalent values must hash to the same value.
140pub trait Equivalent<K: ?Sized> {
141 /// Checks if this value is equivalent to the given key.
142 ///
143 /// Returns `true` if both values are equivalent, and `false` otherwise.
144 ///
145 /// # Correctness
146 ///
147 /// When this function returns `true`, both `self` and `key` must hash to
148 /// the same value.
149 fn equivalent(&self, key: &K) -> bool;
150}
151
152#[cfg(not(feature = "equivalent"))]
153impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q
154where
155 Q: Eq,
156 K: core::borrow::Borrow<Q>,
157{
158 fn equivalent(&self, key: &K) -> bool {
159 self == key.borrow()
160 }
161}
162
163/// The error type for `try_reserve` methods.
164#[derive(Clone, PartialEq, Eq, Debug)]
165pub enum TryReserveError {
166 /// Error due to the computed capacity exceeding the collection's maximum
167 /// (usually `isize::MAX` bytes).
168 CapacityOverflow,
169
170 /// The memory allocator returned an error
171 AllocError {
172 /// The layout of the allocation request that failed.
173 layout: alloc::alloc::Layout,
174 },
175}