cl_aux/traits/
push.rs

1#[cfg(feature = "alloc")]
2use alloc::{string::String, vec::Vec};
3
4/// See [`Push::push`] for more information.
5pub trait Push<I> {
6  /// Error
7  type Error;
8
9  /// Pushes an element, increasing the storage length.
10  fn push(&mut self, input: I) -> Result<(), Self::Error>;
11}
12
13impl<I, T> Push<I> for &mut T
14where
15  T: Push<I>,
16{
17  type Error = T::Error;
18
19  #[inline]
20  fn push(&mut self, input: I) -> Result<(), Self::Error> {
21    (*self).push(input)
22  }
23}
24
25/// ```rust
26/// let mut opt = None;
27/// cl_aux::Push::push(&mut opt, 3);
28/// assert_eq!(opt, Some(3));
29/// ```
30impl<T> Push<T> for () {
31  type Error = crate::Error;
32
33  #[inline]
34  fn push(&mut self, _: T) -> Result<(), Self::Error> {
35    Ok(())
36  }
37}
38
39/// ```rust
40/// let mut opt = None;
41/// cl_aux::Push::push(&mut opt, 3);
42/// assert_eq!(opt, Some(3));
43/// ```
44impl<T> Push<T> for Option<T> {
45  type Error = crate::Error;
46
47  #[inline]
48  fn push(&mut self, input: T) -> Result<(), Self::Error> {
49    if self.is_some() {
50      Err(crate::Error::InsufficientCapacity(1))
51    } else {
52      *self = Some(input);
53      Ok(())
54    }
55  }
56}
57
58/// ```rust
59/// let mut structure = cl_aux::doc_tests::string();
60/// cl_aux::Push::push(&mut structure, '!');
61/// assert_eq!(structure.as_str(), "Hello!");
62/// ```
63#[cfg(feature = "alloc")]
64impl Push<char> for String {
65  type Error = crate::Error;
66
67  #[inline]
68  fn push(&mut self, input: char) -> Result<(), Self::Error> {
69    _check_capacity!(self);
70    self.push(input);
71    Ok(())
72  }
73}
74
75/// ```rust
76/// let mut structure = cl_aux::doc_tests::string();
77/// cl_aux::Push::push(&mut structure, "!!");
78/// assert_eq!(structure.as_str(), "Hello!!");
79/// ```
80#[cfg(feature = "alloc")]
81impl<'input> Push<&'input str> for String {
82  type Error = crate::Error;
83
84  #[inline]
85  fn push(&mut self, input: &'input str) -> Result<(), Self::Error> {
86    _check_capacity!(self);
87    self.push_str(input);
88    Ok(())
89  }
90}
91
92/// ```rust
93/// let mut structure = cl_aux::doc_tests::vec();
94/// cl_aux::Push::push(&mut structure, 20);
95/// assert_eq!(structure.get(3), Some(&20));
96/// ```
97#[cfg(feature = "alloc")]
98impl<T> Push<T> for Vec<T> {
99  type Error = crate::Error;
100
101  #[inline]
102  fn push(&mut self, input: T) -> Result<(), Self::Error> {
103    _check_capacity!(self);
104    self.push(input);
105    Ok(())
106  }
107}
108
109/// ```rust
110/// let mut structure = cl_aux::doc_tests::array_string();
111/// cl_aux::Push::push(&mut structure, '!');
112/// assert_eq!(structure.as_str(), "Hello!");
113/// ```
114#[cfg(feature = "arrayvec")]
115impl<const N: usize> Push<char> for arrayvec::ArrayString<N> {
116  type Error = crate::Error;
117
118  #[inline]
119  fn push(&mut self, input: char) -> Result<(), Self::Error> {
120    _check_capacity!(self);
121    self.push(input);
122    Ok(())
123  }
124}
125
126/// ```rust
127/// let mut structure = cl_aux::doc_tests::array_string();
128/// cl_aux::Push::push(&mut structure, "!!");
129/// assert_eq!(structure.as_str(), "Hello!!");
130/// ```
131#[cfg(feature = "arrayvec")]
132impl<'input, const N: usize> Push<&'input str> for arrayvec::ArrayString<N> {
133  type Error = crate::Error;
134
135  #[inline]
136  fn push(&mut self, input: &'input str) -> Result<(), Self::Error> {
137    _check_capacity!(self);
138    self.push_str(input);
139    Ok(())
140  }
141}
142
143/// ```rust
144/// let mut structure = cl_aux::doc_tests::array_vec();
145/// cl_aux::Push::push(&mut structure, 20);
146/// assert_eq!(structure.get(3), Some(&20));
147/// ```
148#[cfg(feature = "arrayvec")]
149impl<T, const N: usize> Push<T> for arrayvec::ArrayVec<T, N> {
150  type Error = crate::Error;
151
152  #[inline]
153  fn push(&mut self, input: T) -> Result<(), Self::Error> {
154    _check_capacity!(self);
155    self.push(input);
156    Ok(())
157  }
158}
159
160/// ```rust
161/// let mut structure = cl_aux::doc_tests::small_vec();
162/// cl_aux::Push::push(&mut structure, 20);
163/// assert_eq!(structure.get(3), Some(&20));
164/// ```
165#[cfg(feature = "smallvec")]
166impl<A> Push<A::Item> for smallvec::SmallVec<A>
167where
168  A: smallvec::Array,
169{
170  type Error = crate::Error;
171
172  #[inline]
173  fn push(&mut self, input: A::Item) -> Result<(), Self::Error> {
174    _check_capacity!(self);
175    self.push(input);
176    Ok(())
177  }
178}
179
180/// ```rust
181/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
182/// cl_aux::Push::push(&mut structure, 20);
183/// assert_eq!(structure.get(3), Some(&20));
184/// ```
185#[cfg(feature = "tinyvec")]
186impl<A> Push<A::Item> for tinyvec::ArrayVec<A>
187where
188  A: tinyvec::Array,
189  A::Item: Default,
190{
191  type Error = crate::Error;
192
193  #[inline]
194  fn push(&mut self, input: A::Item) -> Result<(), Self::Error> {
195    _check_capacity!(self);
196    self.push(input);
197    Ok(())
198  }
199}
200
201/// ```rust
202/// let mut structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
203/// cl_aux::Push::push(&mut structure, 20);
204/// assert_eq!(structure.get(3), Some(&20));
205/// ```
206#[cfg(all(feature = "alloc", feature = "tinyvec"))]
207impl<A> Push<A::Item> for tinyvec::TinyVec<A>
208where
209  A: tinyvec::Array,
210  A::Item: Default,
211{
212  type Error = crate::Error;
213
214  #[inline]
215  fn push(&mut self, input: A::Item) -> Result<(), Self::Error> {
216    _check_capacity!(self);
217    self.push(input);
218    Ok(())
219  }
220}