1 | //! An immutable map constructed at compile time. |
2 | use core::fmt; |
3 | use core::iter::FusedIterator; |
4 | use core::iter::IntoIterator; |
5 | use core::ops::Index; |
6 | use core::slice; |
7 | use phf_shared::{self, HashKey, PhfBorrow, PhfHash}; |
8 | #[cfg (feature = "serde" )] |
9 | use serde::ser::{Serialize, SerializeMap, Serializer}; |
10 | |
11 | /// An immutable map constructed at compile time. |
12 | /// |
13 | /// ## Note |
14 | /// |
15 | /// The fields of this struct are public so that they may be initialized by the |
16 | /// `phf_map!` macro and code generation. They are subject to change at any |
17 | /// time and should never be accessed directly. |
18 | pub struct Map<K: 'static, V: 'static> { |
19 | #[doc (hidden)] |
20 | pub key: HashKey, |
21 | #[doc (hidden)] |
22 | pub disps: &'static [(u32, u32)], |
23 | #[doc (hidden)] |
24 | pub entries: &'static [(K, V)], |
25 | } |
26 | |
27 | impl<K, V> fmt::Debug for Map<K, V> |
28 | where |
29 | K: fmt::Debug, |
30 | V: fmt::Debug, |
31 | { |
32 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
33 | fmt.debug_map().entries(self.entries()).finish() |
34 | } |
35 | } |
36 | |
37 | impl<'a, K, V, T: ?Sized> Index<&'a T> for Map<K, V> |
38 | where |
39 | T: Eq + PhfHash, |
40 | K: PhfBorrow<T>, |
41 | { |
42 | type Output = V; |
43 | |
44 | fn index(&self, k: &'a T) -> &V { |
45 | self.get(k).expect(msg:"invalid key" ) |
46 | } |
47 | } |
48 | |
49 | impl<K, V> Map<K, V> { |
50 | /// Returns the number of entries in the `Map`. |
51 | #[inline ] |
52 | pub const fn len(&self) -> usize { |
53 | self.entries.len() |
54 | } |
55 | |
56 | /// Returns true if the `Map` is empty. |
57 | #[inline ] |
58 | pub const fn is_empty(&self) -> bool { |
59 | self.len() == 0 |
60 | } |
61 | |
62 | /// Determines if `key` is in the `Map`. |
63 | pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool |
64 | where |
65 | T: Eq + PhfHash, |
66 | K: PhfBorrow<T>, |
67 | { |
68 | self.get(key).is_some() |
69 | } |
70 | |
71 | /// Returns a reference to the value that `key` maps to. |
72 | pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V> |
73 | where |
74 | T: Eq + PhfHash, |
75 | K: PhfBorrow<T>, |
76 | { |
77 | self.get_entry(key).map(|e| e.1) |
78 | } |
79 | |
80 | /// Returns a reference to the map's internal static instance of the given |
81 | /// key. |
82 | /// |
83 | /// This can be useful for interning schemes. |
84 | pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K> |
85 | where |
86 | T: Eq + PhfHash, |
87 | K: PhfBorrow<T>, |
88 | { |
89 | self.get_entry(key).map(|e| e.0) |
90 | } |
91 | |
92 | /// Like `get`, but returns both the key and the value. |
93 | pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)> |
94 | where |
95 | T: Eq + PhfHash, |
96 | K: PhfBorrow<T>, |
97 | { |
98 | if self.disps.is_empty() { |
99 | return None; |
100 | } //Prevent panic on empty map |
101 | let hashes = phf_shared::hash(key, &self.key); |
102 | let index = phf_shared::get_index(&hashes, &*self.disps, self.entries.len()); |
103 | let entry = &self.entries[index as usize]; |
104 | let b: &T = entry.0.borrow(); |
105 | if b == key { |
106 | Some((&entry.0, &entry.1)) |
107 | } else { |
108 | None |
109 | } |
110 | } |
111 | |
112 | /// Returns an iterator over the key/value pairs in the map. |
113 | /// |
114 | /// Entries are returned in an arbitrary but fixed order. |
115 | pub fn entries(&self) -> Entries<'_, K, V> { |
116 | Entries { |
117 | iter: self.entries.iter(), |
118 | } |
119 | } |
120 | |
121 | /// Returns an iterator over the keys in the map. |
122 | /// |
123 | /// Keys are returned in an arbitrary but fixed order. |
124 | pub fn keys(&self) -> Keys<'_, K, V> { |
125 | Keys { |
126 | iter: self.entries(), |
127 | } |
128 | } |
129 | |
130 | /// Returns an iterator over the values in the map. |
131 | /// |
132 | /// Values are returned in an arbitrary but fixed order. |
133 | pub fn values(&self) -> Values<'_, K, V> { |
134 | Values { |
135 | iter: self.entries(), |
136 | } |
137 | } |
138 | } |
139 | |
140 | impl<'a, K, V> IntoIterator for &'a Map<K, V> { |
141 | type Item = (&'a K, &'a V); |
142 | type IntoIter = Entries<'a, K, V>; |
143 | |
144 | fn into_iter(self) -> Entries<'a, K, V> { |
145 | self.entries() |
146 | } |
147 | } |
148 | |
149 | /// An iterator over the key/value pairs in a `Map`. |
150 | pub struct Entries<'a, K, V> { |
151 | iter: slice::Iter<'a, (K, V)>, |
152 | } |
153 | |
154 | impl<'a, K, V> Clone for Entries<'a, K, V> { |
155 | #[inline ] |
156 | fn clone(&self) -> Self { |
157 | Self { |
158 | iter: self.iter.clone(), |
159 | } |
160 | } |
161 | } |
162 | |
163 | impl<'a, K, V> fmt::Debug for Entries<'a, K, V> |
164 | where |
165 | K: fmt::Debug, |
166 | V: fmt::Debug, |
167 | { |
168 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
169 | f.debug_list().entries(self.clone()).finish() |
170 | } |
171 | } |
172 | |
173 | impl<'a, K, V> Iterator for Entries<'a, K, V> { |
174 | type Item = (&'a K, &'a V); |
175 | |
176 | fn next(&mut self) -> Option<(&'a K, &'a V)> { |
177 | self.iter.next().map(|&(ref k: &K, ref v: &V)| (k, v)) |
178 | } |
179 | |
180 | fn size_hint(&self) -> (usize, Option<usize>) { |
181 | self.iter.size_hint() |
182 | } |
183 | } |
184 | |
185 | impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> { |
186 | fn next_back(&mut self) -> Option<(&'a K, &'a V)> { |
187 | self.iter.next_back().map(|e: &(K, V)| (&e.0, &e.1)) |
188 | } |
189 | } |
190 | |
191 | impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {} |
192 | |
193 | impl<'a, K, V> FusedIterator for Entries<'a, K, V> {} |
194 | |
195 | /// An iterator over the keys in a `Map`. |
196 | pub struct Keys<'a, K, V> { |
197 | iter: Entries<'a, K, V>, |
198 | } |
199 | |
200 | impl<'a, K, V> Clone for Keys<'a, K, V> { |
201 | #[inline ] |
202 | fn clone(&self) -> Self { |
203 | Self { |
204 | iter: self.iter.clone(), |
205 | } |
206 | } |
207 | } |
208 | |
209 | impl<'a, K, V> fmt::Debug for Keys<'a, K, V> |
210 | where |
211 | K: fmt::Debug, |
212 | { |
213 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
214 | f.debug_list().entries(self.clone()).finish() |
215 | } |
216 | } |
217 | |
218 | impl<'a, K, V> Iterator for Keys<'a, K, V> { |
219 | type Item = &'a K; |
220 | |
221 | fn next(&mut self) -> Option<&'a K> { |
222 | self.iter.next().map(|e: (&K, &V)| e.0) |
223 | } |
224 | |
225 | fn size_hint(&self) -> (usize, Option<usize>) { |
226 | self.iter.size_hint() |
227 | } |
228 | } |
229 | |
230 | impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> { |
231 | fn next_back(&mut self) -> Option<&'a K> { |
232 | self.iter.next_back().map(|e: (&K, &V)| e.0) |
233 | } |
234 | } |
235 | |
236 | impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {} |
237 | |
238 | impl<'a, K, V> FusedIterator for Keys<'a, K, V> {} |
239 | |
240 | /// An iterator over the values in a `Map`. |
241 | pub struct Values<'a, K, V> { |
242 | iter: Entries<'a, K, V>, |
243 | } |
244 | |
245 | impl<'a, K, V> Clone for Values<'a, K, V> { |
246 | #[inline ] |
247 | fn clone(&self) -> Self { |
248 | Self { |
249 | iter: self.iter.clone(), |
250 | } |
251 | } |
252 | } |
253 | |
254 | impl<'a, K, V> fmt::Debug for Values<'a, K, V> |
255 | where |
256 | V: fmt::Debug, |
257 | { |
258 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
259 | f.debug_list().entries(self.clone()).finish() |
260 | } |
261 | } |
262 | |
263 | impl<'a, K, V> Iterator for Values<'a, K, V> { |
264 | type Item = &'a V; |
265 | |
266 | fn next(&mut self) -> Option<&'a V> { |
267 | self.iter.next().map(|e: (&K, &V)| e.1) |
268 | } |
269 | |
270 | fn size_hint(&self) -> (usize, Option<usize>) { |
271 | self.iter.size_hint() |
272 | } |
273 | } |
274 | |
275 | impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> { |
276 | fn next_back(&mut self) -> Option<&'a V> { |
277 | self.iter.next_back().map(|e: (&K, &V)| e.1) |
278 | } |
279 | } |
280 | |
281 | impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {} |
282 | |
283 | impl<'a, K, V> FusedIterator for Values<'a, K, V> {} |
284 | |
285 | #[cfg (feature = "serde" )] |
286 | impl<K, V> Serialize for Map<K, V> |
287 | where |
288 | K: Serialize, |
289 | V: Serialize, |
290 | { |
291 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
292 | where |
293 | S: Serializer, |
294 | { |
295 | let mut map = serializer.serialize_map(Some(self.len()))?; |
296 | for (k, v) in self.entries() { |
297 | map.serialize_entry(k, v)?; |
298 | } |
299 | map.end() |
300 | } |
301 | } |
302 | |