1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4use self::compat::*;
5
6pub mod ptr_weak_hash_set;
7pub mod ptr_weak_key_hash_map;
8pub mod ptr_weak_weak_hash_map;
9pub mod traits;
10pub mod weak_hash_set;
11pub mod weak_key_hash_map;
12pub mod weak_value_hash_map;
13pub mod weak_weak_hash_map;
14
15#[cfg(test)]
16mod tests;
17
18mod by_ptr;
19mod common;
20mod compat;
21mod inner;
22mod size_policy;
23mod util;
24
25#[cfg(any(test, feature = "std", feature = "ahash"))]
27macro_rules! declare_structs {
28 {
29 $(
30 $(#[$meta:meta])*
31 pub struct $name:ident < $($param:ident),* ,?S > ( $($members:tt)+ );
32 )*
33 } => {
34 $(
35 $(#[$meta])*
36 pub struct $name < $($param),* , S = RandomState > ( $($members)+ );
37 )*
38 }
39}
40
41#[cfg(not(any(test, feature = "std", feature = "ahash")))]
43macro_rules! declare_structs {
44 {
45 $(
46 $(#[$meta:meta])*
47 pub struct $name:ident < $($param:ident),* ,?S > ( $($members:tt)+ );
48 )*
49 } => {
50 $(
51 $(#[$meta])*
52 pub struct $name < $($param),* , S > ( $($members)+ );
53 )*
54 }
55}
56
57declare_structs! {
58#[derive(Clone)]
62pub struct WeakKeyHashMap<K, V, ?S>(inner::Table<inner::WeakK<K>, inner::Owned<V>, S>);
63
64#[derive(Clone)]
95pub struct PtrWeakKeyHashMap<K, V,?S>(WeakKeyHashMap<by_ptr::ByPtr<K>, V, S>);
96
97#[derive(Clone)]
101pub struct WeakValueHashMap<K, V, ?S>(
102 inner::Table<inner::Owned<K>, inner::WeakV<V>, S>,
103);
104
105#[derive(Clone)]
109pub struct WeakWeakHashMap<K, V, ?S>(
110 inner::Table<inner::WeakK<K>, inner::WeakV<V>, S>,
111);
112
113#[derive(Clone)]
117pub struct PtrWeakWeakHashMap<K, V, ?S>(WeakWeakHashMap<by_ptr::ByPtr<K>, V, S>);
118
119#[derive(Clone)]
123pub struct WeakHashSet<T, ?S>(WeakKeyHashMap<T, (), S>);
124
125#[derive(Clone)]
129pub struct PtrWeakHashSet<T, ?S>(PtrWeakKeyHashMap<T, (), S>);
130}
131
132#[derive(Clone, Debug)]
134#[non_exhaustive]
135pub enum TryReserveError {
136 CapacityOverflow,
139
140 AllocError {
142 layout: Layout,
144 },
145}
146
147impl TryReserveError {
148 #[allow(clippy::needless_pass_by_value)]
153 pub(crate) fn from_hashbrown(error: hashbrown::TryReserveError) -> Self {
154 match error {
155 hashbrown::TryReserveError::CapacityOverflow => TryReserveError::CapacityOverflow,
156 hashbrown::TryReserveError::AllocError { layout } => {
157 TryReserveError::AllocError { layout }
158 }
159 }
160 }
161}
162impl Display for TryReserveError {
163 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
164 match self {
165 TryReserveError::CapacityOverflow => write!(
166 f,
167 "Allocation failed: arithmetic overflow in capacity calculation"
168 ),
169 TryReserveError::AllocError { .. } => {
170 write!(f, "Allocation failed: memory allocator returned an error")
171 }
172 }
173 }
174}
175#[cfg(feature = "std")]
176impl Error for TryReserveError {}