1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#![allow(unused_lifetimes)]

#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};

/// See [Push::push] for more information.
pub trait Push<I> {
  /// Error
  type Error;
  /// Output
  type Output;

  /// Pushes an element, increasing the storage length.
  fn push(&mut self, input: I) -> Result<Self::Output, Self::Error>;
}

/// ```rust
/// let mut opt = None;
/// cl_aux::Push::push(&mut opt, 3);
/// assert_eq!(opt, Some(3));
/// ```
impl<T> Push<T> for Option<T> {
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: T) -> Result<Self::Output, Self::Error> {
    if self.is_some() {
      Err(crate::Error::InsufficientCapacity(stringify!(self), 1))
    } else {
      *self = Some(input);
      Ok(())
    }
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::string();
/// cl_aux::Push::push(&mut structure, '!');
/// assert_eq!(structure.as_str(), "Hello!");
/// ```
#[cfg(feature = "alloc")]
impl Push<char> for String {
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: char) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::string();
/// cl_aux::Push::push(&mut structure, "!!");
/// assert_eq!(structure.as_str(), "Hello!!");
/// ```
#[cfg(feature = "alloc")]
impl<'input> Push<&'input str> for String {
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: &'input str) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push_str(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::vec();
/// cl_aux::Push::push(&mut structure, 20);
/// assert_eq!(structure.get(3), Some(&20));
/// ```
#[cfg(feature = "alloc")]
impl<T> Push<T> for Vec<T> {
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: T) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::array_string();
/// cl_aux::Push::push(&mut structure, '!');
/// assert_eq!(structure.as_str(), "Hello!");
/// ```
#[cfg(feature = "arrayvec")]
impl<const N: usize> Push<char> for arrayvec::ArrayString<N> {
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: char) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::array_string();
/// cl_aux::Push::push(&mut structure, "!!");
/// assert_eq!(structure.as_str(), "Hello!!");
/// ```
#[cfg(feature = "arrayvec")]
impl<'input, const N: usize> Push<&'input str> for arrayvec::ArrayString<N> {
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: &'input str) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push_str(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::array_vec();
/// cl_aux::Push::push(&mut structure, 20);
/// assert_eq!(structure.get(3), Some(&20));
/// ```
#[cfg(feature = "arrayvec")]
impl<T, const N: usize> Push<T> for arrayvec::ArrayVec<T, N> {
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: T) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::small_vec();
/// cl_aux::Push::push(&mut structure, 20);
/// assert_eq!(structure.get(3), Some(&20));
/// ```
#[cfg(feature = "smallvec")]
impl<A> Push<A::Item> for smallvec::SmallVec<A>
where
  A: smallvec::Array,
{
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: A::Item) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::static_vec();
/// cl_aux::Push::push(&mut structure, 20);
/// assert_eq!(structure.get(3), Some(&20));
/// ```
#[cfg(feature = "staticvec")]
impl<T, const N: usize> Push<T> for staticvec::StaticVec<T, N> {
  type Error = T;
  type Output = ();

  #[inline]
  fn push(&mut self, input: T) -> Result<Self::Output, Self::Error> {
    self.try_push(input).map_err(|e| e.into_value())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
/// cl_aux::Push::push(&mut structure, 20);
/// assert_eq!(structure.get(3), Some(&20));
/// ```
#[cfg(feature = "tinyvec")]
impl<A> Push<A::Item> for tinyvec::ArrayVec<A>
where
  A: tinyvec::Array,
  A::Item: Default,
{
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: A::Item) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push(input);
    Ok(())
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::tiny_vec();
/// cl_aux::Push::push(&mut structure, 20);
/// assert_eq!(structure.get(3), Some(&20));
/// ```
#[cfg(feature = "tinyvec")]
impl<A> Push<A::Item> for tinyvec::TinyVec<A>
where
  A: tinyvec::Array,
  A::Item: Default,
{
  type Error = crate::Error;
  type Output = ();

  #[inline]
  fn push(&mut self, input: A::Item) -> Result<Self::Output, Self::Error> {
    _check_capacity!(self);
    self.push(input);
    Ok(())
  }
}