1use core::fmt::Debug;
4use core::{borrow::Borrow, hash::Hash, iter::FusedIterator, ops::Index};
5
6#[cfg(not(feature = "no-hash-maps"))]
7mod detail {
8 use crate::collections::hash;
9 use hashbrown::hash_map;
10
11 pub type MapImpl<K, V> = hash_map::HashMap<K, V, hash::RandomState>;
12 pub type EntryImpl<'a, K, V> = hash_map::Entry<'a, K, V, hash::RandomState>;
13 pub type OccupiedEntryImpl<'a, K, V> = hash_map::OccupiedEntry<'a, K, V, hash::RandomState>;
14 pub type VacantEntryImpl<'a, K, V> = hash_map::VacantEntry<'a, K, V, hash::RandomState>;
15 pub type IterImpl<'a, K, V> = hash_map::Iter<'a, K, V>;
16 pub type IterMutImpl<'a, K, V> = hash_map::IterMut<'a, K, V>;
17 pub type IntoIterImpl<K, V> = hash_map::IntoIter<K, V>;
18 pub type KeysImpl<'a, K, V> = hash_map::Keys<'a, K, V>;
19 pub type ValuesImpl<'a, K, V> = hash_map::Values<'a, K, V>;
20 pub type ValuesMutImpl<'a, K, V> = hash_map::ValuesMut<'a, K, V>;
21 pub type IntoKeysImpl<K, V> = hash_map::IntoKeys<K, V>;
22 pub type IntoValuesImpl<K, V> = hash_map::IntoValues<K, V>;
23}
24
25#[cfg(feature = "no-hash-maps")]
26mod detail {
27 use alloc::collections::btree_map;
28
29 pub type MapImpl<K, V> = btree_map::BTreeMap<K, V>;
30 pub type EntryImpl<'a, K, V> = btree_map::Entry<'a, K, V>;
31 pub type OccupiedEntryImpl<'a, K, V> = btree_map::OccupiedEntry<'a, K, V>;
32 pub type VacantEntryImpl<'a, K, V> = btree_map::VacantEntry<'a, K, V>;
33 pub type IterImpl<'a, K, V> = btree_map::Iter<'a, K, V>;
34 pub type IterMutImpl<'a, K, V> = btree_map::IterMut<'a, K, V>;
35 pub type IntoIterImpl<K, V> = btree_map::IntoIter<K, V>;
36 pub type KeysImpl<'a, K, V> = btree_map::Keys<'a, K, V>;
37 pub type ValuesImpl<'a, K, V> = btree_map::Values<'a, K, V>;
38 pub type ValuesMutImpl<'a, K, V> = btree_map::ValuesMut<'a, K, V>;
39 pub type IntoKeysImpl<K, V> = btree_map::IntoKeys<K, V>;
40 pub type IntoValuesImpl<K, V> = btree_map::IntoValues<K, V>;
41}
42
43#[derive(Debug, Clone)]
50pub struct Map<K, V> {
51 inner: detail::MapImpl<K, V>,
52}
53
54impl<K, V> Default for Map<K, V> {
55 #[inline]
56 fn default() -> Self {
57 Self {
58 inner: detail::MapImpl::default(),
59 }
60 }
61}
62
63impl<K, V> Map<K, V> {
64 #[inline]
66 pub fn new() -> Self {
67 Self::default()
68 }
69
70 #[inline]
72 pub fn clear(&mut self) {
73 self.inner.clear()
74 }
75
76 #[inline]
78 pub fn len(&self) -> usize {
79 self.inner.len()
80 }
81
82 #[inline]
84 pub fn is_empty(&self) -> bool {
85 self.inner.is_empty()
86 }
87
88 #[inline]
90 pub fn iter(&self) -> Iter<'_, K, V> {
91 Iter {
92 inner: self.inner.iter(),
93 }
94 }
95
96 #[inline]
98 pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
99 IterMut {
100 inner: self.inner.iter_mut(),
101 }
102 }
103
104 #[inline]
106 pub fn keys(&self) -> Keys<'_, K, V> {
107 Keys {
108 inner: self.inner.keys(),
109 }
110 }
111
112 #[inline]
117 pub fn into_keys(self) -> IntoKeys<K, V> {
118 IntoKeys {
119 inner: self.inner.into_keys(),
120 }
121 }
122
123 #[inline]
125 pub fn values(&self) -> Values<'_, K, V> {
126 Values {
127 inner: self.inner.values(),
128 }
129 }
130
131 #[inline]
136 pub fn into_values(self) -> IntoValues<K, V> {
137 IntoValues {
138 inner: self.inner.into_values(),
139 }
140 }
141
142 #[inline]
144 pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
145 ValuesMut {
146 inner: self.inner.values_mut(),
147 }
148 }
149}
150
151impl<K, V> Map<K, V>
152where
153 K: Hash + Eq + Ord,
154{
155 #[inline]
157 pub fn reserve(&mut self, additional: usize) {
158 #[cfg(not(feature = "no-hash-maps"))]
159 self.inner.reserve(additional);
160 #[cfg(feature = "no-hash-maps")]
161 let _ = additional;
162 }
163
164 #[inline]
166 pub fn contains_key<Q>(&self, key: &Q) -> bool
167 where
168 K: Borrow<Q>,
169 Q: ?Sized + Hash + Eq + Ord,
170 {
171 self.inner.contains_key(key)
172 }
173
174 #[inline]
176 pub fn get<Q>(&self, key: &Q) -> Option<&V>
177 where
178 K: Borrow<Q>,
179 Q: ?Sized + Hash + Eq + Ord,
180 {
181 self.inner.get(key)
182 }
183
184 #[inline]
189 pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
190 where
191 K: Borrow<Q>,
192 Q: ?Sized + Hash + Eq + Ord,
193 {
194 self.inner.get_key_value(key)
195 }
196
197 #[inline]
199 pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
200 where
201 K: Borrow<Q>,
202 Q: ?Sized + Hash + Eq + Ord,
203 {
204 self.inner.get_mut(key)
205 }
206
207 #[inline]
215 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
216 self.inner.insert(key, value)
217 }
218
219 #[inline]
221 pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
222 where
223 K: Borrow<Q>,
224 Q: ?Sized + Hash + Eq + Ord,
225 {
226 self.inner.remove(key)
227 }
228
229 #[inline]
235 pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
236 where
237 K: Borrow<Q>,
238 Q: ?Sized + Hash + Ord,
239 {
240 self.inner.remove_entry(key)
241 }
242
243 #[inline]
245 pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
246 match self.inner.entry(key) {
247 detail::EntryImpl::Occupied(entry) => Entry::Occupied(OccupiedEntry { inner: entry }),
248 detail::EntryImpl::Vacant(entry) => Entry::Vacant(VacantEntry { inner: entry }),
249 }
250 }
251
252 #[inline]
257 pub fn retain<F>(&mut self, f: F)
258 where
259 F: FnMut(&K, &mut V) -> bool,
260 {
261 self.inner.retain(f)
262 }
263}
264
265impl<K, V> PartialEq for Map<K, V>
266where
267 K: Eq + Hash,
268 V: Eq,
269{
270 #[inline]
271 fn eq(&self, other: &Self) -> bool {
272 self.inner == other.inner
273 }
274}
275
276impl<K, V> Eq for Map<K, V>
277where
278 K: Eq + Hash,
279 V: Eq,
280{
281}
282
283impl<K, Q, V> Index<&Q> for Map<K, V>
284where
285 K: Borrow<Q> + Hash + Eq + Ord,
286 Q: ?Sized + Hash + Eq + Ord,
287{
288 type Output = V;
289
290 #[inline]
291 fn index(&self, key: &Q) -> &V {
292 &self.inner[key]
293 }
294}
295
296impl<'a, K, V> Extend<(&'a K, &'a V)> for Map<K, V>
297where
298 K: Eq + Hash + Ord + Copy,
299 V: Copy,
300{
301 #[inline]
302 fn extend<Iter: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: Iter) {
303 self.inner.extend(iter)
304 }
305}
306
307impl<K, V> Extend<(K, V)> for Map<K, V>
308where
309 K: Eq + Hash + Ord,
310{
311 #[inline]
312 fn extend<Iter: IntoIterator<Item = (K, V)>>(&mut self, iter: Iter) {
313 self.inner.extend(iter)
314 }
315}
316
317#[derive(Debug)]
321pub enum Entry<'a, K: Ord, V> {
322 Occupied(OccupiedEntry<'a, K, V>),
324 Vacant(VacantEntry<'a, K, V>),
326}
327
328impl<'a, K, V> Entry<'a, K, V>
329where
330 K: Hash + Ord,
331{
332 #[inline]
335 pub fn or_insert(self, default: V) -> &'a mut V {
336 match self {
337 Self::Occupied(entry) => entry.into_mut(),
338 Self::Vacant(entry) => entry.insert(default),
339 }
340 }
341
342 #[inline]
345 pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
346 match self {
347 Self::Occupied(entry) => entry.into_mut(),
348 Self::Vacant(entry) => entry.insert(default()),
349 }
350 }
351
352 #[inline]
359 pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
360 match self {
361 Self::Occupied(entry) => entry.into_mut(),
362 Self::Vacant(entry) => {
363 let value = default(entry.key());
364 entry.insert(value)
365 }
366 }
367 }
368
369 #[inline]
371 pub fn key(&self) -> &K {
372 match *self {
373 Self::Occupied(ref entry) => entry.key(),
374 Self::Vacant(ref entry) => entry.key(),
375 }
376 }
377
378 #[inline]
381 pub fn and_modify<F>(self, f: F) -> Self
382 where
383 F: FnOnce(&mut V),
384 {
385 match self {
386 Self::Occupied(mut entry) => {
387 f(entry.get_mut());
388 Self::Occupied(entry)
389 }
390 Self::Vacant(entry) => Self::Vacant(entry),
391 }
392 }
393}
394
395impl<'a, K, V> Entry<'a, K, V>
396where
397 K: Hash + Ord,
398 V: Default,
399{
400 #[inline]
403 pub fn or_default(self) -> &'a mut V {
404 match self {
405 Self::Occupied(entry) => entry.into_mut(),
406 Self::Vacant(entry) => entry.insert(Default::default()),
407 }
408 }
409}
410
411pub struct OccupiedEntry<'a, K, V> {
415 inner: detail::OccupiedEntryImpl<'a, K, V>,
416}
417
418impl<'a, K, V> Debug for OccupiedEntry<'a, K, V>
419where
420 K: Debug + Ord + 'a,
421 V: Debug + 'a,
422{
423 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
424 self.inner.fmt(f)
425 }
426}
427
428impl<'a, K, V> OccupiedEntry<'a, K, V>
429where
430 K: Ord + 'a,
431 V: 'a,
432{
433 #[inline]
435 pub fn key(&self) -> &K {
436 self.inner.key()
437 }
438
439 #[inline]
441 pub fn get(&self) -> &V {
442 self.inner.get()
443 }
444
445 #[inline]
447 pub fn get_mut(&mut self) -> &mut V {
448 self.inner.get_mut()
449 }
450
451 #[inline]
453 pub fn insert(&mut self, value: V) -> V {
454 self.inner.insert(value)
455 }
456
457 #[inline]
460 pub fn into_mut(self) -> &'a mut V {
461 self.inner.into_mut()
462 }
463
464 #[inline]
466 pub fn remove_entry(self) -> (K, V) {
467 self.inner.remove_entry()
468 }
469
470 #[inline]
472 pub fn remove(self) -> V {
473 self.inner.remove()
474 }
475}
476
477pub struct VacantEntry<'a, K, V> {
481 inner: detail::VacantEntryImpl<'a, K, V>,
482}
483
484impl<'a, K, V> Debug for VacantEntry<'a, K, V>
485where
486 K: Debug + Ord + 'a,
487 V: Debug + 'a,
488{
489 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
490 self.inner.fmt(f)
491 }
492}
493
494impl<'a, K, V> VacantEntry<'a, K, V>
495where
496 K: Ord + 'a,
497 V: 'a,
498{
499 #[inline]
501 pub fn key(&self) -> &K {
502 self.inner.key()
503 }
504
505 #[inline]
507 pub fn into_key(self) -> K {
508 self.inner.into_key()
509 }
510
511 #[inline]
513 pub fn insert(self, value: V) -> &'a mut V
514 where
515 K: Hash,
516 {
517 self.inner.insert(value)
518 }
519}
520
521impl<K, V> FromIterator<(K, V)> for Map<K, V>
522where
523 K: Hash + Eq + Ord,
524{
525 #[inline]
526 fn from_iter<I>(iter: I) -> Self
527 where
528 I: IntoIterator<Item = (K, V)>,
529 {
530 Self {
531 inner: <detail::MapImpl<K, V>>::from_iter(iter),
532 }
533 }
534}
535
536impl<'a, K, V> IntoIterator for &'a Map<K, V> {
537 type Item = (&'a K, &'a V);
538 type IntoIter = Iter<'a, K, V>;
539
540 #[inline]
541 fn into_iter(self) -> Self::IntoIter {
542 self.iter()
543 }
544}
545
546#[derive(Debug, Clone)]
548pub struct Iter<'a, K, V> {
549 inner: detail::IterImpl<'a, K, V>,
550}
551
552impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
553 type Item = (&'a K, &'a V);
554
555 #[inline]
556 fn size_hint(&self) -> (usize, Option<usize>) {
557 self.inner.size_hint()
558 }
559
560 #[inline]
561 fn next(&mut self) -> Option<Self::Item> {
562 self.inner.next()
563 }
564}
565
566impl<'a, K: 'a, V: 'a> ExactSizeIterator for Iter<'a, K, V> {
567 #[inline]
568 fn len(&self) -> usize {
569 self.inner.len()
570 }
571}
572
573impl<'a, K: 'a, V: 'a> FusedIterator for Iter<'a, K, V> where
574 detail::IterImpl<'a, K, V>: FusedIterator
575{
576}
577
578impl<'a, K: 'a, V: 'a> IntoIterator for &'a mut Map<K, V> {
579 type Item = (&'a K, &'a mut V);
580 type IntoIter = IterMut<'a, K, V>;
581
582 #[inline]
583 fn into_iter(self) -> Self::IntoIter {
584 self.iter_mut()
585 }
586}
587
588#[derive(Debug)]
590pub struct IterMut<'a, K, V> {
591 inner: detail::IterMutImpl<'a, K, V>,
592}
593
594impl<'a, K: 'a, V: 'a> Iterator for IterMut<'a, K, V> {
595 type Item = (&'a K, &'a mut V);
596
597 #[inline]
598 fn size_hint(&self) -> (usize, Option<usize>) {
599 self.inner.size_hint()
600 }
601
602 #[inline]
603 fn next(&mut self) -> Option<Self::Item> {
604 self.inner.next()
605 }
606}
607
608impl<'a, K: 'a, V: 'a> ExactSizeIterator for IterMut<'a, K, V> {
609 #[inline]
610 fn len(&self) -> usize {
611 self.inner.len()
612 }
613}
614
615impl<'a, K: 'a, V: 'a> FusedIterator for IterMut<'a, K, V> where
616 detail::IterMutImpl<'a, K, V>: FusedIterator
617{
618}
619
620impl<K, V> IntoIterator for Map<K, V> {
621 type Item = (K, V);
622 type IntoIter = IntoIter<K, V>;
623
624 #[inline]
625 fn into_iter(self) -> Self::IntoIter {
626 IntoIter {
627 inner: self.inner.into_iter(),
628 }
629 }
630}
631
632#[derive(Debug)]
634pub struct IntoIter<K, V> {
635 inner: detail::IntoIterImpl<K, V>,
636}
637
638impl<K, V> Iterator for IntoIter<K, V> {
639 type Item = (K, V);
640
641 #[inline]
642 fn size_hint(&self) -> (usize, Option<usize>) {
643 self.inner.size_hint()
644 }
645
646 #[inline]
647 fn next(&mut self) -> Option<Self::Item> {
648 self.inner.next()
649 }
650}
651
652impl<K, V> ExactSizeIterator for IntoIter<K, V> {
653 #[inline]
654 fn len(&self) -> usize {
655 self.inner.len()
656 }
657}
658
659impl<K, V> FusedIterator for IntoIter<K, V> where detail::IntoIterImpl<K, V>: FusedIterator {}
660
661#[derive(Debug, Clone)]
663pub struct Keys<'a, K, V> {
664 inner: detail::KeysImpl<'a, K, V>,
665}
666
667impl<'a, K: 'a, V> Iterator for Keys<'a, K, V> {
668 type Item = &'a K;
669
670 #[inline]
671 fn size_hint(&self) -> (usize, Option<usize>) {
672 self.inner.size_hint()
673 }
674
675 #[inline]
676 fn next(&mut self) -> Option<Self::Item> {
677 self.inner.next()
678 }
679}
680
681impl<'a, K: 'a, V> ExactSizeIterator for Keys<'a, K, V> {
682 #[inline]
683 fn len(&self) -> usize {
684 self.inner.len()
685 }
686}
687
688impl<'a, K: 'a, V> FusedIterator for Keys<'a, K, V> where detail::KeysImpl<'a, K, V>: FusedIterator {}
689
690#[derive(Debug, Clone)]
692pub struct Values<'a, K, V> {
693 inner: detail::ValuesImpl<'a, K, V>,
694}
695
696impl<'a, K, V: 'a> Iterator for Values<'a, K, V> {
697 type Item = &'a V;
698
699 #[inline]
700 fn size_hint(&self) -> (usize, Option<usize>) {
701 self.inner.size_hint()
702 }
703
704 #[inline]
705 fn next(&mut self) -> Option<Self::Item> {
706 self.inner.next()
707 }
708}
709
710impl<'a, K, V: 'a> ExactSizeIterator for Values<'a, K, V> {
711 #[inline]
712 fn len(&self) -> usize {
713 self.inner.len()
714 }
715}
716
717impl<'a, K, V: 'a> FusedIterator for Values<'a, K, V> where
718 detail::ValuesImpl<'a, K, V>: FusedIterator
719{
720}
721
722#[derive(Debug)]
724pub struct ValuesMut<'a, K, V> {
725 inner: detail::ValuesMutImpl<'a, K, V>,
726}
727
728impl<'a, K, V: 'a> Iterator for ValuesMut<'a, K, V> {
729 type Item = &'a mut V;
730
731 #[inline]
732 fn size_hint(&self) -> (usize, Option<usize>) {
733 self.inner.size_hint()
734 }
735
736 #[inline]
737 fn next(&mut self) -> Option<Self::Item> {
738 self.inner.next()
739 }
740}
741
742impl<'a, K, V: 'a> ExactSizeIterator for ValuesMut<'a, K, V> {
743 #[inline]
744 fn len(&self) -> usize {
745 self.inner.len()
746 }
747}
748
749impl<'a, K, V: 'a> FusedIterator for ValuesMut<'a, K, V> where
750 detail::ValuesMutImpl<'a, K, V>: FusedIterator
751{
752}
753
754#[derive(Debug)]
756pub struct IntoKeys<K, V> {
757 inner: detail::IntoKeysImpl<K, V>,
758}
759
760impl<K, V> Iterator for IntoKeys<K, V> {
761 type Item = K;
762
763 #[inline]
764 fn size_hint(&self) -> (usize, Option<usize>) {
765 self.inner.size_hint()
766 }
767
768 #[inline]
769 fn next(&mut self) -> Option<Self::Item> {
770 self.inner.next()
771 }
772}
773
774impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
775 #[inline]
776 fn len(&self) -> usize {
777 self.inner.len()
778 }
779}
780
781impl<K, V> FusedIterator for IntoKeys<K, V> where detail::IntoKeysImpl<K, V>: FusedIterator {}
782
783#[derive(Debug)]
785pub struct IntoValues<K, V> {
786 inner: detail::IntoValuesImpl<K, V>,
787}
788
789impl<K, V> Iterator for IntoValues<K, V> {
790 type Item = V;
791
792 #[inline]
793 fn size_hint(&self) -> (usize, Option<usize>) {
794 self.inner.size_hint()
795 }
796
797 #[inline]
798 fn next(&mut self) -> Option<Self::Item> {
799 self.inner.next()
800 }
801}
802
803impl<K, V> ExactSizeIterator for IntoValues<K, V> {
804 #[inline]
805 fn len(&self) -> usize {
806 self.inner.len()
807 }
808}
809
810impl<K, V> FusedIterator for IntoValues<K, V> where detail::IntoValuesImpl<K, V>: FusedIterator {}
811
812#[cfg(feature = "serde")]
813impl<K, V> serde::Serialize for Map<K, V>
814where
815 K: serde::Serialize + Eq + Hash + Ord,
816 V: serde::Serialize,
817{
818 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
819 where
820 S: serde::ser::Serializer,
821 {
822 serde::Serialize::serialize(&self.inner, serializer)
823 }
824}
825
826#[cfg(feature = "serde")]
827impl<'a, K, V> serde::Deserialize<'a> for Map<K, V>
828where
829 K: serde::Deserialize<'a> + Eq + Hash + Ord,
830 V: serde::Deserialize<'a>,
831{
832 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
833 where
834 D: serde::de::Deserializer<'a>,
835 {
836 Ok(Map {
837 inner: serde::Deserialize::deserialize(deserializer)?,
838 })
839 }
840}