cl_aux/traits/
insert.rs

1#![allow(
2  // `_manage_hash` is also used by BTreeMap
3  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
36/// See [`Insert::insert`] for more information.
37pub trait Insert {
38  /// Error
39  type Error;
40  /// Input
41  type Input;
42
43  /// Inserts an `Input` element.
44  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/// ```rust
61/// let mut structure = cl_aux::doc_tests::b_tree_map();
62/// cl_aux::Insert::insert(&mut structure, (10, 100));
63/// assert_eq!(structure.iter().find(|(k, v)| **k == 10), Some((&10, &100)));
64/// ```
65#[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/// ```rust
80/// let mut structure = cl_aux::doc_tests::b_tree_set();
81/// cl_aux::Insert::insert(&mut structure, 10);
82/// assert_eq!(structure.iter().find(|&&e| e == 10), Some(&10));
83/// ```
84#[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/// ```rust
99/// let mut structure = cl_aux::doc_tests::hash_map();
100/// cl_aux::Insert::insert(&mut structure, (10, 100));
101/// assert_eq!(structure.iter().find(|(k, v)| **k == 10), Some((&10, &100)));
102/// ```
103#[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/// ```rust
119/// let mut structure = cl_aux::doc_tests::hash_set();
120/// cl_aux::Insert::insert(&mut structure, 10);
121/// assert_eq!(structure.iter().find(|&&e| e == 10), Some(&10));
122/// ```
123#[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
138/// ```rust
139/// let mut opt = None;
140/// cl_aux::Insert::insert(&mut opt, 3);
141/// assert_eq!(opt, Some(3));
142/// ```
143impl<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/// ```rust
159/// let mut structure = cl_aux::doc_tests::vec();
160/// cl_aux::Insert::insert(&mut structure, (0, 10));
161/// assert_eq!(structure.get(0), Some(&10));
162/// ```
163#[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/// ```rust
177/// let mut structure = cl_aux::doc_tests::array_vec();
178/// cl_aux::Insert::insert(&mut structure, (0, 10));
179/// assert_eq!(structure.get(0), Some(&10));
180/// ```
181#[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/// ```rust
195/// let mut structure = cl_aux::doc_tests::small_vec();
196/// cl_aux::Insert::insert(&mut structure, (0, 10));
197/// assert_eq!(structure.get(0), Some(&10));
198/// ```
199#[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/// ```rust
216/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
217/// cl_aux::Insert::insert(&mut structure, (0, 10));
218/// assert_eq!(structure.get(0), Some(&10));
219/// ```
220#[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/// ```rust
238/// let mut structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
239/// cl_aux::Insert::insert(&mut structure, (0, 10));
240/// assert_eq!(structure.get(0), Some(&10));
241/// ```
242#[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}