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