1use super::{Borrow, VecMap};
4use std::fmt::{self, Debug};
5use std::mem;
6
7pub struct RawEntryBuilderMut<'a, K, V, const N: usize, S> {
13 pub(crate) map: &'a mut VecMap<K, V, N, S>,
14}
15
16pub enum RawEntryMut<'a, K, V, const N: usize, S> {
28 Occupied(RawOccupiedEntryMut<'a, K, V, N, S>),
30 Vacant(RawVacantEntryMut<'a, K, V, N, S>),
32}
33
34pub struct RawOccupiedEntryMut<'a, K, V, const N: usize, S> {
39 idx: usize,
40 map: &'a mut VecMap<K, V, N, S>,
41}
42
43unsafe impl<K, V, const N: usize, S> Send for RawOccupiedEntryMut<'_, K, V, N, S>
44where
45 K: Send,
46 V: Send,
47 S: Send,
48{
49}
50unsafe impl<K, V, const N: usize, S> Sync for RawOccupiedEntryMut<'_, K, V, N, S>
51where
52 K: Sync,
53 V: Sync,
54 S: Send,
55{
56}
57
58pub struct RawVacantEntryMut<'a, K, V, const N: usize, S> {
63 map: &'a mut VecMap<K, V, N, S>,
64}
65
66pub struct RawEntryBuilder<'map, K, V, const N: usize, S> {
72 pub(crate) map: &'map VecMap<K, V, N, S>,
73}
74
75impl<'map, K, V, const N: usize, S> RawEntryBuilderMut<'map, K, V, N, S> {
76 #[inline]
78 #[allow(clippy::wrong_self_convention)]
79 pub fn from_key<Q>(self, k: &Q) -> RawEntryMut<'map, K, V, N, S>
80 where
81 K: Borrow<Q>,
82 Q: Eq + ?Sized,
83 {
84 self.from_key_hashed_nocheck(0, k)
85 }
86
87 #[inline]
89 #[allow(clippy::wrong_self_convention)]
90 pub fn from_key_hashed_nocheck<Q>(self, hash: u64, k: &Q) -> RawEntryMut<'map, K, V, N, S>
91 where
92 K: Borrow<Q>,
93 Q: Eq + ?Sized,
94 {
95 self.from_hash(hash, |q| q.borrow().eq(k))
96 }
97}
98
99impl<'a, K, V, const N: usize, S> RawEntryBuilderMut<'a, K, V, N, S> {
100 #[inline]
104 #[allow(clippy::wrong_self_convention)]
105 pub fn from_hash<F>(self, _hash: u64, is_match: F) -> RawEntryMut<'a, K, V, N, S>
106 where
107 for<'b> F: FnMut(&'b K) -> bool,
108 {
109 self.search(is_match)
110 }
111
112 #[inline]
113 fn search<F>(self, mut is_match: F) -> RawEntryMut<'a, K, V, N, S>
114 where
115 for<'b> F: FnMut(&'b K) -> bool,
116 {
117 for (idx, (key, _v)) in self.map.v.iter().enumerate() {
118 if is_match(key) {
119 return RawEntryMut::Occupied(RawOccupiedEntryMut { idx, map: self.map });
120 }
121 }
122 RawEntryMut::Vacant(RawVacantEntryMut { map: self.map })
123 }
124}
125
126impl<'a, K, V, const N: usize, S> RawEntryBuilder<'a, K, V, N, S> {
127 #[inline]
129 #[allow(clippy::wrong_self_convention)]
130 pub fn from_key<Q>(self, k: &Q) -> Option<(&'a K, &'a V)>
131 where
132 K: Borrow<Q>,
133 Q: Eq + ?Sized,
134 {
135 self.from_key_hashed_nocheck(0, k)
136 }
137
138 #[inline]
141 #[allow(clippy::wrong_self_convention)]
142 pub fn from_key_hashed_nocheck<Q>(self, hash: u64, k: &Q) -> Option<(&'a K, &'a V)>
143 where
144 K: Borrow<Q>,
145 Q: Eq + ?Sized,
146 {
147 self.from_hash(hash, |q| q.borrow().eq(k))
148 }
149
150 #[inline]
151 fn search<F>(self, _hash: u64, mut is_match: F) -> Option<(&'a K, &'a V)>
152 where
153 F: FnMut(&K) -> bool,
154 {
155 for (k, v) in &self.map.v {
156 if is_match(k) {
157 return Some((k, v));
158 }
159 }
160 None
161 }
162
163 #[inline]
165 #[allow(clippy::wrong_self_convention)]
166 pub fn from_hash<F>(self, hash: u64, is_match: F) -> Option<(&'a K, &'a V)>
167 where
168 F: FnMut(&K) -> bool,
169 {
170 self.search(hash, is_match)
171 }
172}
173
174impl<'a, K, V, const N: usize, S> RawEntryMut<'a, K, V, N, S> {
175 #[inline]
188 pub fn insert(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V, N, S> {
189 match self {
190 RawEntryMut::Occupied(mut entry) => {
191 entry.insert(value);
192 entry
193 }
194 RawEntryMut::Vacant(entry) => entry.insert_entry(key, value),
195 }
196 }
197
198 #[inline]
215 pub fn or_insert(self, default_key: K, default_val: V) -> (&'a mut K, &'a mut V) {
216 match self {
217 RawEntryMut::Occupied(entry) => entry.into_key_value(),
218 RawEntryMut::Vacant(entry) => entry.insert(default_key, default_val),
219 }
220 }
221
222 #[inline]
239 pub fn or_insert_with<F>(self, default: F) -> (&'a mut K, &'a mut V)
240 where
241 F: FnOnce() -> (K, V),
242 {
243 match self {
244 RawEntryMut::Occupied(entry) => entry.into_key_value(),
245 RawEntryMut::Vacant(entry) => {
246 let (k, v) = default();
247 entry.insert(k, v)
248 }
249 }
250 }
251
252 #[inline]
275 pub fn and_modify<F>(self, f: F) -> Self
276 where
277 F: FnOnce(&mut K, &mut V),
278 {
279 match self {
280 RawEntryMut::Occupied(mut entry) => {
281 {
282 let (k, v) = entry.get_key_value_mut();
283 f(k, v);
284 }
285 RawEntryMut::Occupied(entry)
286 }
287 RawEntryMut::Vacant(entry) => RawEntryMut::Vacant(entry),
288 }
289 }
290}
291
292impl<'a, K, V, const N: usize, S> RawOccupiedEntryMut<'a, K, V, N, S> {
293 #[inline]
295 pub fn key(&self) -> &K {
296 unsafe { &self.map.v.get_unchecked(self.idx).0 }
297 }
298
299 #[inline]
301 pub fn key_mut(&mut self) -> &mut K {
302 unsafe { &mut self.map.v.get_unchecked_mut(self.idx).0 }
303 }
304
305 #[inline]
308 pub fn into_key(self) -> &'a mut K {
309 unsafe { &mut self.map.v.get_unchecked_mut(self.idx).0 }
310 }
311
312 #[inline]
314 pub fn get(&self) -> &V {
315 unsafe { &self.map.v.get_unchecked(self.idx).1 }
316 }
317
318 #[inline]
321 pub fn into_mut(self) -> &'a mut V {
322 unsafe { &mut self.map.v.get_unchecked_mut(self.idx).1 }
323 }
324
325 #[inline]
327 pub fn get_mut(&mut self) -> &mut V {
328 unsafe { &mut self.map.v.get_unchecked_mut(self.idx).1 }
329 }
330
331 #[inline]
333 pub fn get_key_value(&mut self) -> (&K, &V) {
334 unsafe {
335 let (key, value) = &self.map.v.get_unchecked(self.idx);
336 (key, value)
337 }
338 }
339
340 #[inline]
342 pub fn get_key_value_mut(&mut self) -> (&mut K, &mut V) {
343 unsafe { self.map.get_mut_idx(self.idx) }
344 }
345
346 #[inline]
349 pub fn into_key_value(self) -> (&'a mut K, &'a mut V) {
350 unsafe { self.map.get_mut_idx(self.idx) }
351 }
352
353 #[inline]
355 pub fn insert(&mut self, value: V) -> V {
356 mem::replace(self.get_mut(), value)
357 }
358
359 #[inline]
361 pub fn insert_key(&mut self, key: K) -> K {
362 mem::replace(self.key_mut(), key)
363 }
364
365 #[inline]
367 pub fn remove(self) -> V {
368 self.remove_entry().1
369 }
370
371 #[inline]
373 pub fn remove_entry(self) -> (K, V) {
374 unsafe { self.map.remove_idx(self.idx) }
375 }
376}
377
378impl<'a, K, V, const N: usize, S> RawVacantEntryMut<'a, K, V, N, S> {
379 #[inline]
382 pub fn insert(self, key: K, value: V) -> (&'a mut K, &'a mut V) {
383 let i = self.map.insert_idx(key, value);
384 unsafe { self.map.get_mut_idx(i) }
385 }
386
387 #[inline]
390 #[allow(clippy::shadow_unrelated)]
391 pub fn insert_hashed_nocheck(self, _hash: u64, key: K, value: V) -> (&'a mut K, &'a mut V) {
392 self.insert(key, value)
393 }
394
395 #[inline]
397 pub fn insert_with_hasher<H>(
398 self,
399 _hash: u64,
400 key: K,
401 value: V,
402 _hasher: H,
403 ) -> (&'a mut K, &'a mut V)
404 where
405 H: Fn(&K) -> u64,
406 {
407 self.insert(key, value)
408 }
409
410 #[inline]
411 fn insert_entry(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V, N, S> {
412 let idx = self.map.insert_idx(key, value);
413 RawOccupiedEntryMut { idx, map: self.map }
414 }
415}
416
417impl<K, V, const N: usize, S> Debug for RawEntryBuilderMut<'_, K, V, N, S> {
418 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
419 f.debug_struct("RawEntryBuilder").finish()
420 }
421}
422
423impl<K, V, const N: usize, S> Debug for RawEntryMut<'_, K, V, N, S>
424where
425 K: Debug,
426 V: Debug,
427{
428 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
429 match *self {
430 RawEntryMut::Vacant(ref v) => f.debug_tuple("RawEntry").field(v).finish(),
431 RawEntryMut::Occupied(ref o) => f.debug_tuple("RawEntry").field(o).finish(),
432 }
433 }
434}
435
436impl<K: Debug, V: Debug, const N: usize, S> Debug for RawOccupiedEntryMut<'_, K, V, N, S> {
437 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
438 f.debug_struct("RawOccupiedEntryMut")
439 .field("key", self.key())
440 .field("value", self.get())
441 .finish()
442 }
443}
444
445impl<K, V, const N: usize, S> Debug for RawVacantEntryMut<'_, K, V, N, S> {
446 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
447 f.debug_struct("RawVacantEntryMut").finish()
448 }
449}
450
451impl<K, V, const N: usize, S> Debug for RawEntryBuilder<'_, K, V, N, S> {
452 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
453 f.debug_struct("RawEntryBuilder").finish()
454 }
455}