1#[cfg(feature = "alloc")]
2use alloc::{string::String, vec::Vec};
3
4pub trait Push<I> {
6 type Error;
8
9 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
25impl<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
39impl<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#[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#[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#[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#[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#[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#[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#[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#[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#[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}