defuse_map_utils/
hash_map.rs1use std::{
2 collections::hash_map::{self, HashMap},
3 hash::{BuildHasher, Hash},
4};
5
6use crate::iter::IterableMap;
7
8use super::{Entry, Map, OccupiedEntry, VacantEntry};
9
10impl<K, V, S> Map for HashMap<K, V, S>
11where
12 K: Eq + Hash,
13 S: BuildHasher,
14{
15 type K = K;
16
17 type V = V;
18
19 type VacantEntry<'a>
20 = hash_map::VacantEntry<'a, K, V>
21 where
22 Self: 'a;
23
24 type OccupiedEntry<'a>
25 = hash_map::OccupiedEntry<'a, K, V>
26 where
27 Self: 'a;
28
29 #[inline]
30 fn contains_key(&self, k: &Self::K) -> bool {
31 self.contains_key(k)
32 }
33
34 #[inline]
35 fn get(&self, k: &Self::K) -> Option<&Self::V> {
36 self.get(k)
37 }
38
39 #[inline]
40 fn get_mut(&mut self, k: &Self::K) -> Option<&mut Self::V> {
41 self.get_mut(k)
42 }
43
44 #[inline]
45 fn insert(&mut self, k: Self::K, v: Self::V) -> Option<Self::V> {
46 self.insert(k, v)
47 }
48
49 #[inline]
50 fn entry(&mut self, k: Self::K) -> Entry<Self::VacantEntry<'_>, Self::OccupiedEntry<'_>> {
51 match self.entry(k) {
52 hash_map::Entry::Occupied(entry) => Entry::Occupied(entry),
53 hash_map::Entry::Vacant(entry) => Entry::Vacant(entry),
54 }
55 }
56
57 #[inline]
58 fn remove(&mut self, k: &Self::K) -> Option<Self::V> {
59 self.remove(k)
60 }
61}
62
63impl<'a, K, V> VacantEntry<'a> for hash_map::VacantEntry<'a, K, V> {
64 type K = K;
65
66 type V = V;
67
68 #[inline]
69 fn key(&self) -> &Self::K {
70 self.key()
71 }
72
73 #[inline]
74 fn into_key(self) -> Self::K {
75 self.into_key()
76 }
77
78 #[inline]
79 fn insert(self, v: Self::V) -> &'a mut Self::V {
80 self.insert(v)
81 }
82}
83
84impl<'a, K, V> OccupiedEntry<'a> for hash_map::OccupiedEntry<'a, K, V> {
85 type K = K;
86
87 type V = V;
88
89 #[inline]
90 fn key(&self) -> &Self::K {
91 self.key()
92 }
93
94 #[inline]
95 fn get(&self) -> &Self::V {
96 self.get()
97 }
98
99 #[inline]
100 fn get_mut(&mut self) -> &mut Self::V {
101 self.get_mut()
102 }
103
104 #[inline]
105 fn into_mut(self) -> &'a mut Self::V {
106 self.into_mut()
107 }
108
109 #[inline]
110 fn insert(&mut self, v: Self::V) -> Self::V {
111 self.insert(v)
112 }
113
114 #[inline]
115 fn remove(self) -> Self::V {
116 self.remove()
117 }
118}
119
120impl<K, V, S> IterableMap for HashMap<K, V, S>
121where
122 K: Eq + Hash,
123 S: BuildHasher,
124{
125 type Keys<'a>
126 = hash_map::Keys<'a, K, V>
127 where
128 Self: 'a;
129
130 type Values<'a>
131 = hash_map::Values<'a, K, V>
132 where
133 Self: 'a;
134
135 type ValuesMut<'a>
136 = hash_map::ValuesMut<'a, K, V>
137 where
138 Self: 'a;
139
140 type Iter<'a>
141 = hash_map::Iter<'a, K, V>
142 where
143 Self: 'a;
144
145 type IterMut<'a>
146 = hash_map::IterMut<'a, K, V>
147 where
148 Self: 'a;
149
150 type Drain<'a>
151 = hash_map::Drain<'a, K, V>
152 where
153 Self: 'a;
154
155 #[inline]
156 fn len(&self) -> usize {
157 self.len()
158 }
159
160 #[inline]
161 fn is_empty(&self) -> bool {
162 self.is_empty()
163 }
164
165 #[inline]
166 fn keys(&self) -> Self::Keys<'_> {
167 self.keys()
168 }
169
170 #[inline]
171 fn values(&self) -> Self::Values<'_> {
172 self.values()
173 }
174
175 #[inline]
176 fn values_mut(&mut self) -> Self::ValuesMut<'_> {
177 self.values_mut()
178 }
179
180 #[inline]
181 fn iter(&self) -> Self::Iter<'_> {
182 self.iter()
183 }
184
185 #[inline]
186 fn iter_mut(&mut self) -> Self::IterMut<'_> {
187 self.iter_mut()
188 }
189
190 #[inline]
191 fn drain(&mut self) -> Self::Drain<'_> {
192 self.drain()
193 }
194
195 #[inline]
196 fn clear(&mut self) {
197 self.clear();
198 }
199}