Skip to main content

serde_with/duplicate_key_impls/
error_on_duplicate.rs

1use crate::prelude::*;
2#[cfg(any(
3    feature = "std",
4    feature = "hashbrown_0_14",
5    feature = "hashbrown_0_15",
6    feature = "hashbrown_0_16",
7    feature = "hashbrown_0_17",
8    feature = "indexmap_1",
9    feature = "indexmap_2"
10))]
11use crate::utils::size_hint_cautious;
12
13pub trait PreventDuplicateInsertsSet<T> {
14    fn new(size_hint: Option<usize>) -> Self;
15
16    /// Return true if the insert was successful and the value did not exist in the set
17    fn insert(&mut self, value: T) -> bool;
18}
19
20pub trait PreventDuplicateInsertsMap<K, V> {
21    fn new(size_hint: Option<usize>) -> Self;
22
23    /// Return true if the insert was successful and the key did not exist in the map
24    fn insert(&mut self, key: K, value: V) -> bool;
25}
26
27#[cfg(feature = "std")]
28impl<T, S> PreventDuplicateInsertsSet<T> for HashSet<T, S>
29where
30    T: Eq + Hash,
31    S: BuildHasher + Default,
32{
33    #[inline]
34    fn new(size_hint: Option<usize>) -> Self {
35        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
36    }
37
38    #[inline]
39    fn insert(&mut self, value: T) -> bool {
40        self.insert(value)
41    }
42}
43
44#[cfg(feature = "hashbrown_0_14")]
45impl<T, S> PreventDuplicateInsertsSet<T> for hashbrown_0_14::HashSet<T, S>
46where
47    T: Eq + Hash,
48    S: BuildHasher + Default,
49{
50    #[inline]
51    fn new(size_hint: Option<usize>) -> Self {
52        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
53    }
54
55    #[inline]
56    fn insert(&mut self, value: T) -> bool {
57        self.insert(value)
58    }
59}
60
61#[cfg(feature = "hashbrown_0_15")]
62impl<T, S> PreventDuplicateInsertsSet<T> for hashbrown_0_15::HashSet<T, S>
63where
64    T: Eq + Hash,
65    S: BuildHasher + Default,
66{
67    #[inline]
68    fn new(size_hint: Option<usize>) -> Self {
69        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
70    }
71
72    #[inline]
73    fn insert(&mut self, value: T) -> bool {
74        self.insert(value)
75    }
76}
77
78#[cfg(feature = "hashbrown_0_16")]
79impl<T, S> PreventDuplicateInsertsSet<T> for hashbrown_0_16::HashSet<T, S>
80where
81    T: Eq + Hash,
82    S: BuildHasher + Default,
83{
84    #[inline]
85    fn new(size_hint: Option<usize>) -> Self {
86        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
87    }
88
89    #[inline]
90    fn insert(&mut self, value: T) -> bool {
91        self.insert(value)
92    }
93}
94
95#[cfg(feature = "hashbrown_0_17")]
96impl<T, S> PreventDuplicateInsertsSet<T> for hashbrown_0_17::HashSet<T, S>
97where
98    T: Eq + Hash,
99    S: BuildHasher + Default,
100{
101    #[inline]
102    fn new(size_hint: Option<usize>) -> Self {
103        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
104    }
105
106    #[inline]
107    fn insert(&mut self, value: T) -> bool {
108        self.insert(value)
109    }
110}
111
112#[cfg(feature = "indexmap_1")]
113impl<T, S> PreventDuplicateInsertsSet<T> for indexmap_1::IndexSet<T, S>
114where
115    T: Eq + Hash,
116    S: BuildHasher + Default,
117{
118    #[inline]
119    fn new(size_hint: Option<usize>) -> Self {
120        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
121    }
122
123    #[inline]
124    fn insert(&mut self, value: T) -> bool {
125        self.insert(value)
126    }
127}
128
129#[cfg(feature = "indexmap_2")]
130impl<T, S> PreventDuplicateInsertsSet<T> for indexmap_2::IndexSet<T, S>
131where
132    T: Eq + Hash,
133    S: BuildHasher + Default,
134{
135    #[inline]
136    fn new(size_hint: Option<usize>) -> Self {
137        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
138    }
139
140    #[inline]
141    fn insert(&mut self, value: T) -> bool {
142        self.insert(value)
143    }
144}
145
146impl<T> PreventDuplicateInsertsSet<T> for BTreeSet<T>
147where
148    T: Ord,
149{
150    #[inline]
151    fn new(_size_hint: Option<usize>) -> Self {
152        Self::new()
153    }
154
155    #[inline]
156    fn insert(&mut self, value: T) -> bool {
157        self.insert(value)
158    }
159}
160
161#[cfg(feature = "std")]
162impl<K, V, S> PreventDuplicateInsertsMap<K, V> for HashMap<K, V, S>
163where
164    K: Eq + Hash,
165    S: BuildHasher + Default,
166{
167    #[inline]
168    fn new(size_hint: Option<usize>) -> Self {
169        Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
170    }
171
172    #[inline]
173    fn insert(&mut self, key: K, value: V) -> bool {
174        self.insert(key, value).is_none()
175    }
176}
177
178#[cfg(feature = "hashbrown_0_14")]
179impl<K, V, S> PreventDuplicateInsertsMap<K, V> for hashbrown_0_14::HashMap<K, V, S>
180where
181    K: Eq + Hash,
182    S: BuildHasher + Default,
183{
184    #[inline]
185    fn new(size_hint: Option<usize>) -> Self {
186        Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
187    }
188
189    #[inline]
190    fn insert(&mut self, key: K, value: V) -> bool {
191        self.insert(key, value).is_none()
192    }
193}
194
195#[cfg(feature = "hashbrown_0_15")]
196impl<K, V, S> PreventDuplicateInsertsMap<K, V> for hashbrown_0_15::HashMap<K, V, S>
197where
198    K: Eq + Hash,
199    S: BuildHasher + Default,
200{
201    #[inline]
202    fn new(size_hint: Option<usize>) -> Self {
203        Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
204    }
205
206    #[inline]
207    fn insert(&mut self, key: K, value: V) -> bool {
208        self.insert(key, value).is_none()
209    }
210}
211
212#[cfg(feature = "hashbrown_0_16")]
213impl<K, V, S> PreventDuplicateInsertsMap<K, V> for hashbrown_0_16::HashMap<K, V, S>
214where
215    K: Eq + Hash,
216    S: BuildHasher + Default,
217{
218    #[inline]
219    fn new(size_hint: Option<usize>) -> Self {
220        Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
221    }
222
223    #[inline]
224    fn insert(&mut self, key: K, value: V) -> bool {
225        self.insert(key, value).is_none()
226    }
227}
228
229#[cfg(feature = "hashbrown_0_17")]
230impl<K, V, S> PreventDuplicateInsertsMap<K, V> for hashbrown_0_17::HashMap<K, V, S>
231where
232    K: Eq + Hash,
233    S: BuildHasher + Default,
234{
235    #[inline]
236    fn new(size_hint: Option<usize>) -> Self {
237        Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
238    }
239
240    #[inline]
241    fn insert(&mut self, key: K, value: V) -> bool {
242        self.insert(key, value).is_none()
243    }
244}
245
246#[cfg(feature = "indexmap_1")]
247impl<K, V, S> PreventDuplicateInsertsMap<K, V> for indexmap_1::IndexMap<K, V, S>
248where
249    K: Eq + Hash,
250    S: BuildHasher + Default,
251{
252    #[inline]
253    fn new(size_hint: Option<usize>) -> Self {
254        Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
255    }
256
257    #[inline]
258    fn insert(&mut self, key: K, value: V) -> bool {
259        self.insert(key, value).is_none()
260    }
261}
262
263#[cfg(feature = "indexmap_2")]
264impl<K, V, S> PreventDuplicateInsertsMap<K, V> for indexmap_2::IndexMap<K, V, S>
265where
266    K: Eq + Hash,
267    S: BuildHasher + Default,
268{
269    #[inline]
270    fn new(size_hint: Option<usize>) -> Self {
271        Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
272    }
273
274    #[inline]
275    fn insert(&mut self, key: K, value: V) -> bool {
276        self.insert(key, value).is_none()
277    }
278}
279
280impl<K, V> PreventDuplicateInsertsMap<K, V> for BTreeMap<K, V>
281where
282    K: Ord,
283{
284    #[inline]
285    fn new(_size_hint: Option<usize>) -> Self {
286        Self::new()
287    }
288
289    #[inline]
290    fn insert(&mut self, key: K, value: V) -> bool {
291        self.insert(key, value).is_none()
292    }
293}