serde_with/duplicate_key_impls/
error_on_duplicate.rs
1use crate::prelude::*;
2
3pub trait PreventDuplicateInsertsSet<T> {
4 fn new(size_hint: Option<usize>) -> Self;
5
6 fn insert(&mut self, value: T) -> bool;
8}
9
10pub trait PreventDuplicateInsertsMap<K, V> {
11 fn new(size_hint: Option<usize>) -> Self;
12
13 fn insert(&mut self, key: K, value: V) -> bool;
15}
16
17#[cfg(feature = "std")]
18impl<T, S> PreventDuplicateInsertsSet<T> for HashSet<T, S>
19where
20 T: Eq + Hash,
21 S: BuildHasher + Default,
22{
23 #[inline]
24 fn new(size_hint: Option<usize>) -> Self {
25 match size_hint {
26 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
27 None => Self::with_hasher(S::default()),
28 }
29 }
30
31 #[inline]
32 fn insert(&mut self, value: T) -> bool {
33 self.insert(value)
34 }
35}
36
37#[cfg(feature = "hashbrown_0_14")]
38impl<T, S> PreventDuplicateInsertsSet<T> for hashbrown_0_14::HashSet<T, S>
39where
40 T: Eq + Hash,
41 S: BuildHasher + Default,
42{
43 #[inline]
44 fn new(size_hint: Option<usize>) -> Self {
45 match size_hint {
46 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
47 None => Self::with_hasher(S::default()),
48 }
49 }
50
51 #[inline]
52 fn insert(&mut self, value: T) -> bool {
53 self.insert(value)
54 }
55}
56
57#[cfg(feature = "hashbrown_0_15")]
58impl<T, S> PreventDuplicateInsertsSet<T> for hashbrown_0_15::HashSet<T, S>
59where
60 T: Eq + Hash,
61 S: BuildHasher + Default,
62{
63 #[inline]
64 fn new(size_hint: Option<usize>) -> Self {
65 match size_hint {
66 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
67 None => Self::with_hasher(S::default()),
68 }
69 }
70
71 #[inline]
72 fn insert(&mut self, value: T) -> bool {
73 self.insert(value)
74 }
75}
76
77#[cfg(feature = "indexmap_1")]
78impl<T, S> PreventDuplicateInsertsSet<T> for indexmap_1::IndexSet<T, S>
79where
80 T: Eq + Hash,
81 S: BuildHasher + Default,
82{
83 #[inline]
84 fn new(size_hint: Option<usize>) -> Self {
85 match size_hint {
86 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
87 None => Self::with_hasher(S::default()),
88 }
89 }
90
91 #[inline]
92 fn insert(&mut self, value: T) -> bool {
93 self.insert(value)
94 }
95}
96
97#[cfg(feature = "indexmap_2")]
98impl<T, S> PreventDuplicateInsertsSet<T> for indexmap_2::IndexSet<T, S>
99where
100 T: Eq + Hash,
101 S: BuildHasher + Default,
102{
103 #[inline]
104 fn new(size_hint: Option<usize>) -> Self {
105 match size_hint {
106 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
107 None => Self::with_hasher(S::default()),
108 }
109 }
110
111 #[inline]
112 fn insert(&mut self, value: T) -> bool {
113 self.insert(value)
114 }
115}
116
117impl<T> PreventDuplicateInsertsSet<T> for BTreeSet<T>
118where
119 T: Ord,
120{
121 #[inline]
122 fn new(_size_hint: Option<usize>) -> Self {
123 Self::new()
124 }
125
126 #[inline]
127 fn insert(&mut self, value: T) -> bool {
128 self.insert(value)
129 }
130}
131
132#[cfg(feature = "std")]
133impl<K, V, S> PreventDuplicateInsertsMap<K, V> for HashMap<K, V, S>
134where
135 K: Eq + Hash,
136 S: BuildHasher + Default,
137{
138 #[inline]
139 fn new(size_hint: Option<usize>) -> Self {
140 match size_hint {
141 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
142 None => Self::with_hasher(S::default()),
143 }
144 }
145
146 #[inline]
147 fn insert(&mut self, key: K, value: V) -> bool {
148 self.insert(key, value).is_none()
149 }
150}
151
152#[cfg(feature = "hashbrown_0_14")]
153impl<K, V, S> PreventDuplicateInsertsMap<K, V> for hashbrown_0_14::HashMap<K, V, S>
154where
155 K: Eq + Hash,
156 S: BuildHasher + Default,
157{
158 #[inline]
159 fn new(size_hint: Option<usize>) -> Self {
160 match size_hint {
161 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
162 None => Self::with_hasher(S::default()),
163 }
164 }
165
166 #[inline]
167 fn insert(&mut self, key: K, value: V) -> bool {
168 self.insert(key, value).is_none()
169 }
170}
171
172#[cfg(feature = "hashbrown_0_15")]
173impl<K, V, S> PreventDuplicateInsertsMap<K, V> for hashbrown_0_15::HashMap<K, V, S>
174where
175 K: Eq + Hash,
176 S: BuildHasher + Default,
177{
178 #[inline]
179 fn new(size_hint: Option<usize>) -> Self {
180 match size_hint {
181 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
182 None => Self::with_hasher(S::default()),
183 }
184 }
185
186 #[inline]
187 fn insert(&mut self, key: K, value: V) -> bool {
188 self.insert(key, value).is_none()
189 }
190}
191
192#[cfg(feature = "indexmap_1")]
193impl<K, V, S> PreventDuplicateInsertsMap<K, V> for indexmap_1::IndexMap<K, V, S>
194where
195 K: Eq + Hash,
196 S: BuildHasher + Default,
197{
198 #[inline]
199 fn new(size_hint: Option<usize>) -> Self {
200 match size_hint {
201 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
202 None => Self::with_hasher(S::default()),
203 }
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 = "indexmap_2")]
213impl<K, V, S> PreventDuplicateInsertsMap<K, V> for indexmap_2::IndexMap<K, V, S>
214where
215 K: Eq + Hash,
216 S: BuildHasher + Default,
217{
218 #[inline]
219 fn new(size_hint: Option<usize>) -> Self {
220 match size_hint {
221 Some(size) => Self::with_capacity_and_hasher(size, S::default()),
222 None => Self::with_hasher(S::default()),
223 }
224 }
225
226 #[inline]
227 fn insert(&mut self, key: K, value: V) -> bool {
228 self.insert(key, value).is_none()
229 }
230}
231
232impl<K, V> PreventDuplicateInsertsMap<K, V> for BTreeMap<K, V>
233where
234 K: Ord,
235{
236 #[inline]
237 fn new(_size_hint: Option<usize>) -> Self {
238 Self::new()
239 }
240
241 #[inline]
242 fn insert(&mut self, key: K, value: V) -> bool {
243 self.insert(key, value).is_none()
244 }
245}