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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use super::vec::Vec;
use crate::alloc::{AllocationError, DefaultAllocator, IAlloc};
use crate::num::NonMaxUsize;
use crate::result::OkGuard;
use crate::{IDeterminantProvider, IStable};
mod seal {
    #[crate::stabby]
    pub struct Single<T, Alloc> {
        pub value: T,
        pub alloc: Alloc,
    }
}
pub(crate) use seal::*;
/// A vector that doesn't need to allocate for its first value.
///
/// Once a second value is pushed, or if greater capacity is reserved,
/// the allocated vector will be used regardless of how the vector's
/// number of elements evolves.
#[crate::stabby]
pub struct SingleOrVec<T: IStable, Alloc: IAlloc + IStable = DefaultAllocator>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    inner: crate::Result<Single<T, Alloc>, Vec<T, Alloc>>,
}

#[cfg(feature = "libc")]
impl<T: IStable> SingleOrVec<T>
where
    Single<T, DefaultAllocator>: IDeterminantProvider<Vec<T, DefaultAllocator>>,
    Vec<T, DefaultAllocator>: IStable,
    crate::Result<Single<T, DefaultAllocator>, Vec<T, DefaultAllocator>>: IStable,
{
    /// Constructs a new vector. This doesn't actually allocate.
    pub fn new() -> Self {
        Self::new_in(DefaultAllocator::new())
    }
}

impl<T: IStable, Alloc: IAlloc + IStable + Default> Default for SingleOrVec<T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn default() -> Self {
        Self::new_in(Alloc::default())
    }
}
impl<T: IStable, Alloc: IAlloc + IStable> SingleOrVec<T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    /// Constructs a new vector in `alloc`. This doesn't actually allocate.
    pub fn new_in(alloc: Alloc) -> Self {
        Self {
            inner: crate::Result::Err(Vec::new_in(alloc)),
        }
    }
    /// Constructs a new vector in `alloc`, allocating sufficient space for `capacity` elements.
    ///
    /// # Panics
    /// If the allocator failed to provide a large enough allocation.
    pub fn with_capacity_in(capacity: usize, alloc: Alloc) -> Self {
        let mut this = Self::new_in(alloc);
        this.reserve(capacity);
        this
    }
    /// Constructs a new vector, allocating sufficient space for `capacity` elements.
    ///
    /// # Panics
    /// If the allocator failed to provide a large enough allocation.
    pub fn with_capacity(capacity: usize) -> Self
    where
        Alloc: Default,
    {
        Self::with_capacity_in(capacity, Alloc::default())
    }
    /// Constructs a new vector in `alloc`, allocating sufficient space for `capacity` elements.
    /// # Errors
    /// Returns an [`AllocationError`] if the allocator couldn't provide a sufficient allocation.
    pub fn try_with_capacity_in(capacity: usize, alloc: Alloc) -> Result<Self, Alloc> {
        Vec::try_with_capacity_in(capacity, alloc).map(|vec| Self {
            inner: crate::Result::Err(vec),
        })
    }
    /// Constructs a new vector, allocating sufficient space for `capacity` elements.
    /// # Errors
    /// Returns an [`AllocationError`] if the allocator couldn't provide a sufficient allocation.
    pub fn try_with_capacity(capacity: usize) -> Result<Self, Alloc>
    where
        Alloc: Default,
        Self: IDeterminantProvider<AllocationError>,
    {
        Self::try_with_capacity_in(capacity, Alloc::default())
    }
    /// Returns the number of elements in the vector.
    pub fn len(&self) -> usize {
        self.inner.match_ref(|_| 1, |vec| vec.len())
    }
    /// Returns `true` if the vector is empty.
    pub fn is_empty(&self) -> bool {
        self.inner.match_ref(|_| false, |vec| vec.is_empty())
    }
    /// Adds `value` at the end of `self`.
    /// # Panics
    /// This function panics if the vector tried to grow due to
    /// being full, and the allocator failed to provide a new allocation.
    pub fn push(&mut self, value: T) {
        if self.try_push(value).is_err() {
            panic!("Failed to push because reallocation failed.")
        }
    }
    /// Adds `value` at the end of `self`.
    ///
    /// # Errors
    /// This function gives back the `value` if the vector tried to grow due to
    /// being full, and the allocator failed to provide a new allocation.
    ///
    /// `self` is still valid should that happen.
    pub fn try_push(&mut self, item: T) -> Result<(), T> {
        // Safety: either `this` or `*self` MUST be leaked to prevent double-frees.
        let this = unsafe { core::ptr::read(self) };
        // Safety: Any path that returns `Err` MUST leak the contents of the second argument.
        let this = this.inner.match_owned_ctx(
            item,
            |item, single| {
                // either `inner` must be leaked and overwritten by the new owner of `value` and `alloc`,
                // or these two must be leaked to prevent double frees.
                let Single { value, alloc } = single;
                match Vec::try_with_capacity_in(8, alloc) {
                    Ok(mut vec) => {
                        vec.push(value);
                        vec.push(item);
                        Ok(crate::Result::Err(vec))
                    }
                    Err(alloc) => {
                        // Safety: leak both `value` and `alloc` since `*self` won't be leaked
                        core::mem::forget((value, alloc));
                        Err(item)
                    }
                }
            },
            |item, mut vec| {
                if vec.capacity() == 0 {
                    unsafe {
                        let alloc = core::ptr::read(&vec.inner.alloc);
                        core::mem::forget(vec);
                        Ok(crate::Result::Ok(Single { value: item, alloc }))
                    }
                } else {
                    match vec.try_push(item) {
                        Ok(()) => Ok(crate::Result::Err(vec)),
                        Err(item) => {
                            // Safety: `vec` since `*self` won't be leaked
                            core::mem::forget(vec);
                            Err(item)
                        }
                    }
                }
            },
        );
        match this {
            Ok(inner) => unsafe {
                // Safety: this leaks `*self`, preventing it from being unduely destroyed
                core::ptr::write(self, Self { inner });
                Ok(())
            },
            Err(item) => Err(item),
        }
    }
    /// The total capacity of the vector.
    pub fn capacity(&self) -> usize {
        self.inner.match_ref(|_| 1, |vec| vec.capacity())
    }
    /// The remaining number of elements that can be pushed before reallocating.
    pub fn remaining_capacity(&self) -> usize {
        self.inner.match_ref(|_| 0, |vec| vec.remaining_capacity())
    }
    /// Ensures that `additional` more elements can be pushed on `self` without reallocating.
    ///
    /// This may reallocate once to provide this guarantee.
    ///
    /// # Panics
    /// This function panics if the allocator failed to provide an appropriate allocation.
    pub fn reserve(&mut self, additional: usize) {
        self.try_reserve(additional).unwrap();
    }
    /// Ensures that `additional` more elements can be pushed on `self` without reallocating.
    ///
    /// This may reallocate once to provide this guarantee.
    ///
    /// # Errors
    /// Returns Ok(new_capacity) if succesful (including if no reallocation was needed),
    /// otherwise returns Err(AllocationError)
    pub fn try_reserve(&mut self, additional: usize) -> Result<NonMaxUsize, AllocationError> {
        let inner = &mut self.inner as *mut _;
        self.inner.match_mut(
            |value| unsafe {
                let new_capacity = 1 + additional;
                // either `inner` must be leaked and overwritten by the new owner of `value` and `alloc`,
                // or these two must be leaked to prevent double frees.
                let Single { value, alloc } = core::ptr::read(&*value);
                match Vec::try_with_capacity_in(new_capacity, alloc) {
                    Ok(mut vec) => {
                        vec.push(value);
                        // overwrite `inner` with `value` and `alloc` with their new owner without freeing `inner`.
                        core::ptr::write(inner, crate::Result::Err(vec));
                        NonMaxUsize::new(new_capacity).ok_or(AllocationError())
                    }
                    Err(alloc) => {
                        // leak both `value` and `alloc` since `inner` can't be overwritten
                        core::mem::drop((value, alloc));
                        Err(AllocationError())
                    }
                }
            },
            |mut vec| vec.try_reserve(additional),
        )
    }
    /// Removes all elements from `self` from the `len`th onward.
    ///
    /// Does nothing if `self.len() <= len`
    pub fn truncate(&mut self, len: usize) {
        if self.len() <= len {
            return;
        }
        let inner = &mut self.inner as *mut _;
        self.inner.match_mut(
            |value| unsafe {
                let Single { value, alloc } = core::ptr::read(&*value);
                core::mem::drop(value); // drop `value` to prevent leaking it since we'll overwrite `inner` with something that doesn't own it
                                        // overwrite `inner` with the new owner of `alloc`
                core::ptr::write(inner, crate::Result::Err(Vec::new_in(alloc)))
            },
            |mut vec| vec.truncate(len),
        )
    }
    /// Returns a slice of the elements in the vector.
    pub fn as_slice(&self) -> &[T] {
        self.inner.match_ref(
            |value| core::slice::from_ref(&value.value),
            |vec| vec.as_slice(),
        )
    }
    /// Returns a mutable slice of the elements in the vector.
    pub fn as_slice_mut(&mut self) -> SliceGuardMut<T, Alloc> {
        self.inner.match_mut(
            |value| SliceGuardMut { inner: Ok(value) },
            |mut vec| SliceGuardMut {
                inner: Err(unsafe { core::mem::transmute(vec.as_slice_mut()) }),
            },
        )
    }
    // pub fn iter(&self) -> core::slice::Iter<'_, T> {
    //     self.into_iter()
    // }
    // pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, T> {
    //     self.into_iter()
    // }
}

/// A mutable accessor to [`SingleOrVec`]'s inner slice.
///
/// Failing to drop this guard may cause Undefined Behaviour
pub struct SliceGuardMut<'a, T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
    Alloc: IAlloc,
{
    #[allow(clippy::type_complexity)]
    inner: Result<OkGuard<'a, Single<T, Alloc>, Vec<T, Alloc>>, &'a mut [T]>,
}
impl<'a, T, Alloc> core::ops::Deref for SliceGuardMut<'a, T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
    Alloc: IAlloc,
{
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        match &self.inner {
            Ok(v) => core::slice::from_ref(&v.value),
            Err(v) => v,
        }
    }
}
impl<'a, T, Alloc> core::ops::DerefMut for SliceGuardMut<'a, T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
    Alloc: IAlloc,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        match &mut self.inner {
            Ok(v) => core::slice::from_mut(&mut v.value),
            Err(v) => v,
        }
    }
}

impl<T: Clone, Alloc: IAlloc + Clone> Clone for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn clone(&self) -> Self {
        self.inner.match_ref(
            |Single { value, alloc }| Self {
                inner: crate::Result::Ok(Single {
                    value: value.clone(),
                    alloc: alloc.clone(),
                }),
            },
            |vec| Self {
                inner: crate::Result::Err(vec.clone()),
            },
        )
    }
}
impl<T: PartialEq, Alloc: IAlloc, Rhs: AsRef<[T]>> PartialEq<Rhs> for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn eq(&self, other: &Rhs) -> bool {
        self.as_slice() == other.as_ref()
    }
}
impl<T: Eq, Alloc: IAlloc> Eq for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
}
impl<T: PartialOrd, Alloc: IAlloc, Rhs: AsRef<[T]>> PartialOrd<Rhs> for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn partial_cmp(&self, other: &Rhs) -> Option<core::cmp::Ordering> {
        self.as_slice().partial_cmp(other.as_ref())
    }
}
impl<T: Ord, Alloc: IAlloc> Ord for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}
impl<T, Alloc: IAlloc> core::ops::Deref for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}
impl<T, Alloc: IAlloc> core::convert::AsRef<[T]> for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn as_ref(&self) -> &[T] {
        self.as_slice()
    }
}
impl<T, Alloc: IAlloc> core::iter::Extend<T> for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
        let iter = iter.into_iter();
        let min = iter.size_hint().0;
        self.reserve(min);
        for item in iter {
            self.push(item);
        }
    }
}

impl<'a, T, Alloc: IAlloc> IntoIterator for &'a SingleOrVec<T, Alloc>
where
    T: IStable + 'a,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    type Item = &'a T;
    type IntoIter = core::slice::Iter<'a, T>;
    fn into_iter(self) -> Self::IntoIter {
        self.as_slice().iter()
    }
}
impl<'a, T, Alloc: IAlloc> IntoIterator for &'a mut SingleOrVec<T, Alloc>
where
    T: IStable + 'a,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    type Item = &'a mut T;
    type IntoIter = IterMut<'a, T, Alloc>;
    fn into_iter(self) -> Self::IntoIter {
        let inner = self.as_slice_mut();
        IterMut {
            start: 0,
            end: inner.len(),
            inner,
        }
    }
}

/// An iterator over a [`SliceGuardMut`].
pub struct IterMut<'a, T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
    Alloc: IAlloc,
{
    inner: SliceGuardMut<'a, T, Alloc>,
    start: usize,
    end: usize,
}

impl<'a, T, Alloc> Iterator for IterMut<'a, T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
    Alloc: IAlloc,
{
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        if self.start < self.end {
            let r = unsafe { core::mem::transmute(self.inner.get_unchecked_mut(self.start)) };
            self.start += 1;
            Some(r)
        } else {
            None
        }
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.len();
        (len, Some(len))
    }

    fn count(self) -> usize
    where
        Self: Sized,
    {
        self.len()
    }

    fn last(mut self) -> Option<Self::Item>
    where
        Self: Sized,
    {
        self.next_back()
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.start += n;
        self.next()
    }
}

impl<'a, T, Alloc> DoubleEndedIterator for IterMut<'a, T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
    Alloc: IAlloc,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.start < self.end {
            self.end -= 1;
            let r = unsafe { core::mem::transmute(self.inner.get_unchecked_mut(self.end)) };
            Some(r)
        } else {
            None
        }
    }
    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.end = self.end.saturating_sub(n);
        self.next_back()
    }
}
impl<'a, T, Alloc> ExactSizeIterator for IterMut<'a, T, Alloc>
where
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
    Alloc: IAlloc,
{
    fn len(&self) -> usize {
        self.end.saturating_sub(self.start)
    }
}

impl<T, Alloc: IAlloc> From<Vec<T, Alloc>> for SingleOrVec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn from(value: Vec<T, Alloc>) -> Self {
        Self {
            inner: crate::Result::Err(value),
        }
    }
}

impl<T, Alloc: IAlloc> From<SingleOrVec<T, Alloc>> for Vec<T, Alloc>
where
    T: IStable,
    Alloc: IStable,
    Single<T, Alloc>: IDeterminantProvider<Vec<T, Alloc>>,
    Vec<T, Alloc>: IStable,
    crate::Result<Single<T, Alloc>, Vec<T, Alloc>>: IStable,
{
    fn from(value: SingleOrVec<T, Alloc>) -> Self {
        value.inner.match_owned(
            |Single { value, alloc }| {
                let mut vec = Vec::new_in(alloc);
                vec.push(value);
                vec
            },
            |vec| vec,
        )
    }
}

#[test]
fn test() {
    use rand::Rng;
    const LEN: usize = 20;
    let mut std = std::vec::Vec::with_capacity(LEN);
    let mut new: SingleOrVec<u8> = SingleOrVec::new();
    let mut capacity: SingleOrVec<u8> = SingleOrVec::with_capacity(LEN);
    let mut rng = rand::thread_rng();
    let n: u8 = rng.gen();
    new.push(n);
    capacity.push(n);
    std.push(n);
    assert!(new.inner.is_ok());
    assert!(capacity.inner.is_err());
    for _ in 0..LEN {
        let n: u8 = rng.gen();
        new.push(n);
        capacity.push(n);
        std.push(n);
    }
    assert_eq!(new.as_slice(), std.as_slice());
    assert_eq!(new.as_slice(), capacity.as_slice());
    let clone = new.clone();
    assert_eq!(new.as_slice(), clone.as_slice());
}