serde_lite/
map.rs

1use std::{
2    borrow::Cow,
3    ops::{Deref, DerefMut},
4};
5
6use crate::Intermediate;
7
8/// Type alias.
9#[cfg(feature = "preserve-order")]
10pub type MapImpl<K, V> = indexmap::IndexMap<K, V>;
11
12/// Type alias.
13#[cfg(not(feature = "preserve-order"))]
14pub type MapImpl<K, V> = std::collections::HashMap<K, V>;
15
16/// Map from string keys to `Intermediate` values.
17///
18/// It wraps the underlying map implementation and prohibits inlining of some
19/// methods in order to make the generated code smaller.
20#[derive(Debug, Clone)]
21pub struct Map {
22    inner: MapImpl<Cow<'static, str>, Intermediate>,
23}
24
25impl Map {
26    /// Create a new map.
27    #[inline(never)]
28    pub fn new() -> Self {
29        Self {
30            inner: MapImpl::new(),
31        }
32    }
33
34    /// Create a new map with a given capacity.
35    #[inline(never)]
36    pub fn with_capacity(capacity: usize) -> Self {
37        Self {
38            inner: MapImpl::with_capacity(capacity),
39        }
40    }
41
42    /// Get value associated with a given key.
43    #[inline(never)]
44    pub fn get(&self, key: &str) -> Option<&Intermediate> {
45        self.inner.get(key)
46    }
47
48    /// Insert a given key-value pair into the map.
49    #[inline(never)]
50    pub fn insert_with_static_key(&mut self, key: &'static str, value: Intermediate) {
51        self.inner.insert(Cow::Borrowed(key), value);
52    }
53
54    /// Insert a given key-value pair into the map.
55    #[inline(never)]
56    pub fn insert_with_owned_key(&mut self, key: String, value: Intermediate) {
57        self.inner.insert(Cow::Owned(key), value);
58    }
59}
60
61impl Default for Map {
62    #[inline]
63    fn default() -> Self {
64        Self::new()
65    }
66}
67
68impl From<MapImpl<Cow<'static, str>, Intermediate>> for Map {
69    #[inline]
70    fn from(map: MapImpl<Cow<'static, str>, Intermediate>) -> Self {
71        Self { inner: map }
72    }
73}
74
75impl From<Map> for MapImpl<Cow<'static, str>, Intermediate> {
76    #[inline]
77    fn from(map: Map) -> Self {
78        map.inner
79    }
80}
81
82impl Deref for Map {
83    type Target = MapImpl<Cow<'static, str>, Intermediate>;
84
85    #[inline]
86    fn deref(&self) -> &Self::Target {
87        &self.inner
88    }
89}
90
91impl DerefMut for Map {
92    #[inline]
93    fn deref_mut(&mut self) -> &mut Self::Target {
94        &mut self.inner
95    }
96}
97
98impl IntoIterator for Map {
99    type Item = (Cow<'static, str>, Intermediate);
100
101    #[cfg(feature = "preserve-order")]
102    type IntoIter = indexmap::map::IntoIter<Cow<'static, str>, Intermediate>;
103
104    #[cfg(not(feature = "preserve-order"))]
105    type IntoIter = std::collections::hash_map::IntoIter<Cow<'static, str>, Intermediate>;
106
107    #[inline]
108    fn into_iter(self) -> Self::IntoIter {
109        self.inner.into_iter()
110    }
111}
112
113impl<'a> IntoIterator for &'a Map {
114    type Item = (&'a Cow<'static, str>, &'a Intermediate);
115
116    #[cfg(feature = "preserve-order")]
117    type IntoIter = indexmap::map::Iter<'a, Cow<'static, str>, Intermediate>;
118
119    #[cfg(not(feature = "preserve-order"))]
120    type IntoIter = std::collections::hash_map::Iter<'a, Cow<'static, str>, Intermediate>;
121
122    #[inline]
123    fn into_iter(self) -> Self::IntoIter {
124        self.inner.iter()
125    }
126}