serde_with/duplicate_key_impls/
error_on_duplicate.rs

1use alloc::collections::{BTreeMap, BTreeSet};
2use core::hash::{BuildHasher, Hash};
3#[cfg(feature = "indexmap")]
4use indexmap_crate::{IndexMap, IndexSet};
5use std::collections::{HashMap, HashSet};
6
7pub trait PreventDuplicateInsertsSet<T> {
8    fn new(size_hint: Option<usize>) -> Self;
9
10    /// Return true if the insert was successful and the value did not exist in the set
11    fn insert(&mut self, value: T) -> bool;
12}
13
14pub trait PreventDuplicateInsertsMap<K, V> {
15    fn new(size_hint: Option<usize>) -> Self;
16
17    /// Return true if the insert was successful and the key did not exist in the map
18    fn insert(&mut self, key: K, value: V) -> bool;
19}
20
21impl<T, S> PreventDuplicateInsertsSet<T> for HashSet<T, S>
22where
23    T: Eq + Hash,
24    S: BuildHasher + Default,
25{
26    #[inline]
27    fn new(size_hint: Option<usize>) -> Self {
28        match size_hint {
29            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
30            None => Self::with_hasher(S::default()),
31        }
32    }
33
34    #[inline]
35    fn insert(&mut self, value: T) -> bool {
36        self.insert(value)
37    }
38}
39
40#[cfg(feature = "indexmap")]
41impl<T, S> PreventDuplicateInsertsSet<T> for IndexSet<T, S>
42where
43    T: Eq + Hash,
44    S: BuildHasher + Default,
45{
46    #[inline]
47    fn new(size_hint: Option<usize>) -> Self {
48        match size_hint {
49            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
50            None => Self::with_hasher(S::default()),
51        }
52    }
53
54    #[inline]
55    fn insert(&mut self, value: T) -> bool {
56        self.insert(value)
57    }
58}
59
60impl<T> PreventDuplicateInsertsSet<T> for BTreeSet<T>
61where
62    T: Ord,
63{
64    #[inline]
65    fn new(_size_hint: Option<usize>) -> Self {
66        Self::new()
67    }
68
69    #[inline]
70    fn insert(&mut self, value: T) -> bool {
71        self.insert(value)
72    }
73}
74
75impl<K, V, S> PreventDuplicateInsertsMap<K, V> for HashMap<K, V, S>
76where
77    K: Eq + Hash,
78    S: BuildHasher + Default,
79{
80    #[inline]
81    fn new(size_hint: Option<usize>) -> Self {
82        match size_hint {
83            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
84            None => Self::with_hasher(S::default()),
85        }
86    }
87
88    #[inline]
89    fn insert(&mut self, key: K, value: V) -> bool {
90        self.insert(key, value).is_none()
91    }
92}
93
94#[cfg(feature = "indexmap")]
95impl<K, V, S> PreventDuplicateInsertsMap<K, V> for IndexMap<K, V, S>
96where
97    K: Eq + Hash,
98    S: BuildHasher + Default,
99{
100    #[inline]
101    fn new(size_hint: Option<usize>) -> Self {
102        match size_hint {
103            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
104            None => Self::with_hasher(S::default()),
105        }
106    }
107
108    #[inline]
109    fn insert(&mut self, key: K, value: V) -> bool {
110        self.insert(key, value).is_none()
111    }
112}
113
114impl<K, V> PreventDuplicateInsertsMap<K, V> for BTreeMap<K, V>
115where
116    K: Ord,
117{
118    #[inline]
119    fn new(_size_hint: Option<usize>) -> Self {
120        Self::new()
121    }
122
123    #[inline]
124    fn insert(&mut self, key: K, value: V) -> bool {
125        self.insert(key, value).is_none()
126    }
127}