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        strict_provenance_lints
25    )
26)]
27#![cfg_attr(feature = "rustc-dep-of-std", feature(rustc_attrs))]
28#![allow(
29    clippy::doc_markdown,
30    clippy::module_name_repetitions,
31    clippy::must_use_candidate,
32    clippy::option_if_let_else,
33    clippy::redundant_else,
34    clippy::manual_map,
35    clippy::missing_safety_doc,
36    clippy::missing_errors_doc
37)]
38#![warn(missing_docs)]
39#![warn(rust_2018_idioms)]
40#![cfg_attr(feature = "nightly", warn(fuzzy_provenance_casts))]
41#![cfg_attr(
42    feature = "nightly",
43    allow(clippy::incompatible_msrv, internal_features)
44)]
45#![cfg_attr(
46    all(feature = "nightly", target_arch = "loongarch64"),
47    feature(stdarch_loongarch)
48)]
49
50/// Default hasher for [`HashMap`] and [`HashSet`].
51#[cfg(feature = "default-hasher")]
52pub type DefaultHashBuilder = foldhash::fast::RandomState;
53
54/// Dummy default hasher for [`HashMap`] and [`HashSet`].
55#[cfg(not(feature = "default-hasher"))]
56pub enum DefaultHashBuilder {}
57
58#[cfg(test)]
59#[macro_use]
60extern crate std;
61
62#[cfg_attr(test, macro_use)]
63#[cfg_attr(feature = "rustc-dep-of-std", allow(unused_extern_crates))]
64extern crate alloc;
65
66#[cfg(feature = "nightly")]
67#[cfg(doctest)]
68doc_comment::doctest!("../README.md");
69
70#[macro_use]
71mod macros;
72
73mod control;
74mod raw;
75mod util;
76
77mod external_trait_impls;
78mod map;
79#[cfg(feature = "raw-entry")]
80mod raw_entry;
81#[cfg(feature = "rustc-internal-api")]
82mod rustc_entry;
83mod scopeguard;
84mod set;
85mod table;
86
87pub mod hash_map {
88    //! A hash map implemented with quadratic probing and SIMD lookup.
89    pub use crate::map::*;
90
91    #[cfg(feature = "rustc-internal-api")]
92    pub use crate::rustc_entry::*;
93
94    #[cfg(feature = "rayon")]
95    /// [rayon]-based parallel iterator types for hash maps.
96    /// You will rarely need to interact with it directly unless you have need
97    /// to name one of the iterator types.
98    ///
99    /// [rayon]: https://docs.rs/rayon/1.0/rayon
100    pub mod rayon {
101        pub use crate::external_trait_impls::rayon::map::*;
102    }
103}
104pub mod hash_set {
105    //! A hash set implemented as a `HashMap` where the value is `()`.
106    pub use crate::set::*;
107
108    #[cfg(feature = "rayon")]
109    /// [rayon]-based parallel iterator types for hash sets.
110    /// You will rarely need to interact with it directly unless you have need
111    /// to name one of the iterator types.
112    ///
113    /// [rayon]: https://docs.rs/rayon/1.0/rayon
114    pub mod rayon {
115        pub use crate::external_trait_impls::rayon::set::*;
116    }
117}
118pub mod hash_table {
119    //! A hash table implemented with quadratic probing and SIMD lookup.
120    pub use crate::table::*;
121
122    #[cfg(feature = "rayon")]
123    /// [rayon]-based parallel iterator types for hash tables.
124    /// You will rarely need to interact with it directly unless you have need
125    /// to name one of the iterator types.
126    ///
127    /// [rayon]: https://docs.rs/rayon/1.0/rayon
128    pub mod rayon {
129        pub use crate::external_trait_impls::rayon::table::*;
130    }
131}
132
133pub use crate::map::HashMap;
134pub use crate::set::HashSet;
135pub use crate::table::HashTable;
136
137#[cfg(feature = "equivalent")]
138pub use equivalent::Equivalent;
139
140// This is only used as a fallback when building as part of `std`.
141#[cfg(not(feature = "equivalent"))]
142/// Key equivalence trait.
143///
144/// This trait defines the function used to compare the input value with the
145/// map keys (or set values) during a lookup operation such as [`HashMap::get`]
146/// or [`HashSet::contains`].
147/// It is provided with a blanket implementation based on the
148/// [`Borrow`](core::borrow::Borrow) trait.
149///
150/// # Correctness
151///
152/// Equivalent values must hash to the same value.
153pub trait Equivalent<K: ?Sized> {
154    /// Checks if this value is equivalent to the given key.
155    ///
156    /// Returns `true` if both values are equivalent, and `false` otherwise.
157    ///
158    /// # Correctness
159    ///
160    /// When this function returns `true`, both `self` and `key` must hash to
161    /// the same value.
162    fn equivalent(&self, key: &K) -> bool;
163}
164
165#[cfg(not(feature = "equivalent"))]
166impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q
167where
168    Q: Eq,
169    K: core::borrow::Borrow<Q>,
170{
171    fn equivalent(&self, key: &K) -> bool {
172        self == key.borrow()
173    }
174}
175
176/// The error type for `try_reserve` methods.
177#[derive(Clone, PartialEq, Eq, Debug)]
178pub enum TryReserveError {
179    /// Error due to the computed capacity exceeding the collection's maximum
180    /// (usually `isize::MAX` bytes).
181    CapacityOverflow,
182
183    /// The memory allocator returned an error
184    AllocError {
185        /// The layout of the allocation request that failed.
186        layout: alloc::alloc::Layout,
187    },
188}