1#![allow(
2 clippy::map_entry
4)]
5
6macro_rules! _manage_hash {
7 ($hash:expr, $key:expr, $value:expr) => {{
8 if $hash.contains_key(&$key) {
9 Err(crate::Error::AlreadyExistingElement)
10 } else {
11 let _maybe_discarded = $hash.insert($key, $value);
12 Ok(())
13 }
14 }};
15}
16
17macro_rules! _manage_set {
18 ($set:expr, $value:expr) => {{
19 if $set.contains(&$value) {
20 Err(crate::Error::AlreadyExistingElement)
21 } else {
22 let _ = $set.insert($value);
23 Ok(())
24 }
25 }};
26}
27
28#[cfg(feature = "alloc")]
29use alloc::{
30 collections::{BTreeMap, BTreeSet},
31 vec::Vec,
32};
33#[cfg(feature = "std")]
34use std::collections::{HashMap, HashSet};
35
36pub trait Insert {
38 type Error;
40 type Input;
42
43 fn insert(&mut self, input: Self::Input) -> Result<(), Self::Error>;
45}
46
47impl<T> Insert for &mut T
48where
49 T: Insert,
50{
51 type Error = T::Error;
52 type Input = T::Input;
53
54 #[inline]
55 fn insert(&mut self, input: Self::Input) -> Result<(), Self::Error> {
56 (*self).insert(input)
57 }
58}
59
60#[cfg(feature = "alloc")]
66impl<K, V> Insert for BTreeMap<K, V>
67where
68 K: Ord,
69{
70 type Error = crate::Error;
71 type Input = (K, V);
72
73 #[inline]
74 fn insert(&mut self, (key, val): Self::Input) -> Result<(), Self::Error> {
75 _manage_hash!(self, key, val)
76 }
77}
78
79#[cfg(feature = "alloc")]
85impl<V> Insert for BTreeSet<V>
86where
87 V: Ord,
88{
89 type Error = crate::Error;
90 type Input = V;
91
92 #[inline]
93 fn insert(&mut self, input: Self::Input) -> Result<(), Self::Error> {
94 _manage_set!(self, input)
95 }
96}
97
98#[cfg(feature = "std")]
104impl<K, V, S> Insert for HashMap<K, V, S>
105where
106 K: Eq + core::hash::Hash,
107 S: core::hash::BuildHasher,
108{
109 type Error = crate::Error;
110 type Input = (K, V);
111
112 #[inline]
113 fn insert(&mut self, (k, v): Self::Input) -> Result<(), Self::Error> {
114 _manage_hash!(self, k, v)
115 }
116}
117
118#[cfg(feature = "std")]
124impl<V, S> Insert for HashSet<V, S>
125where
126 V: core::hash::Hash + Eq,
127 S: core::hash::BuildHasher,
128{
129 type Error = crate::Error;
130 type Input = V;
131
132 #[inline]
133 fn insert(&mut self, v: Self::Input) -> Result<(), Self::Error> {
134 _manage_set!(self, v)
135 }
136}
137
138impl<T> Insert for Option<T> {
144 type Error = crate::Error;
145 type Input = T;
146
147 #[inline]
148 fn insert(&mut self, input: Self::Input) -> Result<(), Self::Error> {
149 if self.is_some() {
150 Err(crate::Error::InsufficientCapacity(1))
151 } else {
152 *self = Some(input);
153 Ok(())
154 }
155 }
156}
157
158#[cfg(feature = "alloc")]
164impl<T> Insert for Vec<T> {
165 type Error = crate::Error;
166 type Input = (usize, T);
167
168 #[inline]
169 fn insert(&mut self, (idx, elem): Self::Input) -> Result<(), Self::Error> {
170 _check_indcs!(self, idx);
171 self.insert(idx, elem);
172 Ok(())
173 }
174}
175
176#[cfg(feature = "arrayvec")]
182impl<T, const N: usize> Insert for arrayvec::ArrayVec<T, N> {
183 type Error = crate::Error;
184 type Input = (usize, T);
185
186 #[inline]
187 fn insert(&mut self, (idx, elem): Self::Input) -> Result<(), Self::Error> {
188 _check_indcs!(self, idx);
189 self.insert(idx, elem);
190 Ok(())
191 }
192}
193
194#[cfg(feature = "smallvec")]
200impl<A> Insert for smallvec::SmallVec<A>
201where
202 A: smallvec::Array,
203{
204 type Error = crate::Error;
205 type Input = (usize, A::Item);
206
207 #[inline]
208 fn insert(&mut self, (idx, elem): Self::Input) -> Result<(), Self::Error> {
209 _check_indcs!(self, idx);
210 self.insert(idx, elem);
211 Ok(())
212 }
213}
214
215#[cfg(feature = "tinyvec")]
221impl<A> Insert for tinyvec::ArrayVec<A>
222where
223 A: tinyvec::Array,
224 A::Item: Default,
225{
226 type Error = crate::Error;
227 type Input = (usize, A::Item);
228
229 #[inline]
230 fn insert(&mut self, (idx, elem): Self::Input) -> Result<(), Self::Error> {
231 _check_indcs!(self, idx);
232 self.insert(idx, elem);
233 Ok(())
234 }
235}
236
237#[cfg(all(feature = "alloc", feature = "tinyvec"))]
243impl<A> Insert for tinyvec::TinyVec<A>
244where
245 A: tinyvec::Array,
246 A::Item: Default,
247{
248 type Error = crate::Error;
249 type Input = (usize, A::Item);
250
251 #[inline]
252 fn insert(&mut self, (idx, elem): Self::Input) -> Result<(), Self::Error> {
253 _check_indcs!(self, idx);
254 self.insert(idx, elem);
255 Ok(())
256 }
257}