Skip to main content

deser_core/foreign_impls/
hashbrown.rs

1//! `HashMap` and `HashSet` of `hashbrown`.
2//!
3//! They are serialized like the maps and sets of the standard library.
4use core::hash::{BuildHasher, Hash};
5
6use ::hashbrown::{HashMap, HashSet, hash_map};
7
8use alloc::borrow::Cow;
9
10use crate::State;
11use crate::adapters::{MapSkipError, skip_map_sink};
12use crate::de::impls::{
13    MapOut, MapTarget, SetTarget, collection_methods, map_sink, set_collection, set_sink,
14};
15use crate::de::{Deserialize, SinkHandle};
16use crate::error::Error;
17use crate::event::ContainerShape;
18use crate::ser::impls::{serialize_map, serialize_set};
19use crate::ser::{Describe, Emit, Serialize};
20
21// the hasher of the adapter is not used
22serialize_map! {
23    [K, V, S: Sync, KA, VA, AS: Sync] HashMap<K, V, S> => HashMap<KA, VA, AS>, Arbitrary;
24}
25
26impl<K, V, S> MapTarget<K, V> for HashMap<K, V, S>
27where
28    K: Hash + Eq + Send,
29    V: Send,
30    S: BuildHasher + Default + Send,
31{
32    const NAME: &'static str = "HashMap";
33
34    #[inline]
35    fn insert_entry(&mut self, key: K, value: V, replace: bool) -> bool {
36        match self.entry(key) {
37            hash_map::Entry::Vacant(entry) => {
38                entry.insert(value);
39                false
40            }
41            hash_map::Entry::Occupied(mut entry) => {
42                if replace {
43                    entry.insert(value);
44                }
45                true
46            }
47        }
48    }
49
50    #[inline]
51    fn reserve_entries(&mut self, additional: usize) {
52        self.reserve(additional);
53    }
54
55    #[inline]
56    fn entry_mut(&mut self, key: &K) -> Option<&mut V> {
57        self.get_mut(key)
58    }
59
60    fn merge(&mut self, mut other: Self) {
61        // the smaller map is moved into the larger one
62        if other.len() > self.len() {
63            core::mem::swap(self, &mut other);
64            for (key, value) in other {
65                self.entry(key).or_insert(value);
66            }
67        } else {
68            self.extend(other);
69        }
70    }
71}
72
73// the hasher of the adapter is not used
74impl<'de, K, V, S, KA, VA, AS> Deserialize<'de, HashMap<K, V, S>> for HashMap<KA, VA, AS>
75where
76    K: Hash + Eq + Send,
77    V: Send,
78    S: BuildHasher + Default + Send,
79    KA: Deserialize<'de, K>,
80    VA: Deserialize<'de, V>,
81    AS: Send,
82{
83    #[inline]
84    fn deserialize_into<'out>(
85        out: &'out mut Option<HashMap<K, V, S>>,
86        state: &mut State,
87    ) -> SinkHandle<'out, 'de> {
88        map_sink::<_, K, V, KA, VA>(MapOut::Slot(out), state)
89    }
90
91    fn expecting() -> Cow<'static, str> {
92        Cow::Borrowed(<HashMap<K, V, S> as MapTarget<K, V>>::NAME)
93    }
94
95    /// Merges the entries into the map, the values of keys that exist are
96    /// replaced (not updated).
97    fn deserialize_update<'out>(
98        value: &'out mut HashMap<K, V, S>,
99        state: &mut State,
100    ) -> SinkHandle<'out, 'de> {
101        map_sink::<_, K, V, KA, VA>(MapOut::Update(value), state)
102    }
103}
104
105impl<'de, K, V, S, KA, VA> Deserialize<'de, HashMap<K, V, S>> for MapSkipError<KA, VA>
106where
107    K: Hash + Eq + Send,
108    V: Send,
109    S: BuildHasher + Default + Send,
110    KA: Deserialize<'de, K>,
111    VA: Deserialize<'de, V>,
112{
113    fn deserialize_into<'out>(
114        out: &'out mut Option<HashMap<K, V, S>>,
115        state: &mut State,
116    ) -> SinkHandle<'out, 'de> {
117        skip_map_sink::<_, K, V, KA, VA>(out, state)
118    }
119
120    fn expecting() -> Cow<'static, str> {
121        Cow::Borrowed(<HashMap<K, V, S> as MapTarget<K, V>>::NAME)
122    }
123}
124
125impl<K, V, S, KA, VA> Serialize<HashMap<K, V, S>> for MapSkipError<KA, VA>
126where
127    K: Sync,
128    V: Sync,
129    S: Sync,
130    KA: Serialize<K>,
131    VA: Serialize<V>,
132{
133    fn serialize<'a>(value: &'a HashMap<K, V, S>, state: &mut State) -> Result<Emit<'a>, Error> {
134        <HashMap<KA, VA, S> as Serialize<HashMap<K, V, S>>>::serialize(value, state)
135    }
136
137    fn container_shape(value: &HashMap<K, V, S>) -> ContainerShape {
138        <HashMap<KA, VA, S> as Serialize<HashMap<K, V, S>>>::container_shape(value)
139    }
140
141    fn describe(value: &HashMap<K, V, S>, d: &mut dyn Describe) {
142        <HashMap<KA, VA, S> as Serialize<HashMap<K, V, S>>>::describe(value, d)
143    }
144}
145
146// the hasher of the adapter is not used
147serialize_set! {
148    [T, S: Sync, A, AS: Sync] HashSet<T, S> => HashSet<A, AS>, Arbitrary;
149}
150
151impl<T, S> SetTarget<T> for HashSet<T, S>
152where
153    T: Hash + Eq + Send,
154    S: BuildHasher + Default + Send,
155{
156    const NAME: &'static str = "HashSet";
157
158    #[inline]
159    fn insert_element(&mut self, value: T) {
160        self.insert(value);
161    }
162
163    #[inline]
164    fn reserve_elements(&mut self, additional: usize) {
165        self.reserve(additional);
166    }
167}
168
169set_collection! {
170    [T: Hash + Eq + Send, S: BuildHasher + Default + Send] HashSet<T, S>;
171}
172
173// the hasher of the adapter is not used
174impl<'de, T, S, A, AS> Deserialize<'de, HashSet<T, S>> for HashSet<A, AS>
175where
176    T: Hash + Eq + Send,
177    S: BuildHasher + Default + Send,
178    A: Deserialize<'de, T>,
179    AS: Send,
180{
181    #[inline]
182    fn deserialize_into<'out>(
183        out: &'out mut Option<HashSet<T, S>>,
184        state: &mut State,
185    ) -> SinkHandle<'out, 'de> {
186        set_sink::<_, T, A>(out, state)
187    }
188
189    fn expecting() -> Cow<'static, str> {
190        Cow::Borrowed(<HashSet<T, S> as SetTarget<T>>::NAME)
191    }
192
193    collection_methods!(set HashSet<T, S>);
194}