Skip to main content

serde_with/duplicate_key_impls/
last_value_wins.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 DuplicateInsertsLastWinsSet<T> {
14    fn new(size_hint: Option<usize>) -> Self;
15
16    /// Insert or replace the existing value
17    fn replace(&mut self, value: T);
18}
19
20#[cfg(feature = "std")]
21impl<T, S> DuplicateInsertsLastWinsSet<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        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
29    }
30
31    #[inline]
32    fn replace(&mut self, value: T) {
33        // Hashset already fulfils the contract
34        self.replace(value);
35    }
36}
37
38#[cfg(feature = "hashbrown_0_14")]
39impl<T, S> DuplicateInsertsLastWinsSet<T> for hashbrown_0_14::HashSet<T, S>
40where
41    T: Eq + Hash,
42    S: BuildHasher + Default,
43{
44    #[inline]
45    fn new(size_hint: Option<usize>) -> Self {
46        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
47    }
48
49    #[inline]
50    fn replace(&mut self, value: T) {
51        // Hashset already fulfils the contract
52        self.replace(value);
53    }
54}
55
56#[cfg(feature = "hashbrown_0_15")]
57impl<T, S> DuplicateInsertsLastWinsSet<T> for hashbrown_0_15::HashSet<T, S>
58where
59    T: Eq + Hash,
60    S: BuildHasher + Default,
61{
62    #[inline]
63    fn new(size_hint: Option<usize>) -> Self {
64        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
65    }
66
67    #[inline]
68    fn replace(&mut self, value: T) {
69        // Hashset already fulfils the contract
70        self.replace(value);
71    }
72}
73
74#[cfg(feature = "hashbrown_0_16")]
75impl<T, S> DuplicateInsertsLastWinsSet<T> for hashbrown_0_16::HashSet<T, S>
76where
77    T: Eq + Hash,
78    S: BuildHasher + Default,
79{
80    #[inline]
81    fn new(size_hint: Option<usize>) -> Self {
82        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
83    }
84
85    #[inline]
86    fn replace(&mut self, value: T) {
87        // Hashset already fulfils the contract
88        self.replace(value);
89    }
90}
91
92#[cfg(feature = "hashbrown_0_17")]
93impl<T, S> DuplicateInsertsLastWinsSet<T> for hashbrown_0_17::HashSet<T, S>
94where
95    T: Eq + Hash,
96    S: BuildHasher + Default,
97{
98    #[inline]
99    fn new(size_hint: Option<usize>) -> Self {
100        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
101    }
102
103    #[inline]
104    fn replace(&mut self, value: T) {
105        // Hashset already fulfils the contract
106        self.replace(value);
107    }
108}
109
110#[cfg(feature = "indexmap_1")]
111impl<T, S> DuplicateInsertsLastWinsSet<T> for indexmap_1::IndexSet<T, S>
112where
113    T: Eq + Hash,
114    S: BuildHasher + Default,
115{
116    #[inline]
117    fn new(size_hint: Option<usize>) -> Self {
118        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
119    }
120
121    #[inline]
122    fn replace(&mut self, value: T) {
123        // Hashset already fulfils the contract
124        self.replace(value);
125    }
126}
127
128#[cfg(feature = "indexmap_2")]
129impl<T, S> DuplicateInsertsLastWinsSet<T> for indexmap_2::IndexSet<T, S>
130where
131    T: Eq + Hash,
132    S: BuildHasher + Default,
133{
134    #[inline]
135    fn new(size_hint: Option<usize>) -> Self {
136        Self::with_capacity_and_hasher(size_hint_cautious::<T>(size_hint), S::default())
137    }
138
139    #[inline]
140    fn replace(&mut self, value: T) {
141        // Hashset already fulfils the contract
142        self.replace(value);
143    }
144}
145
146impl<T> DuplicateInsertsLastWinsSet<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 replace(&mut self, value: T) {
157        // BTreeSet already fulfils the contract
158        self.replace(value);
159    }
160}