serde_with/duplicate_key_impls/
first_value_wins.rs

1use crate::prelude::*;
2
3pub trait DuplicateInsertsFirstWinsMap<K, V> {
4    fn new(size_hint: Option<usize>) -> Self;
5
6    /// Insert the value into the map, if there is not already an existing value
7    fn insert(&mut self, key: K, value: V);
8}
9
10#[cfg(feature = "std")]
11impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for HashMap<K, V, S>
12where
13    K: Eq + Hash,
14    S: BuildHasher + Default,
15{
16    #[inline]
17    fn new(size_hint: Option<usize>) -> Self {
18        match size_hint {
19            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
20            None => Self::with_hasher(S::default()),
21        }
22    }
23
24    #[inline]
25    fn insert(&mut self, key: K, value: V) {
26        use std::collections::hash_map::Entry;
27
28        match self.entry(key) {
29            // we want to keep the first value, so do nothing
30            Entry::Occupied(_) => {}
31            Entry::Vacant(vacant) => {
32                vacant.insert(value);
33            }
34        }
35    }
36}
37
38#[cfg(feature = "hashbrown_0_14")]
39impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for hashbrown_0_14::HashMap<K, V, S>
40where
41    K: Eq + Hash,
42    S: BuildHasher + Default,
43{
44    #[inline]
45    fn new(size_hint: Option<usize>) -> Self {
46        match size_hint {
47            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
48            None => Self::with_hasher(S::default()),
49        }
50    }
51
52    #[inline]
53    fn insert(&mut self, key: K, value: V) {
54        use hashbrown_0_14::hash_map::Entry;
55
56        match self.entry(key) {
57            // we want to keep the first value, so do nothing
58            Entry::Occupied(_) => {}
59            Entry::Vacant(vacant) => {
60                vacant.insert(value);
61            }
62        }
63    }
64}
65
66#[cfg(feature = "hashbrown_0_15")]
67impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for hashbrown_0_15::HashMap<K, V, S>
68where
69    K: Eq + Hash,
70    S: BuildHasher + Default,
71{
72    #[inline]
73    fn new(size_hint: Option<usize>) -> Self {
74        match size_hint {
75            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
76            None => Self::with_hasher(S::default()),
77        }
78    }
79
80    #[inline]
81    fn insert(&mut self, key: K, value: V) {
82        use hashbrown_0_15::hash_map::Entry;
83
84        match self.entry(key) {
85            // we want to keep the first value, so do nothing
86            Entry::Occupied(_) => {}
87            Entry::Vacant(vacant) => {
88                vacant.insert(value);
89            }
90        }
91    }
92}
93
94#[cfg(feature = "indexmap_1")]
95impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for indexmap_1::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) {
110        use indexmap_1::map::Entry;
111
112        match self.entry(key) {
113            // we want to keep the first value, so do nothing
114            Entry::Occupied(_) => {}
115            Entry::Vacant(vacant) => {
116                vacant.insert(value);
117            }
118        }
119    }
120}
121
122#[cfg(feature = "indexmap_2")]
123impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for indexmap_2::IndexMap<K, V, S>
124where
125    K: Eq + Hash,
126    S: BuildHasher + Default,
127{
128    #[inline]
129    fn new(size_hint: Option<usize>) -> Self {
130        match size_hint {
131            Some(size) => Self::with_capacity_and_hasher(size, S::default()),
132            None => Self::with_hasher(S::default()),
133        }
134    }
135
136    #[inline]
137    fn insert(&mut self, key: K, value: V) {
138        use indexmap_2::map::Entry;
139
140        match self.entry(key) {
141            // we want to keep the first value, so do nothing
142            Entry::Occupied(_) => {}
143            Entry::Vacant(vacant) => {
144                vacant.insert(value);
145            }
146        }
147    }
148}
149
150impl<K, V> DuplicateInsertsFirstWinsMap<K, V> for BTreeMap<K, V>
151where
152    K: Ord,
153{
154    #[inline]
155    fn new(_size_hint: Option<usize>) -> Self {
156        Self::new()
157    }
158
159    #[inline]
160    fn insert(&mut self, key: K, value: V) {
161        use alloc::collections::btree_map::Entry;
162
163        match self.entry(key) {
164            // we want to keep the first value, so do nothing
165            Entry::Occupied(_) => {}
166            Entry::Vacant(vacant) => {
167                vacant.insert(value);
168            }
169        }
170    }
171}