/build/cargo-vendor-dir/serde_json-1.0.140/src/map.rs
Line | Count | Source |
1 | | //! A map of String to serde_json::Value. |
2 | | //! |
3 | | //! By default the map is backed by a [`BTreeMap`]. Enable the `preserve_order` |
4 | | //! feature of serde_json to use [`IndexMap`] instead. |
5 | | //! |
6 | | //! [`BTreeMap`]: std::collections::BTreeMap |
7 | | //! [`IndexMap`]: indexmap::IndexMap |
8 | | |
9 | | use crate::error::Error; |
10 | | use crate::value::Value; |
11 | | use alloc::string::String; |
12 | | #[cfg(feature = "preserve_order")] |
13 | | use alloc::vec::Vec; |
14 | | use core::borrow::Borrow; |
15 | | use core::fmt::{self, Debug}; |
16 | | use core::hash::{Hash, Hasher}; |
17 | | use core::iter::FusedIterator; |
18 | | #[cfg(feature = "preserve_order")] |
19 | | use core::mem; |
20 | | use core::ops; |
21 | | use serde::de; |
22 | | |
23 | | #[cfg(not(feature = "preserve_order"))] |
24 | | use alloc::collections::{btree_map, BTreeMap}; |
25 | | #[cfg(feature = "preserve_order")] |
26 | | use indexmap::IndexMap; |
27 | | |
28 | | /// Represents a JSON key/value type. |
29 | | pub struct Map<K, V> { |
30 | | map: MapImpl<K, V>, |
31 | | } |
32 | | |
33 | | #[cfg(not(feature = "preserve_order"))] |
34 | | type MapImpl<K, V> = BTreeMap<K, V>; |
35 | | #[cfg(feature = "preserve_order")] |
36 | | type MapImpl<K, V> = IndexMap<K, V>; |
37 | | |
38 | | impl Map<String, Value> { |
39 | | /// Makes a new empty Map. |
40 | | #[inline] |
41 | 0 | pub fn new() -> Self { |
42 | 0 | Map { |
43 | 0 | map: MapImpl::new(), |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | /// Makes a new empty Map with the given initial capacity. |
48 | | #[inline] |
49 | 0 | pub fn with_capacity(capacity: usize) -> Self { |
50 | 0 | Map { |
51 | 0 | #[cfg(not(feature = "preserve_order"))] |
52 | 0 | map: { |
53 | 0 | // does not support with_capacity |
54 | 0 | let _ = capacity; |
55 | 0 | BTreeMap::new() |
56 | 0 | }, |
57 | 0 | #[cfg(feature = "preserve_order")] |
58 | 0 | map: IndexMap::with_capacity(capacity), |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | /// Clears the map, removing all values. |
63 | | #[inline] |
64 | 0 | pub fn clear(&mut self) { |
65 | 0 | self.map.clear(); |
66 | 0 | } |
67 | | |
68 | | /// Returns a reference to the value corresponding to the key. |
69 | | /// |
70 | | /// The key may be any borrowed form of the map's key type, but the ordering |
71 | | /// on the borrowed form *must* match the ordering on the key type. |
72 | | #[inline] |
73 | 0 | pub fn get<Q>(&self, key: &Q) -> Option<&Value> |
74 | 0 | where |
75 | 0 | String: Borrow<Q>, |
76 | 0 | Q: ?Sized + Ord + Eq + Hash, |
77 | 0 | { |
78 | 0 | self.map.get(key) |
79 | 0 | } |
80 | | |
81 | | /// Returns true if the map contains a value for the specified key. |
82 | | /// |
83 | | /// The key may be any borrowed form of the map's key type, but the ordering |
84 | | /// on the borrowed form *must* match the ordering on the key type. |
85 | | #[inline] |
86 | 0 | pub fn contains_key<Q>(&self, key: &Q) -> bool |
87 | 0 | where |
88 | 0 | String: Borrow<Q>, |
89 | 0 | Q: ?Sized + Ord + Eq + Hash, |
90 | 0 | { |
91 | 0 | self.map.contains_key(key) |
92 | 0 | } |
93 | | |
94 | | /// Returns a mutable reference to the value corresponding to the key. |
95 | | /// |
96 | | /// The key may be any borrowed form of the map's key type, but the ordering |
97 | | /// on the borrowed form *must* match the ordering on the key type. |
98 | | #[inline] |
99 | 0 | pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value> |
100 | 0 | where |
101 | 0 | String: Borrow<Q>, |
102 | 0 | Q: ?Sized + Ord + Eq + Hash, |
103 | 0 | { |
104 | 0 | self.map.get_mut(key) |
105 | 0 | } |
106 | | |
107 | | /// Returns the key-value pair matching the given key. |
108 | | /// |
109 | | /// The key may be any borrowed form of the map's key type, but the ordering |
110 | | /// on the borrowed form *must* match the ordering on the key type. |
111 | | #[inline] |
112 | 0 | pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&String, &Value)> |
113 | 0 | where |
114 | 0 | String: Borrow<Q>, |
115 | 0 | Q: ?Sized + Ord + Eq + Hash, |
116 | 0 | { |
117 | 0 | self.map.get_key_value(key) |
118 | 0 | } |
119 | | |
120 | | /// Inserts a key-value pair into the map. |
121 | | /// |
122 | | /// If the map did not have this key present, `None` is returned. |
123 | | /// |
124 | | /// If the map did have this key present, the value is updated, and the old |
125 | | /// value is returned. |
126 | | #[inline] |
127 | 0 | pub fn insert(&mut self, k: String, v: Value) -> Option<Value> { |
128 | 0 | self.map.insert(k, v) |
129 | 0 | } |
130 | | |
131 | | /// Insert a key-value pair in the map at the given index. |
132 | | /// |
133 | | /// If the map did not have this key present, `None` is returned. |
134 | | /// |
135 | | /// If the map did have this key present, the key is moved to the new |
136 | | /// position, the value is updated, and the old value is returned. |
137 | | #[cfg(feature = "preserve_order")] |
138 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
139 | | #[inline] |
140 | | pub fn shift_insert(&mut self, index: usize, k: String, v: Value) -> Option<Value> { |
141 | | self.map.shift_insert(index, k, v) |
142 | | } |
143 | | |
144 | | /// Removes a key from the map, returning the value at the key if the key |
145 | | /// was previously in the map. |
146 | | /// |
147 | | /// The key may be any borrowed form of the map's key type, but the ordering |
148 | | /// on the borrowed form *must* match the ordering on the key type. |
149 | | /// |
150 | | /// If serde_json's "preserve_order" is enabled, `.remove(key)` is |
151 | | /// equivalent to [`.swap_remove(key)`][Self::swap_remove], replacing this |
152 | | /// entry's position with the last element. If you need to preserve the |
153 | | /// relative order of the keys in the map, use |
154 | | /// [`.shift_remove(key)`][Self::shift_remove] instead. |
155 | | #[inline] |
156 | 0 | pub fn remove<Q>(&mut self, key: &Q) -> Option<Value> |
157 | 0 | where |
158 | 0 | String: Borrow<Q>, |
159 | 0 | Q: ?Sized + Ord + Eq + Hash, |
160 | 0 | { |
161 | 0 | #[cfg(feature = "preserve_order")] |
162 | 0 | return self.swap_remove(key); |
163 | 0 | #[cfg(not(feature = "preserve_order"))] |
164 | 0 | return self.map.remove(key); |
165 | 0 | } |
166 | | |
167 | | /// Removes a key from the map, returning the stored key and value if the |
168 | | /// key was previously in the map. |
169 | | /// |
170 | | /// The key may be any borrowed form of the map's key type, but the ordering |
171 | | /// on the borrowed form *must* match the ordering on the key type. |
172 | | /// |
173 | | /// If serde_json's "preserve_order" is enabled, `.remove_entry(key)` is |
174 | | /// equivalent to [`.swap_remove_entry(key)`][Self::swap_remove_entry], |
175 | | /// replacing this entry's position with the last element. If you need to |
176 | | /// preserve the relative order of the keys in the map, use |
177 | | /// [`.shift_remove_entry(key)`][Self::shift_remove_entry] instead. |
178 | | #[inline] |
179 | 0 | pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)> |
180 | 0 | where |
181 | 0 | String: Borrow<Q>, |
182 | 0 | Q: ?Sized + Ord + Eq + Hash, |
183 | 0 | { |
184 | 0 | #[cfg(feature = "preserve_order")] |
185 | 0 | return self.swap_remove_entry(key); |
186 | 0 | #[cfg(not(feature = "preserve_order"))] |
187 | 0 | return self.map.remove_entry(key); |
188 | 0 | } |
189 | | |
190 | | /// Removes and returns the value corresponding to the key from the map. |
191 | | /// |
192 | | /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the |
193 | | /// last element of the map and popping it off. This perturbs the position |
194 | | /// of what used to be the last element! |
195 | | /// |
196 | | /// [`Vec::swap_remove`]: std::vec::Vec::swap_remove |
197 | | #[cfg(feature = "preserve_order")] |
198 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
199 | | #[inline] |
200 | | pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<Value> |
201 | | where |
202 | | String: Borrow<Q>, |
203 | | Q: ?Sized + Ord + Eq + Hash, |
204 | | { |
205 | | self.map.swap_remove(key) |
206 | | } |
207 | | |
208 | | /// Remove and return the key-value pair. |
209 | | /// |
210 | | /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the |
211 | | /// last element of the map and popping it off. This perturbs the position |
212 | | /// of what used to be the last element! |
213 | | /// |
214 | | /// [`Vec::swap_remove`]: std::vec::Vec::swap_remove |
215 | | #[cfg(feature = "preserve_order")] |
216 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
217 | | #[inline] |
218 | | pub fn swap_remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)> |
219 | | where |
220 | | String: Borrow<Q>, |
221 | | Q: ?Sized + Ord + Eq + Hash, |
222 | | { |
223 | | self.map.swap_remove_entry(key) |
224 | | } |
225 | | |
226 | | /// Removes and returns the value corresponding to the key from the map. |
227 | | /// |
228 | | /// Like [`Vec::remove`], the entry is removed by shifting all of the |
229 | | /// elements that follow it, preserving their relative order. This perturbs |
230 | | /// the index of all of those elements! |
231 | | /// |
232 | | /// [`Vec::remove`]: std::vec::Vec::remove |
233 | | #[cfg(feature = "preserve_order")] |
234 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
235 | | #[inline] |
236 | | pub fn shift_remove<Q>(&mut self, key: &Q) -> Option<Value> |
237 | | where |
238 | | String: Borrow<Q>, |
239 | | Q: ?Sized + Ord + Eq + Hash, |
240 | | { |
241 | | self.map.shift_remove(key) |
242 | | } |
243 | | |
244 | | /// Remove and return the key-value pair. |
245 | | /// |
246 | | /// Like [`Vec::remove`], the entry is removed by shifting all of the |
247 | | /// elements that follow it, preserving their relative order. This perturbs |
248 | | /// the index of all of those elements! |
249 | | /// |
250 | | /// [`Vec::remove`]: std::vec::Vec::remove |
251 | | #[cfg(feature = "preserve_order")] |
252 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
253 | | #[inline] |
254 | | pub fn shift_remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)> |
255 | | where |
256 | | String: Borrow<Q>, |
257 | | Q: ?Sized + Ord + Eq + Hash, |
258 | | { |
259 | | self.map.shift_remove_entry(key) |
260 | | } |
261 | | |
262 | | /// Moves all elements from other into self, leaving other empty. |
263 | | #[inline] |
264 | 0 | pub fn append(&mut self, other: &mut Self) { |
265 | 0 | #[cfg(feature = "preserve_order")] |
266 | 0 | self.map |
267 | 0 | .extend(mem::replace(&mut other.map, MapImpl::default())); |
268 | 0 | #[cfg(not(feature = "preserve_order"))] |
269 | 0 | self.map.append(&mut other.map); |
270 | 0 | } |
271 | | |
272 | | /// Gets the given key's corresponding entry in the map for in-place |
273 | | /// manipulation. |
274 | 0 | pub fn entry<S>(&mut self, key: S) -> Entry |
275 | 0 | where |
276 | 0 | S: Into<String>, |
277 | 0 | { |
278 | | #[cfg(not(feature = "preserve_order"))] |
279 | | use alloc::collections::btree_map::Entry as EntryImpl; |
280 | | #[cfg(feature = "preserve_order")] |
281 | | use indexmap::map::Entry as EntryImpl; |
282 | | |
283 | 0 | match self.map.entry(key.into()) { |
284 | 0 | EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }), |
285 | 0 | EntryImpl::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied }), |
286 | | } |
287 | 0 | } |
288 | | |
289 | | /// Returns the number of elements in the map. |
290 | | #[inline] |
291 | 0 | pub fn len(&self) -> usize { |
292 | 0 | self.map.len() |
293 | 0 | } |
294 | | |
295 | | /// Returns true if the map contains no elements. |
296 | | #[inline] |
297 | 0 | pub fn is_empty(&self) -> bool { |
298 | 0 | self.map.is_empty() |
299 | 0 | } |
300 | | |
301 | | /// Gets an iterator over the entries of the map. |
302 | | #[inline] |
303 | 0 | pub fn iter(&self) -> Iter { |
304 | 0 | Iter { |
305 | 0 | iter: self.map.iter(), |
306 | 0 | } |
307 | 0 | } |
308 | | |
309 | | /// Gets a mutable iterator over the entries of the map. |
310 | | #[inline] |
311 | 0 | pub fn iter_mut(&mut self) -> IterMut { |
312 | 0 | IterMut { |
313 | 0 | iter: self.map.iter_mut(), |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | | /// Gets an iterator over the keys of the map. |
318 | | #[inline] |
319 | 0 | pub fn keys(&self) -> Keys { |
320 | 0 | Keys { |
321 | 0 | iter: self.map.keys(), |
322 | 0 | } |
323 | 0 | } |
324 | | |
325 | | /// Gets an iterator over the values of the map. |
326 | | #[inline] |
327 | 0 | pub fn values(&self) -> Values { |
328 | 0 | Values { |
329 | 0 | iter: self.map.values(), |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | | /// Gets an iterator over mutable values of the map. |
334 | | #[inline] |
335 | 0 | pub fn values_mut(&mut self) -> ValuesMut { |
336 | 0 | ValuesMut { |
337 | 0 | iter: self.map.values_mut(), |
338 | 0 | } |
339 | 0 | } |
340 | | |
341 | | /// Gets an iterator over the values of the map. |
342 | | #[inline] |
343 | 0 | pub fn into_values(self) -> IntoValues { |
344 | 0 | IntoValues { |
345 | 0 | iter: self.map.into_values(), |
346 | 0 | } |
347 | 0 | } |
348 | | |
349 | | /// Retains only the elements specified by the predicate. |
350 | | /// |
351 | | /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` |
352 | | /// returns `false`. |
353 | | #[inline] |
354 | 0 | pub fn retain<F>(&mut self, f: F) |
355 | 0 | where |
356 | 0 | F: FnMut(&String, &mut Value) -> bool, |
357 | 0 | { |
358 | 0 | self.map.retain(f); |
359 | 0 | } |
360 | | |
361 | | /// Sorts this map's entries in-place using `str`'s usual ordering. |
362 | | /// |
363 | | /// If serde_json's "preserve_order" feature is not enabled, this method |
364 | | /// does no work because all JSON maps are always kept in a sorted state. |
365 | | /// |
366 | | /// If serde_json's "preserve_order" feature is enabled, this method |
367 | | /// destroys the original source order or insertion order of this map in |
368 | | /// favor of an alphanumerical order that matches how a BTreeMap with the |
369 | | /// same contents would be ordered. This takes **O(n log n + c)** time where |
370 | | /// _n_ is the length of the map and _c_ is the capacity. |
371 | | /// |
372 | | /// Other maps nested within the values of this map are not sorted. If you |
373 | | /// need the entire data structure to be sorted at all levels, you must also |
374 | | /// call |
375 | | /// <code>map.[values_mut]\().for_each([Value::sort_all_objects])</code>. |
376 | | /// |
377 | | /// [values_mut]: Map::values_mut |
378 | | #[inline] |
379 | 0 | pub fn sort_keys(&mut self) { |
380 | 0 | #[cfg(feature = "preserve_order")] |
381 | 0 | self.map.sort_unstable_keys(); |
382 | 0 | } |
383 | | } |
384 | | |
385 | | #[allow(clippy::derivable_impls)] // clippy bug: https://github.com/rust-lang/rust-clippy/issues/7655 |
386 | | impl Default for Map<String, Value> { |
387 | | #[inline] |
388 | 0 | fn default() -> Self { |
389 | 0 | Map { |
390 | 0 | map: MapImpl::new(), |
391 | 0 | } |
392 | 0 | } |
393 | | } |
394 | | |
395 | | impl Clone for Map<String, Value> { |
396 | | #[inline] |
397 | 0 | fn clone(&self) -> Self { |
398 | 0 | Map { |
399 | 0 | map: self.map.clone(), |
400 | 0 | } |
401 | 0 | } |
402 | | |
403 | | #[inline] |
404 | 0 | fn clone_from(&mut self, source: &Self) { |
405 | 0 | self.map.clone_from(&source.map); |
406 | 0 | } |
407 | | } |
408 | | |
409 | | impl PartialEq for Map<String, Value> { |
410 | | #[inline] |
411 | 0 | fn eq(&self, other: &Self) -> bool { |
412 | 0 | self.map.eq(&other.map) |
413 | 0 | } |
414 | | } |
415 | | |
416 | | impl Eq for Map<String, Value> {} |
417 | | |
418 | | impl Hash for Map<String, Value> { |
419 | 0 | fn hash<H: Hasher>(&self, state: &mut H) { |
420 | 0 | #[cfg(not(feature = "preserve_order"))] |
421 | 0 | { |
422 | 0 | self.map.hash(state); |
423 | 0 | } |
424 | 0 |
|
425 | 0 | #[cfg(feature = "preserve_order")] |
426 | 0 | { |
427 | 0 | let mut kv = Vec::from_iter(&self.map); |
428 | 0 | kv.sort_unstable_by(|a, b| a.0.cmp(b.0)); |
429 | 0 | kv.hash(state); |
430 | 0 | } |
431 | 0 | } |
432 | | } |
433 | | |
434 | | /// Access an element of this map. Panics if the given key is not present in the |
435 | | /// map. |
436 | | /// |
437 | | /// ``` |
438 | | /// # use serde_json::Value; |
439 | | /// # |
440 | | /// # let val = &Value::String("".to_owned()); |
441 | | /// # let _ = |
442 | | /// match val { |
443 | | /// Value::String(s) => Some(s.as_str()), |
444 | | /// Value::Array(arr) => arr[0].as_str(), |
445 | | /// Value::Object(map) => map["type"].as_str(), |
446 | | /// _ => None, |
447 | | /// } |
448 | | /// # ; |
449 | | /// ``` |
450 | | impl<Q> ops::Index<&Q> for Map<String, Value> |
451 | | where |
452 | | String: Borrow<Q>, |
453 | | Q: ?Sized + Ord + Eq + Hash, |
454 | | { |
455 | | type Output = Value; |
456 | | |
457 | 0 | fn index(&self, index: &Q) -> &Value { |
458 | 0 | self.map.index(index) |
459 | 0 | } |
460 | | } |
461 | | |
462 | | /// Mutably access an element of this map. Panics if the given key is not |
463 | | /// present in the map. |
464 | | /// |
465 | | /// ``` |
466 | | /// # use serde_json::json; |
467 | | /// # |
468 | | /// # let mut map = serde_json::Map::new(); |
469 | | /// # map.insert("key".to_owned(), serde_json::Value::Null); |
470 | | /// # |
471 | | /// map["key"] = json!("value"); |
472 | | /// ``` |
473 | | impl<Q> ops::IndexMut<&Q> for Map<String, Value> |
474 | | where |
475 | | String: Borrow<Q>, |
476 | | Q: ?Sized + Ord + Eq + Hash, |
477 | | { |
478 | 0 | fn index_mut(&mut self, index: &Q) -> &mut Value { |
479 | 0 | self.map.get_mut(index).expect("no entry found for key") |
480 | 0 | } |
481 | | } |
482 | | |
483 | | impl Debug for Map<String, Value> { |
484 | | #[inline] |
485 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
486 | 0 | self.map.fmt(formatter) |
487 | 0 | } |
488 | | } |
489 | | |
490 | | #[cfg(any(feature = "std", feature = "alloc"))] |
491 | | impl serde::ser::Serialize for Map<String, Value> { |
492 | | #[inline] |
493 | 0 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
494 | 0 | where |
495 | 0 | S: serde::ser::Serializer, |
496 | 0 | { |
497 | | use serde::ser::SerializeMap; |
498 | 0 | let mut map = tri!(serializer.serialize_map(Some(self.len()))); |
499 | 0 | for (k, v) in self { |
500 | 0 | tri!(map.serialize_entry(k, v)); |
501 | | } |
502 | 0 | map.end() |
503 | 0 | } |
504 | | } |
505 | | |
506 | | impl<'de> de::Deserialize<'de> for Map<String, Value> { |
507 | | #[inline] |
508 | 0 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
509 | 0 | where |
510 | 0 | D: de::Deserializer<'de>, |
511 | 0 | { |
512 | | struct Visitor; |
513 | | |
514 | | impl<'de> de::Visitor<'de> for Visitor { |
515 | | type Value = Map<String, Value>; |
516 | | |
517 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
518 | 0 | formatter.write_str("a map") |
519 | 0 | } |
520 | | |
521 | | #[inline] |
522 | 0 | fn visit_unit<E>(self) -> Result<Self::Value, E> |
523 | 0 | where |
524 | 0 | E: de::Error, |
525 | 0 | { |
526 | 0 | Ok(Map::new()) |
527 | 0 | } |
528 | | |
529 | | #[cfg(any(feature = "std", feature = "alloc"))] |
530 | | #[inline] |
531 | 0 | fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> |
532 | 0 | where |
533 | 0 | V: de::MapAccess<'de>, |
534 | 0 | { |
535 | 0 | let mut values = Map::new(); |
536 | | |
537 | 0 | while let Some((key, value)) = tri!(visitor.next_entry()) { |
538 | 0 | values.insert(key, value); |
539 | 0 | } |
540 | | |
541 | 0 | Ok(values) |
542 | 0 | } |
543 | | } |
544 | | |
545 | 0 | deserializer.deserialize_map(Visitor) |
546 | 0 | } |
547 | | } |
548 | | |
549 | | impl FromIterator<(String, Value)> for Map<String, Value> { |
550 | 0 | fn from_iter<T>(iter: T) -> Self |
551 | 0 | where |
552 | 0 | T: IntoIterator<Item = (String, Value)>, |
553 | 0 | { |
554 | 0 | Map { |
555 | 0 | map: FromIterator::from_iter(iter), |
556 | 0 | } |
557 | 0 | } |
558 | | } |
559 | | |
560 | | impl Extend<(String, Value)> for Map<String, Value> { |
561 | 0 | fn extend<T>(&mut self, iter: T) |
562 | 0 | where |
563 | 0 | T: IntoIterator<Item = (String, Value)>, |
564 | 0 | { |
565 | 0 | self.map.extend(iter); |
566 | 0 | } |
567 | | } |
568 | | |
569 | | macro_rules! delegate_iterator { |
570 | | (($name:ident $($generics:tt)*) => $item:ty) => { |
571 | | impl $($generics)* Iterator for $name $($generics)* { |
572 | | type Item = $item; |
573 | | #[inline] |
574 | 0 | fn next(&mut self) -> Option<Self::Item> { |
575 | 0 | self.iter.next() |
576 | 0 | } |
577 | | #[inline] |
578 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
579 | 0 | self.iter.size_hint() |
580 | 0 | } |
581 | | } |
582 | | |
583 | | impl $($generics)* DoubleEndedIterator for $name $($generics)* { |
584 | | #[inline] |
585 | 0 | fn next_back(&mut self) -> Option<Self::Item> { |
586 | 0 | self.iter.next_back() |
587 | 0 | } |
588 | | } |
589 | | |
590 | | impl $($generics)* ExactSizeIterator for $name $($generics)* { |
591 | | #[inline] |
592 | 0 | fn len(&self) -> usize { |
593 | 0 | self.iter.len() |
594 | 0 | } |
595 | | } |
596 | | |
597 | | impl $($generics)* FusedIterator for $name $($generics)* {} |
598 | | } |
599 | | } |
600 | | |
601 | | impl<'de> de::IntoDeserializer<'de, Error> for Map<String, Value> { |
602 | | type Deserializer = Self; |
603 | | |
604 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
605 | 0 | self |
606 | 0 | } |
607 | | } |
608 | | |
609 | | impl<'de> de::IntoDeserializer<'de, Error> for &'de Map<String, Value> { |
610 | | type Deserializer = Self; |
611 | | |
612 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
613 | 0 | self |
614 | 0 | } |
615 | | } |
616 | | |
617 | | ////////////////////////////////////////////////////////////////////////////// |
618 | | |
619 | | /// A view into a single entry in a map, which may either be vacant or occupied. |
620 | | /// This enum is constructed from the [`entry`] method on [`Map`]. |
621 | | /// |
622 | | /// [`entry`]: Map::entry |
623 | | pub enum Entry<'a> { |
624 | | /// A vacant Entry. |
625 | | Vacant(VacantEntry<'a>), |
626 | | /// An occupied Entry. |
627 | | Occupied(OccupiedEntry<'a>), |
628 | | } |
629 | | |
630 | | /// A vacant Entry. It is part of the [`Entry`] enum. |
631 | | pub struct VacantEntry<'a> { |
632 | | vacant: VacantEntryImpl<'a>, |
633 | | } |
634 | | |
635 | | /// An occupied Entry. It is part of the [`Entry`] enum. |
636 | | pub struct OccupiedEntry<'a> { |
637 | | occupied: OccupiedEntryImpl<'a>, |
638 | | } |
639 | | |
640 | | #[cfg(not(feature = "preserve_order"))] |
641 | | type VacantEntryImpl<'a> = btree_map::VacantEntry<'a, String, Value>; |
642 | | #[cfg(feature = "preserve_order")] |
643 | | type VacantEntryImpl<'a> = indexmap::map::VacantEntry<'a, String, Value>; |
644 | | |
645 | | #[cfg(not(feature = "preserve_order"))] |
646 | | type OccupiedEntryImpl<'a> = btree_map::OccupiedEntry<'a, String, Value>; |
647 | | #[cfg(feature = "preserve_order")] |
648 | | type OccupiedEntryImpl<'a> = indexmap::map::OccupiedEntry<'a, String, Value>; |
649 | | |
650 | | impl<'a> Entry<'a> { |
651 | | /// Returns a reference to this entry's key. |
652 | | /// |
653 | | /// # Examples |
654 | | /// |
655 | | /// ``` |
656 | | /// let mut map = serde_json::Map::new(); |
657 | | /// assert_eq!(map.entry("serde").key(), &"serde"); |
658 | | /// ``` |
659 | 0 | pub fn key(&self) -> &String { |
660 | 0 | match self { |
661 | 0 | Entry::Vacant(e) => e.key(), |
662 | 0 | Entry::Occupied(e) => e.key(), |
663 | | } |
664 | 0 | } |
665 | | |
666 | | /// Ensures a value is in the entry by inserting the default if empty, and |
667 | | /// returns a mutable reference to the value in the entry. |
668 | | /// |
669 | | /// # Examples |
670 | | /// |
671 | | /// ``` |
672 | | /// # use serde_json::json; |
673 | | /// # |
674 | | /// let mut map = serde_json::Map::new(); |
675 | | /// map.entry("serde").or_insert(json!(12)); |
676 | | /// |
677 | | /// assert_eq!(map["serde"], 12); |
678 | | /// ``` |
679 | 0 | pub fn or_insert(self, default: Value) -> &'a mut Value { |
680 | 0 | match self { |
681 | 0 | Entry::Vacant(entry) => entry.insert(default), |
682 | 0 | Entry::Occupied(entry) => entry.into_mut(), |
683 | | } |
684 | 0 | } |
685 | | |
686 | | /// Ensures a value is in the entry by inserting the result of the default |
687 | | /// function if empty, and returns a mutable reference to the value in the |
688 | | /// entry. |
689 | | /// |
690 | | /// # Examples |
691 | | /// |
692 | | /// ``` |
693 | | /// # use serde_json::json; |
694 | | /// # |
695 | | /// let mut map = serde_json::Map::new(); |
696 | | /// map.entry("serde").or_insert_with(|| json!("hoho")); |
697 | | /// |
698 | | /// assert_eq!(map["serde"], "hoho".to_owned()); |
699 | | /// ``` |
700 | 0 | pub fn or_insert_with<F>(self, default: F) -> &'a mut Value |
701 | 0 | where |
702 | 0 | F: FnOnce() -> Value, |
703 | 0 | { |
704 | 0 | match self { |
705 | 0 | Entry::Vacant(entry) => entry.insert(default()), |
706 | 0 | Entry::Occupied(entry) => entry.into_mut(), |
707 | | } |
708 | 0 | } |
709 | | |
710 | | /// Provides in-place mutable access to an occupied entry before any |
711 | | /// potential inserts into the map. |
712 | | /// |
713 | | /// # Examples |
714 | | /// |
715 | | /// ``` |
716 | | /// # use serde_json::json; |
717 | | /// # |
718 | | /// let mut map = serde_json::Map::new(); |
719 | | /// map.entry("serde") |
720 | | /// .and_modify(|e| *e = json!("rust")) |
721 | | /// .or_insert(json!("cpp")); |
722 | | /// |
723 | | /// assert_eq!(map["serde"], "cpp"); |
724 | | /// |
725 | | /// map.entry("serde") |
726 | | /// .and_modify(|e| *e = json!("rust")) |
727 | | /// .or_insert(json!("cpp")); |
728 | | /// |
729 | | /// assert_eq!(map["serde"], "rust"); |
730 | | /// ``` |
731 | 0 | pub fn and_modify<F>(self, f: F) -> Self |
732 | 0 | where |
733 | 0 | F: FnOnce(&mut Value), |
734 | 0 | { |
735 | 0 | match self { |
736 | 0 | Entry::Occupied(mut entry) => { |
737 | 0 | f(entry.get_mut()); |
738 | 0 | Entry::Occupied(entry) |
739 | | } |
740 | 0 | Entry::Vacant(entry) => Entry::Vacant(entry), |
741 | | } |
742 | 0 | } |
743 | | } |
744 | | |
745 | | impl<'a> VacantEntry<'a> { |
746 | | /// Gets a reference to the key that would be used when inserting a value |
747 | | /// through the VacantEntry. |
748 | | /// |
749 | | /// # Examples |
750 | | /// |
751 | | /// ``` |
752 | | /// use serde_json::map::Entry; |
753 | | /// |
754 | | /// let mut map = serde_json::Map::new(); |
755 | | /// |
756 | | /// match map.entry("serde") { |
757 | | /// Entry::Vacant(vacant) => { |
758 | | /// assert_eq!(vacant.key(), &"serde"); |
759 | | /// } |
760 | | /// Entry::Occupied(_) => unimplemented!(), |
761 | | /// } |
762 | | /// ``` |
763 | | #[inline] |
764 | 0 | pub fn key(&self) -> &String { |
765 | 0 | self.vacant.key() |
766 | 0 | } |
767 | | |
768 | | /// Sets the value of the entry with the VacantEntry's key, and returns a |
769 | | /// mutable reference to it. |
770 | | /// |
771 | | /// # Examples |
772 | | /// |
773 | | /// ``` |
774 | | /// # use serde_json::json; |
775 | | /// # |
776 | | /// use serde_json::map::Entry; |
777 | | /// |
778 | | /// let mut map = serde_json::Map::new(); |
779 | | /// |
780 | | /// match map.entry("serde") { |
781 | | /// Entry::Vacant(vacant) => { |
782 | | /// vacant.insert(json!("hoho")); |
783 | | /// } |
784 | | /// Entry::Occupied(_) => unimplemented!(), |
785 | | /// } |
786 | | /// ``` |
787 | | #[inline] |
788 | 0 | pub fn insert(self, value: Value) -> &'a mut Value { |
789 | 0 | self.vacant.insert(value) |
790 | 0 | } |
791 | | } |
792 | | |
793 | | impl<'a> OccupiedEntry<'a> { |
794 | | /// Gets a reference to the key in the entry. |
795 | | /// |
796 | | /// # Examples |
797 | | /// |
798 | | /// ``` |
799 | | /// # use serde_json::json; |
800 | | /// # |
801 | | /// use serde_json::map::Entry; |
802 | | /// |
803 | | /// let mut map = serde_json::Map::new(); |
804 | | /// map.insert("serde".to_owned(), json!(12)); |
805 | | /// |
806 | | /// match map.entry("serde") { |
807 | | /// Entry::Occupied(occupied) => { |
808 | | /// assert_eq!(occupied.key(), &"serde"); |
809 | | /// } |
810 | | /// Entry::Vacant(_) => unimplemented!(), |
811 | | /// } |
812 | | /// ``` |
813 | | #[inline] |
814 | 0 | pub fn key(&self) -> &String { |
815 | 0 | self.occupied.key() |
816 | 0 | } |
817 | | |
818 | | /// Gets a reference to the value in the entry. |
819 | | /// |
820 | | /// # Examples |
821 | | /// |
822 | | /// ``` |
823 | | /// # use serde_json::json; |
824 | | /// # |
825 | | /// use serde_json::map::Entry; |
826 | | /// |
827 | | /// let mut map = serde_json::Map::new(); |
828 | | /// map.insert("serde".to_owned(), json!(12)); |
829 | | /// |
830 | | /// match map.entry("serde") { |
831 | | /// Entry::Occupied(occupied) => { |
832 | | /// assert_eq!(occupied.get(), 12); |
833 | | /// } |
834 | | /// Entry::Vacant(_) => unimplemented!(), |
835 | | /// } |
836 | | /// ``` |
837 | | #[inline] |
838 | 0 | pub fn get(&self) -> &Value { |
839 | 0 | self.occupied.get() |
840 | 0 | } |
841 | | |
842 | | /// Gets a mutable reference to the value in the entry. |
843 | | /// |
844 | | /// # Examples |
845 | | /// |
846 | | /// ``` |
847 | | /// # use serde_json::json; |
848 | | /// # |
849 | | /// use serde_json::map::Entry; |
850 | | /// |
851 | | /// let mut map = serde_json::Map::new(); |
852 | | /// map.insert("serde".to_owned(), json!([1, 2, 3])); |
853 | | /// |
854 | | /// match map.entry("serde") { |
855 | | /// Entry::Occupied(mut occupied) => { |
856 | | /// occupied.get_mut().as_array_mut().unwrap().push(json!(4)); |
857 | | /// } |
858 | | /// Entry::Vacant(_) => unimplemented!(), |
859 | | /// } |
860 | | /// |
861 | | /// assert_eq!(map["serde"].as_array().unwrap().len(), 4); |
862 | | /// ``` |
863 | | #[inline] |
864 | 0 | pub fn get_mut(&mut self) -> &mut Value { |
865 | 0 | self.occupied.get_mut() |
866 | 0 | } |
867 | | |
868 | | /// Converts the entry into a mutable reference to its value. |
869 | | /// |
870 | | /// # Examples |
871 | | /// |
872 | | /// ``` |
873 | | /// # use serde_json::json; |
874 | | /// # |
875 | | /// use serde_json::map::Entry; |
876 | | /// |
877 | | /// let mut map = serde_json::Map::new(); |
878 | | /// map.insert("serde".to_owned(), json!([1, 2, 3])); |
879 | | /// |
880 | | /// match map.entry("serde") { |
881 | | /// Entry::Occupied(mut occupied) => { |
882 | | /// occupied.into_mut().as_array_mut().unwrap().push(json!(4)); |
883 | | /// } |
884 | | /// Entry::Vacant(_) => unimplemented!(), |
885 | | /// } |
886 | | /// |
887 | | /// assert_eq!(map["serde"].as_array().unwrap().len(), 4); |
888 | | /// ``` |
889 | | #[inline] |
890 | 0 | pub fn into_mut(self) -> &'a mut Value { |
891 | 0 | self.occupied.into_mut() |
892 | 0 | } |
893 | | |
894 | | /// Sets the value of the entry with the `OccupiedEntry`'s key, and returns |
895 | | /// the entry's old value. |
896 | | /// |
897 | | /// # Examples |
898 | | /// |
899 | | /// ``` |
900 | | /// # use serde_json::json; |
901 | | /// # |
902 | | /// use serde_json::map::Entry; |
903 | | /// |
904 | | /// let mut map = serde_json::Map::new(); |
905 | | /// map.insert("serde".to_owned(), json!(12)); |
906 | | /// |
907 | | /// match map.entry("serde") { |
908 | | /// Entry::Occupied(mut occupied) => { |
909 | | /// assert_eq!(occupied.insert(json!(13)), 12); |
910 | | /// assert_eq!(occupied.get(), 13); |
911 | | /// } |
912 | | /// Entry::Vacant(_) => unimplemented!(), |
913 | | /// } |
914 | | /// ``` |
915 | | #[inline] |
916 | 0 | pub fn insert(&mut self, value: Value) -> Value { |
917 | 0 | self.occupied.insert(value) |
918 | 0 | } |
919 | | |
920 | | /// Takes the value of the entry out of the map, and returns it. |
921 | | /// |
922 | | /// If serde_json's "preserve_order" is enabled, `.remove()` is |
923 | | /// equivalent to [`.swap_remove()`][Self::swap_remove], replacing this |
924 | | /// entry's position with the last element. If you need to preserve the |
925 | | /// relative order of the keys in the map, use |
926 | | /// [`.shift_remove()`][Self::shift_remove] instead. |
927 | | /// |
928 | | /// # Examples |
929 | | /// |
930 | | /// ``` |
931 | | /// # use serde_json::json; |
932 | | /// # |
933 | | /// use serde_json::map::Entry; |
934 | | /// |
935 | | /// let mut map = serde_json::Map::new(); |
936 | | /// map.insert("serde".to_owned(), json!(12)); |
937 | | /// |
938 | | /// match map.entry("serde") { |
939 | | /// Entry::Occupied(occupied) => { |
940 | | /// assert_eq!(occupied.remove(), 12); |
941 | | /// } |
942 | | /// Entry::Vacant(_) => unimplemented!(), |
943 | | /// } |
944 | | /// ``` |
945 | | #[inline] |
946 | 0 | pub fn remove(self) -> Value { |
947 | 0 | #[cfg(feature = "preserve_order")] |
948 | 0 | return self.swap_remove(); |
949 | 0 | #[cfg(not(feature = "preserve_order"))] |
950 | 0 | return self.occupied.remove(); |
951 | 0 | } |
952 | | |
953 | | /// Takes the value of the entry out of the map, and returns it. |
954 | | /// |
955 | | /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the |
956 | | /// last element of the map and popping it off. This perturbs the position |
957 | | /// of what used to be the last element! |
958 | | /// |
959 | | /// [`Vec::swap_remove`]: std::vec::Vec::swap_remove |
960 | | #[cfg(feature = "preserve_order")] |
961 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
962 | | #[inline] |
963 | | pub fn swap_remove(self) -> Value { |
964 | | self.occupied.swap_remove() |
965 | | } |
966 | | |
967 | | /// Takes the value of the entry out of the map, and returns it. |
968 | | /// |
969 | | /// Like [`Vec::remove`], the entry is removed by shifting all of the |
970 | | /// elements that follow it, preserving their relative order. This perturbs |
971 | | /// the index of all of those elements! |
972 | | /// |
973 | | /// [`Vec::remove`]: std::vec::Vec::remove |
974 | | #[cfg(feature = "preserve_order")] |
975 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
976 | | #[inline] |
977 | | pub fn shift_remove(self) -> Value { |
978 | | self.occupied.shift_remove() |
979 | | } |
980 | | |
981 | | /// Removes the entry from the map, returning the stored key and value. |
982 | | /// |
983 | | /// If serde_json's "preserve_order" is enabled, `.remove_entry()` is |
984 | | /// equivalent to [`.swap_remove_entry()`][Self::swap_remove_entry], |
985 | | /// replacing this entry's position with the last element. If you need to |
986 | | /// preserve the relative order of the keys in the map, use |
987 | | /// [`.shift_remove_entry()`][Self::shift_remove_entry] instead. |
988 | | /// |
989 | | /// # Examples |
990 | | /// |
991 | | /// ``` |
992 | | /// # use serde_json::json; |
993 | | /// # |
994 | | /// use serde_json::map::Entry; |
995 | | /// |
996 | | /// let mut map = serde_json::Map::new(); |
997 | | /// map.insert("serde".to_owned(), json!(12)); |
998 | | /// |
999 | | /// match map.entry("serde") { |
1000 | | /// Entry::Occupied(occupied) => { |
1001 | | /// let (key, value) = occupied.remove_entry(); |
1002 | | /// assert_eq!(key, "serde"); |
1003 | | /// assert_eq!(value, 12); |
1004 | | /// } |
1005 | | /// Entry::Vacant(_) => unimplemented!(), |
1006 | | /// } |
1007 | | /// ``` |
1008 | | #[inline] |
1009 | 0 | pub fn remove_entry(self) -> (String, Value) { |
1010 | 0 | #[cfg(feature = "preserve_order")] |
1011 | 0 | return self.swap_remove_entry(); |
1012 | 0 | #[cfg(not(feature = "preserve_order"))] |
1013 | 0 | return self.occupied.remove_entry(); |
1014 | 0 | } |
1015 | | |
1016 | | /// Removes the entry from the map, returning the stored key and value. |
1017 | | /// |
1018 | | /// Like [`Vec::swap_remove`], the entry is removed by swapping it with the |
1019 | | /// last element of the map and popping it off. This perturbs the position |
1020 | | /// of what used to be the last element! |
1021 | | /// |
1022 | | /// [`Vec::swap_remove`]: std::vec::Vec::swap_remove |
1023 | | #[cfg(feature = "preserve_order")] |
1024 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
1025 | | #[inline] |
1026 | | pub fn swap_remove_entry(self) -> (String, Value) { |
1027 | | self.occupied.swap_remove_entry() |
1028 | | } |
1029 | | |
1030 | | /// Removes the entry from the map, returning the stored key and value. |
1031 | | /// |
1032 | | /// Like [`Vec::remove`], the entry is removed by shifting all of the |
1033 | | /// elements that follow it, preserving their relative order. This perturbs |
1034 | | /// the index of all of those elements! |
1035 | | /// |
1036 | | /// [`Vec::remove`]: std::vec::Vec::remove |
1037 | | #[cfg(feature = "preserve_order")] |
1038 | | #[cfg_attr(docsrs, doc(cfg(feature = "preserve_order")))] |
1039 | | #[inline] |
1040 | | pub fn shift_remove_entry(self) -> (String, Value) { |
1041 | | self.occupied.shift_remove_entry() |
1042 | | } |
1043 | | } |
1044 | | |
1045 | | ////////////////////////////////////////////////////////////////////////////// |
1046 | | |
1047 | | impl<'a> IntoIterator for &'a Map<String, Value> { |
1048 | | type Item = (&'a String, &'a Value); |
1049 | | type IntoIter = Iter<'a>; |
1050 | | #[inline] |
1051 | 0 | fn into_iter(self) -> Self::IntoIter { |
1052 | 0 | Iter { |
1053 | 0 | iter: self.map.iter(), |
1054 | 0 | } |
1055 | 0 | } |
1056 | | } |
1057 | | |
1058 | | /// An iterator over a serde_json::Map's entries. |
1059 | | pub struct Iter<'a> { |
1060 | | iter: IterImpl<'a>, |
1061 | | } |
1062 | | |
1063 | | #[cfg(not(feature = "preserve_order"))] |
1064 | | type IterImpl<'a> = btree_map::Iter<'a, String, Value>; |
1065 | | #[cfg(feature = "preserve_order")] |
1066 | | type IterImpl<'a> = indexmap::map::Iter<'a, String, Value>; |
1067 | | |
1068 | | delegate_iterator!((Iter<'a>) => (&'a String, &'a Value)); |
1069 | | |
1070 | | ////////////////////////////////////////////////////////////////////////////// |
1071 | | |
1072 | | impl<'a> IntoIterator for &'a mut Map<String, Value> { |
1073 | | type Item = (&'a String, &'a mut Value); |
1074 | | type IntoIter = IterMut<'a>; |
1075 | | #[inline] |
1076 | 0 | fn into_iter(self) -> Self::IntoIter { |
1077 | 0 | IterMut { |
1078 | 0 | iter: self.map.iter_mut(), |
1079 | 0 | } |
1080 | 0 | } |
1081 | | } |
1082 | | |
1083 | | /// A mutable iterator over a serde_json::Map's entries. |
1084 | | pub struct IterMut<'a> { |
1085 | | iter: IterMutImpl<'a>, |
1086 | | } |
1087 | | |
1088 | | #[cfg(not(feature = "preserve_order"))] |
1089 | | type IterMutImpl<'a> = btree_map::IterMut<'a, String, Value>; |
1090 | | #[cfg(feature = "preserve_order")] |
1091 | | type IterMutImpl<'a> = indexmap::map::IterMut<'a, String, Value>; |
1092 | | |
1093 | | delegate_iterator!((IterMut<'a>) => (&'a String, &'a mut Value)); |
1094 | | |
1095 | | ////////////////////////////////////////////////////////////////////////////// |
1096 | | |
1097 | | impl IntoIterator for Map<String, Value> { |
1098 | | type Item = (String, Value); |
1099 | | type IntoIter = IntoIter; |
1100 | | #[inline] |
1101 | 0 | fn into_iter(self) -> Self::IntoIter { |
1102 | 0 | IntoIter { |
1103 | 0 | iter: self.map.into_iter(), |
1104 | 0 | } |
1105 | 0 | } |
1106 | | } |
1107 | | |
1108 | | /// An owning iterator over a serde_json::Map's entries. |
1109 | | pub struct IntoIter { |
1110 | | iter: IntoIterImpl, |
1111 | | } |
1112 | | |
1113 | | #[cfg(not(feature = "preserve_order"))] |
1114 | | type IntoIterImpl = btree_map::IntoIter<String, Value>; |
1115 | | #[cfg(feature = "preserve_order")] |
1116 | | type IntoIterImpl = indexmap::map::IntoIter<String, Value>; |
1117 | | |
1118 | | delegate_iterator!((IntoIter) => (String, Value)); |
1119 | | |
1120 | | ////////////////////////////////////////////////////////////////////////////// |
1121 | | |
1122 | | /// An iterator over a serde_json::Map's keys. |
1123 | | pub struct Keys<'a> { |
1124 | | iter: KeysImpl<'a>, |
1125 | | } |
1126 | | |
1127 | | #[cfg(not(feature = "preserve_order"))] |
1128 | | type KeysImpl<'a> = btree_map::Keys<'a, String, Value>; |
1129 | | #[cfg(feature = "preserve_order")] |
1130 | | type KeysImpl<'a> = indexmap::map::Keys<'a, String, Value>; |
1131 | | |
1132 | | delegate_iterator!((Keys<'a>) => &'a String); |
1133 | | |
1134 | | ////////////////////////////////////////////////////////////////////////////// |
1135 | | |
1136 | | /// An iterator over a serde_json::Map's values. |
1137 | | pub struct Values<'a> { |
1138 | | iter: ValuesImpl<'a>, |
1139 | | } |
1140 | | |
1141 | | #[cfg(not(feature = "preserve_order"))] |
1142 | | type ValuesImpl<'a> = btree_map::Values<'a, String, Value>; |
1143 | | #[cfg(feature = "preserve_order")] |
1144 | | type ValuesImpl<'a> = indexmap::map::Values<'a, String, Value>; |
1145 | | |
1146 | | delegate_iterator!((Values<'a>) => &'a Value); |
1147 | | |
1148 | | ////////////////////////////////////////////////////////////////////////////// |
1149 | | |
1150 | | /// A mutable iterator over a serde_json::Map's values. |
1151 | | pub struct ValuesMut<'a> { |
1152 | | iter: ValuesMutImpl<'a>, |
1153 | | } |
1154 | | |
1155 | | #[cfg(not(feature = "preserve_order"))] |
1156 | | type ValuesMutImpl<'a> = btree_map::ValuesMut<'a, String, Value>; |
1157 | | #[cfg(feature = "preserve_order")] |
1158 | | type ValuesMutImpl<'a> = indexmap::map::ValuesMut<'a, String, Value>; |
1159 | | |
1160 | | delegate_iterator!((ValuesMut<'a>) => &'a mut Value); |
1161 | | |
1162 | | ////////////////////////////////////////////////////////////////////////////// |
1163 | | |
1164 | | /// An owning iterator over a serde_json::Map's values. |
1165 | | pub struct IntoValues { |
1166 | | iter: IntoValuesImpl, |
1167 | | } |
1168 | | |
1169 | | #[cfg(not(feature = "preserve_order"))] |
1170 | | type IntoValuesImpl = btree_map::IntoValues<String, Value>; |
1171 | | #[cfg(feature = "preserve_order")] |
1172 | | type IntoValuesImpl = indexmap::map::IntoValues<String, Value>; |
1173 | | |
1174 | | delegate_iterator!((IntoValues) => Value); |