1pub trait StorageProvider<T> {
9 type StorageType: Storage<T>;
11
12 fn new() -> Self;
14
15 fn new_storage(&mut self) -> Self::StorageType;
17
18 fn storage_with_capacity(&mut self, capacity: usize) -> Self::StorageType;
21}
22
23pub trait Storage<T>: Clone {
25 type Provider: StorageProvider<T, StorageType = Self>;
27
28 fn clear(&mut self);
30
31 fn push(&mut self, value: T);
33
34 fn pop(&mut self) -> Option<T>;
36
37 fn as_slice(&self) -> &[T];
39
40 fn as_mut_slice(&mut self) -> &mut [T];
42
43 #[inline]
46 fn len(&self) -> usize {
47 self.as_slice().len()
48 }
49
50 fn capacity(&self) -> usize;
52}
53
54#[cfg(feature = "alloc")]
55pub mod vec {
56 extern crate alloc;
58 use alloc::vec::Vec;
59
60 use super::{Storage, StorageProvider};
61
62 pub struct VecStorage;
64
65 impl<T> StorageProvider<T> for VecStorage
66 where
67 T: Clone,
68 {
69 type StorageType = Vec<T>;
70 #[inline]
71 fn new() -> Self {
72 Self
73 }
74
75 #[inline]
76 fn new_storage(&mut self) -> Self::StorageType {
77 Self::StorageType::new()
78 }
79
80 #[inline]
81 fn storage_with_capacity(&mut self, capacity: usize) -> Self::StorageType {
82 Self::StorageType::with_capacity(capacity)
83 }
84 }
85
86 impl<T> Storage<T> for Vec<T>
87 where
88 T: Clone,
89 {
90 type Provider = VecStorage;
91
92 #[inline]
93 fn clear(&mut self) {
94 self.clear();
95 }
96
97 #[inline]
98 fn push(&mut self, value: T) {
99 self.push(value);
100 }
101
102 #[inline]
103 fn pop(&mut self) -> Option<T> {
104 self.pop()
105 }
106
107 #[inline]
108 fn len(&self) -> usize {
109 self.len()
110 }
111
112 #[inline]
113 fn capacity(&self) -> usize {
114 self.capacity()
115 }
116
117 #[inline]
118 fn as_slice(&self) -> &[T] {
119 self.as_slice()
120 }
121
122 #[inline]
123 fn as_mut_slice(&mut self) -> &mut [T] {
124 self.as_mut_slice()
125 }
126 }
127}
128
129#[cfg(feature = "tinyvec")]
130pub mod tinyvec {
131 use super::{Storage, StorageProvider};
133 #[cfg(feature = "alloc")]
134 use alloc::vec::Vec;
135 #[cfg(feature = "alloc")]
136 pub use tinyvec::TinyVec;
137 pub use tinyvec::{Array, ArrayVec};
138
139 pub struct TinyVecArrayStorage<const CAP: usize>;
141
142 impl<const CAP: usize, T> StorageProvider<T> for TinyVecArrayStorage<CAP>
143 where
144 T: Clone,
145 [T; CAP]: Array<Item = T>,
146 {
147 type StorageType = ArrayVec<[T; CAP]>;
148 #[inline]
149 fn new() -> Self {
150 Self
151 }
152
153 #[inline]
154 fn new_storage(&mut self) -> Self::StorageType {
155 Self::StorageType::new()
156 }
157
158 #[inline]
159 fn storage_with_capacity(&mut self, capacity: usize) -> Self::StorageType {
160 if capacity > <[T; CAP] as Array>::CAPACITY {
161 panic!(
162 "Requested capacity of {} exceeds maximum of {}",
163 capacity,
164 <[T; CAP] as Array>::CAPACITY
165 )
166 }
167 Self::StorageType::new()
168 }
169 }
170
171 impl<const CAP: usize, T> Storage<T> for ArrayVec<[T; CAP]>
172 where
173 T: Clone,
174 [T; CAP]: Array<Item = T>,
175 {
176 type Provider = TinyVecArrayStorage<CAP>;
177
178 #[inline]
179 fn clear(&mut self) {
180 self.clear();
181 }
182
183 #[inline]
184 fn push(&mut self, value: T) {
185 self.push(value);
186 }
187
188 #[inline]
189 fn pop(&mut self) -> Option<T> {
190 self.pop()
191 }
192
193 #[inline]
194 fn len(&self) -> usize {
195 self.len()
196 }
197
198 #[inline]
199 fn capacity(&self) -> usize {
200 self.capacity()
201 }
202
203 #[inline]
204 fn as_slice(&self) -> &[T] {
205 self.as_slice()
206 }
207
208 #[inline]
209 fn as_mut_slice(&mut self) -> &mut [T] {
210 self.as_mut_slice()
211 }
212 }
213
214 #[cfg(feature = "alloc")]
215 pub struct TinyVecStorage<const CAP: usize>;
217
218 #[cfg(feature = "alloc")]
219 impl<const CAP: usize, T> StorageProvider<T> for TinyVecStorage<CAP>
220 where
221 T: Clone,
222 [T; CAP]: Array<Item = T>,
223 {
224 type StorageType = TinyVec<[T; CAP]>;
225 #[inline]
226 fn new() -> Self {
227 Self
228 }
229
230 #[inline]
231 fn new_storage(&mut self) -> Self::StorageType {
232 Self::StorageType::new()
233 }
234
235 #[inline]
236 fn storage_with_capacity(&mut self, capacity: usize) -> Self::StorageType {
237 if capacity <= <[T; CAP] as Array>::CAPACITY {
238 TinyVec::Inline(ArrayVec::new())
239 } else {
240 TinyVec::Heap(Vec::with_capacity(capacity))
241 }
242 }
243 }
244
245 #[cfg(feature = "alloc")]
246 impl<const CAP: usize, T> Storage<T> for TinyVec<[T; CAP]>
247 where
248 T: Clone,
249 [T; CAP]: Array<Item = T>,
250 {
251 type Provider = TinyVecStorage<CAP>;
252
253 #[inline]
254 fn clear(&mut self) {
255 self.clear();
256 }
257
258 #[inline]
259 fn push(&mut self, value: T) {
260 self.push(value);
261 }
262
263 #[inline]
264 fn pop(&mut self) -> Option<T> {
265 self.pop()
266 }
267
268 #[inline]
269 fn len(&self) -> usize {
270 self.len()
271 }
272
273 #[inline]
274 fn capacity(&self) -> usize {
275 self.capacity()
276 }
277
278 #[inline]
279 fn as_slice(&self) -> &[T] {
280 self.as_slice()
281 }
282
283 #[inline]
284 fn as_mut_slice(&mut self) -> &mut [T] {
285 self.as_mut_slice()
286 }
287 }
288}
289
290#[cfg(feature = "arrayvec")]
291pub mod arrayvec {
292 use super::{Storage, StorageProvider};
294 pub use arrayvec::ArrayVec;
295
296 pub struct ArrayVecStorage<const CAP: usize>;
298
299 impl<const CAP: usize, T> StorageProvider<T> for ArrayVecStorage<CAP>
300 where
301 T: Clone,
302 {
303 type StorageType = ArrayVec<T, CAP>;
304 #[inline]
305 fn new() -> Self {
306 Self
307 }
308
309 #[inline]
310 fn new_storage(&mut self) -> Self::StorageType {
311 Self::StorageType::new()
312 }
313
314 #[inline]
315 fn storage_with_capacity(&mut self, capacity: usize) -> Self::StorageType {
316 if capacity > CAP {
317 panic!(
318 "Requested capacity of {} exceeds maximum of {}",
319 capacity, CAP
320 )
321 }
322 Self::StorageType::new()
323 }
324 }
325
326 impl<const CAP: usize, T> Storage<T> for ArrayVec<T, CAP>
327 where
328 T: Clone,
329 {
330 type Provider = ArrayVecStorage<CAP>;
331
332 #[inline]
333 fn clear(&mut self) {
334 self.clear();
335 }
336
337 #[inline]
338 fn push(&mut self, value: T) {
339 self.push(value);
340 }
341
342 #[inline]
343 fn pop(&mut self) -> Option<T> {
344 self.pop()
345 }
346
347 #[inline]
348 fn len(&self) -> usize {
349 self.len()
350 }
351
352 #[inline]
353 fn capacity(&self) -> usize {
354 self.capacity()
355 }
356
357 #[inline]
358 fn as_slice(&self) -> &[T] {
359 self.as_slice()
360 }
361
362 #[inline]
363 fn as_mut_slice(&mut self) -> &mut [T] {
364 self.as_mut_slice()
365 }
366 }
367}
368
369#[cfg(feature = "smallvec")]
370pub mod smallvec {
371 use super::{Storage, StorageProvider};
373 pub use smallvec::{Array, SmallVec};
374
375 pub struct SmallVecStorage<const CAP: usize>;
377
378 impl<const CAP: usize, T> StorageProvider<T> for SmallVecStorage<CAP>
379 where
380 T: Clone,
381 [T; CAP]: Array<Item = T>,
382 {
383 type StorageType = SmallVec<[T; CAP]>;
384 #[inline]
385 fn new() -> Self {
386 Self
387 }
388
389 #[inline]
390 fn new_storage(&mut self) -> Self::StorageType {
391 Self::StorageType::new()
392 }
393
394 #[inline]
395 fn storage_with_capacity(&mut self, capacity: usize) -> Self::StorageType {
396 SmallVec::with_capacity(capacity)
397 }
398 }
399
400 impl<const CAP: usize, T> Storage<T> for SmallVec<[T; CAP]>
401 where
402 T: Clone,
403 [T; CAP]: Array<Item = T>,
404 {
405 type Provider = SmallVecStorage<CAP>;
406
407 #[inline]
408 fn clear(&mut self) {
409 self.clear();
410 }
411
412 #[inline]
413 fn push(&mut self, value: T) {
414 self.push(value);
415 }
416
417 #[inline]
418 fn pop(&mut self) -> Option<T> {
419 self.pop()
420 }
421
422 #[inline]
423 fn len(&self) -> usize {
424 self.len()
425 }
426
427 #[inline]
428 fn capacity(&self) -> usize {
429 self.capacity()
430 }
431
432 #[inline]
433 fn as_slice(&self) -> &[T] {
434 self.as_slice()
435 }
436
437 #[inline]
438 fn as_mut_slice(&mut self) -> &mut [T] {
439 self.as_mut_slice()
440 }
441 }
442}