1pub trait StoreEntry<'a, K, V> {
10 fn is_occupied(&self) -> bool;
12 fn is_vacant(&self) -> bool;
14}
15
16pub trait RawStore<K, V> {
19 fn get(&self, key: &K) -> Option<&V>;
21 fn contains_key(&self, key: &K) -> bool {
23 self.get(key).is_some()
24 }
25}
26pub trait RawStoreMut<K, V>: RawStore<K, V> {
29 fn get_mut(&mut self, key: &K) -> Option<&mut V>;
31 fn insert(&mut self, key: K, value: V) -> Option<V>;
33 fn remove(&mut self, key: &K) -> Option<V>;
35}
36pub trait Store<K, V>: RawStoreMut<K, V> {
40 type Entry<'a>: StoreEntry<'a, K, V>
41 where
42 Self: 'a;
43 fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a>;
45}
46#[cfg(feature = "alloc")]
106mod impl_alloc {
107 use super::*;
108
109 use alloc::collections::btree_map::{self, BTreeMap};
110
111 impl<'a, K, V> StoreEntry<'a, K, V> for btree_map::Entry<'a, K, V> {
112 fn is_occupied(&self) -> bool {
113 matches!(self, btree_map::Entry::Occupied(_))
114 }
115
116 fn is_vacant(&self) -> bool {
117 matches!(self, btree_map::Entry::Vacant(_))
118 }
119 }
120
121 impl<K, V> RawStore<K, V> for BTreeMap<K, V>
122 where
123 K: Ord,
124 {
125 fn contains_key(&self, key: &K) -> bool {
126 BTreeMap::contains_key(self, key)
127 }
128
129 fn get(&self, key: &K) -> Option<&V> {
130 BTreeMap::get(self, key)
131 }
132 }
133
134 impl<K, V> Store<K, V> for BTreeMap<K, V>
135 where
136 K: Ord,
137 {
138 type Entry<'a>
139 = btree_map::Entry<'a, K, V>
140 where
141 Self: 'a;
142
143 fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a> {
144 BTreeMap::entry(self, key)
145 }
146 }
147
148 impl<K, V> RawStoreMut<K, V> for BTreeMap<K, V>
149 where
150 K: Ord,
151 {
152 fn insert(&mut self, key: K, value: V) -> Option<V> {
153 BTreeMap::insert(self, key, value)
154 }
155
156 fn get_mut(&mut self, key: &K) -> Option<&mut V> {
157 BTreeMap::get_mut(self, key)
158 }
159
160 fn remove(&mut self, key: &K) -> Option<V> {
161 BTreeMap::remove(self, key)
162 }
163 }
164}
165
166#[cfg(feature = "hashbrown")]
167mod impl_hashbrown {
168 use super::*;
169 use core::hash::{BuildHasher, Hash};
170 use hashbrown::hash_map::{self, HashMap};
171
172 impl<K, V, S> StoreEntry<'_, K, V> for hash_map::Entry<'_, K, V, S> {
173 fn is_occupied(&self) -> bool {
174 matches!(self, hash_map::Entry::Occupied(_))
175 }
176
177 fn is_vacant(&self) -> bool {
178 matches!(self, hash_map::Entry::Vacant(_))
179 }
180 }
181
182 impl<K, V, S> RawStore<K, V> for HashMap<K, V, S>
183 where
184 K: Eq + Hash,
185 S: BuildHasher,
186 {
187 fn contains_key(&self, key: &K) -> bool {
188 HashMap::contains_key(self, key)
189 }
190
191 fn get(&self, key: &K) -> Option<&V> {
192 HashMap::get(self, key)
193 }
194 }
195
196 impl<K, V, S> RawStoreMut<K, V> for HashMap<K, V, S>
197 where
198 K: Eq + Hash,
199 S: BuildHasher,
200 {
201 fn insert(&mut self, key: K, value: V) -> Option<V> {
202 HashMap::insert(self, key, value)
203 }
204
205 fn get_mut(&mut self, key: &K) -> Option<&mut V> {
206 HashMap::get_mut(self, key)
207 }
208
209 fn remove(&mut self, key: &K) -> Option<V> {
210 HashMap::remove(self, key)
211 }
212 }
213
214 impl<K, V, S> Store<K, V> for HashMap<K, V, S>
215 where
216 K: Eq + Hash,
217 S: BuildHasher,
218 {
219 type Entry<'a>
220 = hash_map::Entry<'a, K, V, S>
221 where
222 Self: 'a;
223
224 fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a> {
225 HashMap::entry(self, key)
226 }
227 }
228}
229
230#[cfg(feature = "std")]
231mod impl_std {
232 use super::*;
233 use core::hash::Hash;
234 use std::collections::hash_map::{self, HashMap};
235
236 impl<K, V> StoreEntry<'_, K, V> for hash_map::Entry<'_, K, V> {
237 fn is_occupied(&self) -> bool {
238 matches!(self, hash_map::Entry::Occupied(_))
239 }
240
241 fn is_vacant(&self) -> bool {
242 matches!(self, hash_map::Entry::Vacant(_))
243 }
244 }
245
246 impl<K, V> RawStore<K, V> for HashMap<K, V>
247 where
248 K: Eq + Hash,
249 {
250 fn contains_key(&self, key: &K) -> bool {
251 HashMap::contains_key(self, key)
252 }
253
254 fn get(&self, key: &K) -> Option<&V> {
255 HashMap::get(self, key)
256 }
257 }
258
259 impl<K, V> RawStoreMut<K, V> for HashMap<K, V>
260 where
261 K: Eq + Hash,
262 {
263 fn insert(&mut self, key: K, value: V) -> Option<V> {
264 HashMap::insert(self, key, value)
265 }
266 fn get_mut(&mut self, key: &K) -> Option<&mut V> {
267 HashMap::get_mut(self, key)
268 }
269
270 fn remove(&mut self, key: &K) -> Option<V> {
271 HashMap::remove(self, key)
272 }
273 }
274
275 impl<K, V> Store<K, V> for HashMap<K, V>
276 where
277 K: Eq + Hash,
278 {
279 type Entry<'a>
280 = hash_map::Entry<'a, K, V>
281 where
282 Self: 'a;
283
284 fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a> {
285 HashMap::entry(self, key)
286 }
287 }
288}