2 use core
::convert
::{TryFrom, TryInto}
;
4 #[cfg(feature = "serde")]
5 use core
::marker
::PhantomData
;
6 #[cfg(feature = "serde")]
8 Deserialize
, Deserializer
, Error
as DeserializeError
, SeqAccess
, Visitor
,
10 #[cfg(feature = "serde")]
11 use serde
::ser
::{Serialize, SerializeSeq, Serializer}
;
13 /// Helper to make an `ArrayVec`.
15 /// You specify the backing array type, and optionally give all the elements you
16 /// want to initially place into the array.
21 /// // The backing array type can be specified in the macro call
22 /// let empty_av = array_vec!([u8; 16]);
23 /// let some_ints = array_vec!([i32; 4] => 1, 2, 3);
25 /// // Or left to inference
26 /// let empty_av: ArrayVec<[u8; 10]> = array_vec!();
27 /// let some_ints: ArrayVec<[u8; 10]> = array_vec!(5, 6, 7, 8);
30 macro_rules
! array_vec
{
31 ($array_type
:ty
=> $
($elem
:expr
),* $
(,)?
) => {
33 let mut av
: $
crate::ArrayVec
<$array_type
> = Default
::default();
39 $
crate::ArrayVec
::<$array_type
>::default()
41 ($
($elem
:expr
),*) => {
42 $
crate::array_vec
!(_
=> $
($elem
),*)
44 ($elem
:expr
; $n
:expr
) => {
45 $
crate::ArrayVec
::from([$elem
; $n
])
52 /// An array-backed, vector-like data structure.
54 /// * `ArrayVec` has a fixed capacity, equal to the array size.
55 /// * `ArrayVec` has a variable length, as you add and remove elements. Attempts
56 /// to fill the vec beyond its capacity will cause a panic.
57 /// * All of the vec's array slots are always initialized in terms of Rust's
58 /// memory model. When you remove a element from a location, the old value at
59 /// that location is replaced with the type's default value.
61 /// The overall API of this type is intended to, as much as possible, emulate
62 /// the API of the [`Vec`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html)
67 /// You can use the `array_vec!` macro similarly to how you might use the `vec!`
68 /// macro. Specify the array type, then optionally give all the initial values
72 /// let some_ints = array_vec!([i32; 4] => 1, 2, 3);
73 /// assert_eq!(some_ints.len(), 3);
76 /// The [`default`](ArrayVec::new) for an `ArrayVec` is to have a default
77 /// array with length 0. The [`new`](ArrayVec::new) method is the same as
81 /// let some_ints = ArrayVec::<[i32; 7]>::default();
82 /// assert_eq!(some_ints.len(), 0);
84 /// let more_ints = ArrayVec::<[i32; 7]>::new();
85 /// assert_eq!(some_ints, more_ints);
88 /// If you have an array and want the _whole thing_ so count as being "in" the
89 /// new `ArrayVec` you can use one of the `from` implementations. If you want
90 /// _part of_ the array then you can use
91 /// [`from_array_len`](ArrayVec::from_array_len):
94 /// let some_ints = ArrayVec::from([5, 6, 7, 8]);
95 /// assert_eq!(some_ints.len(), 4);
97 /// let more_ints = ArrayVec::from_array_len([5, 6, 7, 8], 2);
98 /// assert_eq!(more_ints.len(), 2);
100 /// let no_ints: ArrayVec<[u8; 5]> = ArrayVec::from_array_empty([1, 2, 3, 4, 5]);
101 /// assert_eq!(no_ints.len(), 0);
104 pub struct ArrayVec
<A
> {
109 impl<A
> Clone
for ArrayVec
<A
>
115 fn clone(&self) -> Self {
116 Self { data: self.data.clone(), len: self.len }
120 fn clone_from(&mut self, o
: &Self) {
125 .zip(o
.data
.as_slice())
126 .take(self.len
.max(o
.len
) as usize);
127 for (dst
, src
) in iter
{
130 if let Some(to_drop
) =
131 self.data
.as_slice_mut().get_mut((o
.len
as usize)..(self.len
as usize))
133 to_drop
.iter_mut().for_each(|x
| drop(take(x
)));
139 impl<A
> Copy
for ArrayVec
<A
>
146 impl<A
: Array
> Default
for ArrayVec
<A
> {
147 fn default() -> Self {
148 Self { len: 0, data: A::default() }
152 impl<A
: Array
> Deref
for ArrayVec
<A
> {
153 type Target
= [A
::Item
];
156 fn deref(&self) -> &Self::Target
{
157 &self.data
.as_slice()[..self.len
as usize]
161 impl<A
: Array
> DerefMut
for ArrayVec
<A
> {
164 fn deref_mut(&mut self) -> &mut Self::Target
{
165 &mut self.data
.as_slice_mut()[..self.len
as usize]
169 impl<A
: Array
, I
: SliceIndex
<[A
::Item
]>> Index
<I
> for ArrayVec
<A
> {
170 type Output
= <I
as SliceIndex
<[A
::Item
]>>::Output
;
173 fn index(&self, index
: I
) -> &Self::Output
{
178 impl<A
: Array
, I
: SliceIndex
<[A
::Item
]>> IndexMut
<I
> for ArrayVec
<A
> {
181 fn index_mut(&mut self, index
: I
) -> &mut Self::Output
{
182 &mut self.deref_mut()[index
]
186 #[cfg(feature = "serde")]
187 #[cfg_attr(docs_rs, doc(cfg(feature = "serde")))]
188 impl<A
: Array
> Serialize
for ArrayVec
<A
>
193 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
197 let mut seq
= serializer
.serialize_seq(Some(self.len()))?
;
198 for element
in self.iter() {
199 seq
.serialize_element(element
)?
;
205 #[cfg(feature = "serde")]
206 #[cfg_attr(docs_rs, doc(cfg(feature = "serde")))]
207 impl<'de
, A
: Array
> Deserialize
<'de
> for ArrayVec
<A
>
209 A
::Item
: Deserialize
<'de
>,
211 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
213 D
: Deserializer
<'de
>,
215 deserializer
.deserialize_seq(ArrayVecVisitor(PhantomData
))
219 #[cfg(all(feature = "arbitrary", feature = "nightly_const_generics"))]
222 doc(cfg(all(feature
= "arbitrary", feature
= "nightly_const_generics")))
224 impl<'a
, T
, const N
: usize> arbitrary
::Arbitrary
<'a
> for ArrayVec
<[T
; N
]>
226 T
: arbitrary
::Arbitrary
<'a
> + Default
,
228 fn arbitrary(u
: &mut arbitrary
::Unstructured
<'a
>) -> arbitrary
::Result
<Self> {
229 let v
= <[T
; N
]>::arbitrary(u
)?
;
230 let av
= ArrayVec
::from(v
);
235 impl<A
: Array
> ArrayVec
<A
> {
236 /// Move all values from `other` into this vec.
239 /// * If the vec overflows its capacity
243 /// # use tinyvec::*;
244 /// let mut av = array_vec!([i32; 10] => 1, 2, 3);
245 /// let mut av2 = array_vec!([i32; 10] => 4, 5, 6);
246 /// av.append(&mut av2);
247 /// assert_eq!(av, &[1, 2, 3, 4, 5, 6][..]);
248 /// assert_eq!(av2, &[][..]);
251 pub fn append(&mut self, other
: &mut Self) {
253 self.try_append(other
).is_none(),
254 "ArrayVec::append> total length {} exceeds capacity {}!",
255 self.len() + other
.len(),
260 /// Move all values from `other` into this vec.
261 /// If appending would overflow the capacity, Some(other) is returned.
264 /// # use tinyvec::*;
265 /// let mut av = array_vec!([i32; 7] => 1, 2, 3);
266 /// let mut av2 = array_vec!([i32; 7] => 4, 5, 6);
267 /// av.append(&mut av2);
268 /// assert_eq!(av, &[1, 2, 3, 4, 5, 6][..]);
269 /// assert_eq!(av2, &[][..]);
271 /// let mut av3 = array_vec!([i32; 7] => 7, 8, 9);
272 /// assert!(av.try_append(&mut av3).is_some());
273 /// assert_eq!(av, &[1, 2, 3, 4, 5, 6][..]);
274 /// assert_eq!(av3, &[7, 8, 9][..]);
277 pub fn try_append
<'other
>(
278 &mut self, other
: &'other
mut Self,
279 ) -> Option
<&'other
mut Self> {
280 let new_len
= self.len() + other
.len();
281 if new_len
> A
::CAPACITY
{
285 let iter
= other
.iter_mut().map(take
);
295 /// A `*mut` pointer to the backing array.
299 /// This pointer has provenance over the _entire_ backing array.
302 pub fn as_mut_ptr(&mut self) -> *mut A
::Item
{
303 self.data
.as_slice_mut().as_mut_ptr()
306 /// Performs a `deref_mut`, into unique slice form.
309 pub fn as_mut_slice(&mut self) -> &mut [A
::Item
] {
313 /// A `*const` pointer to the backing array.
317 /// This pointer has provenance over the _entire_ backing array.
320 pub fn as_ptr(&self) -> *const A
::Item
{
321 self.data
.as_slice().as_ptr()
324 /// Performs a `deref`, into shared slice form.
327 pub fn as_slice(&self) -> &[A
::Item
] {
331 /// The capacity of the `ArrayVec`.
333 /// This is fixed based on the array type, but can't yet be made a `const fn`
337 pub fn capacity(&self) -> usize {
338 // Note: This shouldn't use A::CAPACITY, because unsafe code can't rely on
339 // any Array invariants. This ensures that at the very least, the returned
340 // value is a valid length for a subslice of the backing array.
341 self.data
.as_slice().len()
344 /// Truncates the `ArrayVec` down to length 0.
346 pub fn clear(&mut self) {
350 /// Creates a draining iterator that removes the specified range in the vector
351 /// and yields the removed items.
354 /// * If the start is greater than the end
355 /// * If the end is past the edge of the vec.
359 /// # use tinyvec::*;
360 /// let mut av = array_vec!([i32; 4] => 1, 2, 3);
361 /// let av2: ArrayVec<[i32; 4]> = av.drain(1..).collect();
362 /// assert_eq!(av.as_slice(), &[1][..]);
363 /// assert_eq!(av2.as_slice(), &[2, 3][..]);
366 /// assert_eq!(av.as_slice(), &[]);
369 pub fn drain
<R
>(&mut self, range
: R
) -> ArrayVecDrain
<'_
, A
::Item
>
371 R
: RangeBounds
<usize>,
373 ArrayVecDrain
::new(self, range
)
376 /// Returns the inner array of the `ArrayVec`.
378 /// This returns the full array, even if the `ArrayVec` length is currently
384 /// # use tinyvec::{array_vec, ArrayVec};
385 /// let mut favorite_numbers = array_vec!([i32; 5] => 87, 48, 33, 9, 26);
386 /// assert_eq!(favorite_numbers.clone().into_inner(), [87, 48, 33, 9, 26]);
388 /// favorite_numbers.pop();
389 /// assert_eq!(favorite_numbers.into_inner(), [87, 48, 33, 9, 0]);
392 /// A use for this function is to build an array from an iterator by first
393 /// collecting it into an `ArrayVec`.
396 /// # use tinyvec::ArrayVec;
397 /// let arr_vec: ArrayVec<[i32; 10]> = (1..=3).cycle().take(10).collect();
398 /// let inner = arr_vec.into_inner();
399 /// assert_eq!(inner, [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]);
402 pub fn into_inner(self) -> A
{
406 /// Clone each element of the slice into this `ArrayVec`.
409 /// * If the `ArrayVec` would overflow, this will panic.
411 pub fn extend_from_slice(&mut self, sli
: &[A
::Item
])
419 let new_len
= self.len
as usize + sli
.len();
421 new_len
<= A
::CAPACITY
,
422 "ArrayVec::extend_from_slice> total length {} exceeds capacity {}!",
427 let target
= &mut self.data
.as_slice_mut()[self.len
as usize..new_len
];
428 target
.clone_from_slice(sli
);
429 self.set_len(new_len
);
432 /// Fill the vector until its capacity has been reached.
434 /// Successively fills unused space in the spare slice of the vector with
435 /// elements from the iterator. It then returns the remaining iterator
436 /// without exhausting it. This also allows appending the head of an
437 /// infinite iterator.
439 /// This is an alternative to `Extend::extend` method for cases where the
440 /// length of the iterator can not be checked. Since this vector can not
441 /// reallocate to increase its capacity, it is unclear what to do with
442 /// remaining elements in the iterator and the iterator itself. The
443 /// interface also provides no way to communicate this to the caller.
446 /// * If the `next` method of the provided iterator panics.
451 /// # use tinyvec::*;
452 /// let mut av = array_vec!([i32; 4]);
453 /// let mut to_inf = av.fill(0..);
454 /// assert_eq!(&av[..], [0, 1, 2, 3]);
455 /// assert_eq!(to_inf.next(), Some(4));
458 pub fn fill
<I
: IntoIterator
<Item
= A
::Item
>>(
461 // If this is written as a call to push for each element in iter, the
462 // compiler emits code that updates the length for every element. The
463 // additional complexity from that length update is worth nearly 2x in
464 // the runtime of this function.
465 let mut iter
= iter
.into_iter();
467 let to_take
= self.capacity() - self.len();
468 let target
= &mut self.data
.as_slice_mut()[self.len
as usize..];
469 for element
in iter
.by_ref().take(to_take
) {
470 target
[pushed
] = element
;
473 self.len
+= pushed
as u16;
477 /// Wraps up an array and uses the given length as the initial length.
479 /// If you want to simply use the full array, use `from` instead.
483 /// * The length specified must be less than or equal to the capacity of the
487 #[allow(clippy::match_wild_err_arm)]
488 pub fn from_array_len(data
: A
, len
: usize) -> Self {
489 match Self::try_from_array_len(data
, len
) {
492 "ArrayVec::from_array_len> length {} exceeds capacity {}!",
499 /// Inserts an item at the position given, moving all following elements +1
503 /// * If `index` > `len`
504 /// * If the capacity is exhausted
509 /// let mut av = array_vec!([i32; 10] => 1, 2, 3);
511 /// assert_eq!(av.as_slice(), &[1, 4, 2, 3]);
513 /// assert_eq!(av.as_slice(), &[1, 4, 2, 3, 5]);
516 pub fn insert(&mut self, index
: usize, item
: A
::Item
) {
517 let x
= self.try_insert(index
, item
);
518 assert
!(x
.is_none(), "ArrayVec::insert> capacity overflow!");
521 /// Tries to insert an item at the position given, moving all following
522 /// elements +1 index.
523 /// Returns back the element if the capacity is exhausted,
524 /// otherwise returns None.
527 /// * If `index` > `len`
532 /// let mut av = array_vec!([&'static str; 4] => "one", "two", "three");
533 /// av.insert(1, "four");
534 /// assert_eq!(av.as_slice(), &["one", "four", "two", "three"]);
535 /// assert_eq!(av.try_insert(4, "five"), Some("five"));
539 &mut self, index
: usize, mut item
: A
::Item
,
540 ) -> Option
<A
::Item
> {
542 index
<= self.len
as usize,
543 "ArrayVec::try_insert> index {} is out of bounds {}",
548 // A previous implementation used self.try_push and slice::rotate_right
549 // rotate_right and rotate_left generate a huge amount of code and fail to
550 // inline; calling them here incurs the cost of all the cases they
551 // handle even though we're rotating a usually-small array by a constant
552 // 1 offset. This swap-based implementation benchmarks much better for
553 // small array lengths in particular.
555 if (self.len
as usize) < A
::CAPACITY
{
561 let target
= &mut self.as_mut_slice()[index
..];
562 for i
in 0..target
.len() {
563 core
::mem
::swap(&mut item
, &mut target
[i
]);
568 /// Checks if the length is 0.
571 pub fn is_empty(&self) -> bool
{
575 /// The length of the `ArrayVec` (in elements).
578 pub fn len(&self) -> usize {
582 /// Makes a new, empty `ArrayVec`.
585 pub fn new() -> Self {
589 /// Remove and return the last element of the vec, if there is one.
592 /// * If the vec is empty you get `None`.
596 /// # use tinyvec::*;
597 /// let mut av = array_vec!([i32; 10] => 1, 2);
598 /// assert_eq!(av.pop(), Some(2));
599 /// assert_eq!(av.pop(), Some(1));
600 /// assert_eq!(av.pop(), None);
603 pub fn pop(&mut self) -> Option
<A
::Item
> {
606 let out
= take(&mut self.data
.as_slice_mut()[self.len
as usize]);
613 /// Place an element onto the end of the vec.
616 /// * If the length of the vec would overflow the capacity.
620 /// # use tinyvec::*;
621 /// let mut av = array_vec!([i32; 2]);
622 /// assert_eq!(&av[..], []);
624 /// assert_eq!(&av[..], [1]);
626 /// assert_eq!(&av[..], [1, 2]);
627 /// // av.push(3); this would overflow the ArrayVec and panic!
630 pub fn push(&mut self, val
: A
::Item
) {
631 let x
= self.try_push(val
);
632 assert
!(x
.is_none(), "ArrayVec::push> capacity overflow!");
635 /// Tries to place an element onto the end of the vec.\
636 /// Returns back the element if the capacity is exhausted,
637 /// otherwise returns None.
639 /// # use tinyvec::*;
640 /// let mut av = array_vec!([i32; 2]);
641 /// assert_eq!(av.as_slice(), []);
642 /// assert_eq!(av.try_push(1), None);
643 /// assert_eq!(&av[..], [1]);
644 /// assert_eq!(av.try_push(2), None);
645 /// assert_eq!(&av[..], [1, 2]);
646 /// assert_eq!(av.try_push(3), Some(3));
649 pub fn try_push(&mut self, val
: A
::Item
) -> Option
<A
::Item
> {
650 debug_assert
!(self.len
as usize <= A
::CAPACITY
);
652 let itemref
= match self.data
.as_slice_mut().get_mut(self.len
as usize) {
653 None
=> return Some(val
),
662 /// Removes the item at `index`, shifting all others down by one index.
664 /// Returns the removed element.
668 /// * If the index is out of bounds.
673 /// # use tinyvec::*;
674 /// let mut av = array_vec!([i32; 4] => 1, 2, 3);
675 /// assert_eq!(av.remove(1), 2);
676 /// assert_eq!(&av[..], [1, 3]);
679 pub fn remove(&mut self, index
: usize) -> A
::Item
{
680 let targets
: &mut [A
::Item
] = &mut self.deref_mut()[index
..];
681 let item
= take(&mut targets
[0]);
683 // A previous implementation used rotate_left
684 // rotate_right and rotate_left generate a huge amount of code and fail to
685 // inline; calling them here incurs the cost of all the cases they
686 // handle even though we're rotating a usually-small array by a constant
687 // 1 offset. This swap-based implementation benchmarks much better for
688 // small array lengths in particular.
690 for i
in 0..targets
.len() - 1 {
691 targets
.swap(i
, i
+ 1);
697 /// As [`resize_with`](ArrayVec::resize_with)
698 /// and it clones the value as the closure.
703 /// # use tinyvec::*;
705 /// let mut av = array_vec!([&str; 10] => "hello");
706 /// av.resize(3, "world");
707 /// assert_eq!(&av[..], ["hello", "world", "world"]);
709 /// let mut av = array_vec!([i32; 10] => 1, 2, 3, 4);
711 /// assert_eq!(&av[..], [1, 2]);
714 pub fn resize(&mut self, new_len
: usize, new_val
: A
::Item
)
718 self.resize_with(new_len
, || new_val
.clone())
721 /// Resize the vec to the new length.
723 /// If it needs to be longer, it's filled with repeated calls to the provided
724 /// function. If it needs to be shorter, it's truncated.
729 /// # use tinyvec::*;
731 /// let mut av = array_vec!([i32; 10] => 1, 2, 3);
732 /// av.resize_with(5, Default::default);
733 /// assert_eq!(&av[..], [1, 2, 3, 0, 0]);
735 /// let mut av = array_vec!([i32; 10]);
737 /// av.resize_with(4, || {
741 /// assert_eq!(&av[..], [2, 4, 8, 16]);
744 pub fn resize_with
<F
: FnMut() -> A
::Item
>(
745 &mut self, new_len
: usize, mut f
: F
,
747 match new_len
.checked_sub(self.len
as usize) {
748 None
=> self.truncate(new_len
),
749 Some(new_elements
) => {
750 for _
in 0..new_elements
{
757 /// Walk the vec and keep only the elements that pass the predicate given.
762 /// # use tinyvec::*;
764 /// let mut av = array_vec!([i32; 10] => 1, 1, 2, 3, 3, 4);
765 /// av.retain(|&x| x % 2 == 0);
766 /// assert_eq!(&av[..], [2, 4]);
769 pub fn retain
<F
: FnMut(&A
::Item
) -> bool
>(&mut self, mut acceptable
: F
) {
770 // Drop guard to contain exactly the remaining elements when the test
772 struct JoinOnDrop
<'vec
, Item
> {
773 items
: &'vec
mut [Item
],
775 // Start of tail relative to `done_end`.
779 impl<Item
> Drop
for JoinOnDrop
<'_
, Item
> {
781 self.items
[self.done_end
..].rotate_left(self.tail_start
);
785 let mut rest
= JoinOnDrop
{
786 items
: &mut self.data
.as_slice_mut()[..self.len
as usize],
791 let len
= self.len
as usize;
793 // Loop start invariant: idx = rest.done_end + rest.tail_start
794 if !acceptable(&rest
.items
[idx
]) {
795 let _
= take(&mut rest
.items
[idx
]);
797 rest
.tail_start
+= 1;
799 rest
.items
.swap(rest
.done_end
, idx
);
805 /// Forces the length of the vector to `new_len`.
808 /// * If `new_len` is greater than the vec's capacity.
811 /// * This is a fully safe operation! The inactive memory already counts as
812 /// "initialized" by Rust's rules.
813 /// * Other than "the memory is initialized" there are no other guarantees
814 /// regarding what you find in the inactive portion of the vec.
816 pub fn set_len(&mut self, new_len
: usize) {
817 if new_len
> A
::CAPACITY
{
818 // Note(Lokathor): Technically we don't have to panic here, and we could
819 // just let some other call later on trigger a panic on accident when the
820 // length is wrong. However, it's a lot easier to catch bugs when things
821 // are more "fail-fast".
823 "ArrayVec::set_len> new length {} exceeds capacity {}",
829 let new_len
: u16 = new_len
831 .expect("ArrayVec::set_len> new length is not in range 0..=u16::MAX");
835 /// Splits the collection at the point given.
837 /// * `[0, at)` stays in this vec
838 /// * `[at, len)` ends up in the new vec.
846 /// # use tinyvec::*;
847 /// let mut av = array_vec!([i32; 4] => 1, 2, 3);
848 /// let av2 = av.split_off(1);
849 /// assert_eq!(&av[..], [1]);
850 /// assert_eq!(&av2[..], [2, 3]);
853 pub fn split_off(&mut self, at
: usize) -> Self {
854 // FIXME: should this just use drain into the output?
857 "ArrayVec::split_off> at value {} exceeds length of {}",
861 let mut new
= Self::default();
862 let moves
= &mut self.as_mut_slice()[at
..];
863 let split_len
= moves
.len();
864 let targets
= &mut new
.data
.as_slice_mut()[..split_len
];
865 moves
.swap_with_slice(targets
);
867 /* moves.len() <= u16::MAX, so these are surely in u16 range */
868 new
.len
= split_len
as u16;
869 self.len
= at
as u16;
873 /// Creates a splicing iterator that removes the specified range in the
874 /// vector, yields the removed items, and replaces them with elements from
875 /// the provided iterator.
877 /// `splice` fuses the provided iterator, so elements after the first `None`
881 /// * If the start is greater than the end.
882 /// * If the end is past the edge of the vec.
883 /// * If the provided iterator panics.
884 /// * If the new length would overflow the capacity of the array. Because
885 /// `ArrayVecSplice` adds elements to this vec in its destructor when
886 /// necessary, this panic would occur when it is dropped.
891 /// let mut av = array_vec!([i32; 4] => 1, 2, 3);
892 /// let av2: ArrayVec<[i32; 4]> = av.splice(1.., 4..=6).collect();
893 /// assert_eq!(av.as_slice(), &[1, 4, 5, 6][..]);
894 /// assert_eq!(av2.as_slice(), &[2, 3][..]);
896 /// av.splice(.., None);
897 /// assert_eq!(av.as_slice(), &[]);
901 &mut self, range
: R
, replacement
: I
,
902 ) -> ArrayVecSplice
<'_
, A
, core
::iter
::Fuse
<I
::IntoIter
>>
904 R
: RangeBounds
<usize>,
905 I
: IntoIterator
<Item
= A
::Item
>,
907 use core
::ops
::Bound
;
908 let start
= match range
.start_bound() {
909 Bound
::Included(x
) => *x
,
910 Bound
::Excluded(x
) => x
.saturating_add(1),
911 Bound
::Unbounded
=> 0,
913 let end
= match range
.end_bound() {
914 Bound
::Included(x
) => x
.saturating_add(1),
915 Bound
::Excluded(x
) => *x
,
916 Bound
::Unbounded
=> self.len(),
920 "ArrayVec::splice> Illegal range, {} to {}",
926 "ArrayVec::splice> Range ends at {} but length is only {}!",
932 removal_start
: start
,
935 replacement
: replacement
.into_iter().fuse(),
939 /// Remove an element, swapping the end of the vec into its place.
942 /// * If the index is out of bounds.
946 /// # use tinyvec::*;
947 /// let mut av = array_vec!([&str; 4] => "foo", "bar", "quack", "zap");
949 /// assert_eq!(av.swap_remove(1), "bar");
950 /// assert_eq!(&av[..], ["foo", "zap", "quack"]);
952 /// assert_eq!(av.swap_remove(0), "foo");
953 /// assert_eq!(&av[..], ["quack", "zap"]);
956 pub fn swap_remove(&mut self, index
: usize) -> A
::Item
{
959 "ArrayVec::swap_remove> index {} is out of bounds {}",
963 if index
== self.len() - 1 {
966 let i
= self.pop().unwrap();
967 replace(&mut self[index
], i
)
971 /// Reduces the vec's length to the given value.
973 /// If the vec is already shorter than the input, nothing happens.
975 pub fn truncate(&mut self, new_len
: usize) {
976 if new_len
>= self.len
as usize {
980 if needs_drop
::<A
::Item
>() {
981 let len
= self.len
as usize;
982 self.data
.as_slice_mut()[new_len
..len
]
988 /* new_len is less than self.len */
989 self.len
= new_len
as u16;
992 /// Wraps an array, using the given length as the starting length.
994 /// If you want to use the whole length of the array, you can just use the
999 /// If the given length is greater than the capacity of the array this will
1000 /// error, and you'll get the array back in the `Err`.
1002 pub fn try_from_array_len(data
: A
, len
: usize) -> Result
<Self, A
> {
1003 /* Note(Soveu): Should we allow A::CAPACITY > u16::MAX for now? */
1004 if len
<= A
::CAPACITY
{
1005 Ok(Self { data, len: len as u16 }
)
1012 impl<A
> ArrayVec
<A
> {
1013 /// Wraps up an array as a new empty `ArrayVec`.
1015 /// If you want to simply use the full array, use `from` instead.
1019 /// This method in particular allows to create values for statics:
1022 /// # use tinyvec::ArrayVec;
1023 /// static DATA: ArrayVec<[u8; 5]> = ArrayVec::from_array_empty([0; 5]);
1024 /// assert_eq!(DATA.len(), 0);
1027 /// But of course it is just an normal empty `ArrayVec`:
1030 /// # use tinyvec::ArrayVec;
1031 /// let mut data = ArrayVec::from_array_empty([1, 2, 3, 4]);
1032 /// assert_eq!(&data[..], &[]);
1034 /// assert_eq!(&data[..], &[42]);
1038 pub const fn from_array_empty(data
: A
) -> Self {
1039 Self { data, len: 0 }
1043 #[cfg(feature = "grab_spare_slice")]
1044 impl<A
: Array
> ArrayVec
<A
> {
1045 /// Obtain the shared slice of the array _after_ the active memory.
1049 /// # use tinyvec::*;
1050 /// let mut av = array_vec!([i32; 4]);
1051 /// assert_eq!(av.grab_spare_slice().len(), 4);
1056 /// assert_eq!(av.grab_spare_slice().len(), 0);
1059 pub fn grab_spare_slice(&self) -> &[A
::Item
] {
1060 &self.data
.as_slice()[self.len
as usize..]
1063 /// Obtain the mutable slice of the array _after_ the active memory.
1067 /// # use tinyvec::*;
1068 /// let mut av = array_vec!([i32; 4]);
1069 /// assert_eq!(av.grab_spare_slice_mut().len(), 4);
1072 /// assert_eq!(av.grab_spare_slice_mut().len(), 2);
1075 pub fn grab_spare_slice_mut(&mut self) -> &mut [A
::Item
] {
1076 &mut self.data
.as_slice_mut()[self.len
as usize..]
1080 #[cfg(feature = "nightly_slice_partition_dedup")]
1081 impl<A
: Array
> ArrayVec
<A
> {
1082 /// De-duplicates the vec contents.
1084 pub fn dedup(&mut self)
1088 self.dedup_by(|a
, b
| a
== b
)
1091 /// De-duplicates the vec according to the predicate given.
1093 pub fn dedup_by
<F
>(&mut self, same_bucket
: F
)
1095 F
: FnMut(&mut A
::Item
, &mut A
::Item
) -> bool
,
1098 let (dedup
, _
) = self.as_mut_slice().partition_dedup_by(same_bucket
);
1104 /// De-duplicates the vec according to the key selector given.
1106 pub fn dedup_by_key
<F
, K
>(&mut self, mut key
: F
)
1108 F
: FnMut(&mut A
::Item
) -> K
,
1111 self.dedup_by(|a
, b
| key(a
) == key(b
))
1115 /// Splicing iterator for `ArrayVec`
1116 /// See [`ArrayVec::splice`](ArrayVec::<A>::splice)
1117 pub struct ArrayVecSplice
<'p
, A
: Array
, I
: Iterator
<Item
= A
::Item
>> {
1118 parent
: &'p
mut ArrayVec
<A
>,
1119 removal_start
: usize,
1124 impl<'p
, A
: Array
, I
: Iterator
<Item
= A
::Item
>> Iterator
1125 for ArrayVecSplice
<'p
, A
, I
>
1127 type Item
= A
::Item
;
1130 fn next(&mut self) -> Option
<A
::Item
> {
1131 if self.removal_start
< self.removal_end
{
1132 match self.replacement
.next() {
1133 Some(replacement
) => {
1134 let removed
= core
::mem
::replace(
1135 &mut self.parent
[self.removal_start
],
1138 self.removal_start
+= 1;
1142 let removed
= self.parent
.remove(self.removal_start
);
1143 self.removal_end
-= 1;
1153 fn size_hint(&self) -> (usize, Option
<usize>) {
1154 let len
= self.len();
1159 impl<'p
, A
, I
> ExactSizeIterator
for ArrayVecSplice
<'p
, A
, I
>
1162 I
: Iterator
<Item
= A
::Item
>,
1165 fn len(&self) -> usize {
1166 self.removal_end
- self.removal_start
1170 impl<'p
, A
, I
> FusedIterator
for ArrayVecSplice
<'p
, A
, I
>
1173 I
: Iterator
<Item
= A
::Item
>,
1177 impl<'p
, A
, I
> DoubleEndedIterator
for ArrayVecSplice
<'p
, A
, I
>
1180 I
: Iterator
<Item
= A
::Item
> + DoubleEndedIterator
,
1183 fn next_back(&mut self) -> Option
<A
::Item
> {
1184 if self.removal_start
< self.removal_end
{
1185 match self.replacement
.next_back() {
1186 Some(replacement
) => {
1187 let removed
= core
::mem
::replace(
1188 &mut self.parent
[self.removal_end
- 1],
1191 self.removal_end
-= 1;
1195 let removed
= self.parent
.remove(self.removal_end
- 1);
1196 self.removal_end
-= 1;
1206 impl<'p
, A
: Array
, I
: Iterator
<Item
= A
::Item
>> Drop
1207 for ArrayVecSplice
<'p
, A
, I
>
1209 fn drop(&mut self) {
1210 for _
in self.by_ref() {}
1212 // FIXME: reserve lower bound of size_hint
1214 for replacement
in self.replacement
.by_ref() {
1215 self.parent
.insert(self.removal_end
, replacement
);
1216 self.removal_end
+= 1;
1221 impl<A
: Array
> AsMut
<[A
::Item
]> for ArrayVec
<A
> {
1224 fn as_mut(&mut self) -> &mut [A
::Item
] {
1229 impl<A
: Array
> AsRef
<[A
::Item
]> for ArrayVec
<A
> {
1232 fn as_ref(&self) -> &[A
::Item
] {
1237 impl<A
: Array
> Borrow
<[A
::Item
]> for ArrayVec
<A
> {
1240 fn borrow(&self) -> &[A
::Item
] {
1245 impl<A
: Array
> BorrowMut
<[A
::Item
]> for ArrayVec
<A
> {
1248 fn borrow_mut(&mut self) -> &mut [A
::Item
] {
1253 impl<A
: Array
> Extend
<A
::Item
> for ArrayVec
<A
> {
1255 fn extend
<T
: IntoIterator
<Item
= A
::Item
>>(&mut self, iter
: T
) {
1262 impl<A
: Array
> From
<A
> for ArrayVec
<A
> {
1265 /// The output has a length equal to the full array.
1267 /// If you want to select a length, use
1268 /// [`from_array_len`](ArrayVec::from_array_len)
1269 fn from(data
: A
) -> Self {
1274 .expect("ArrayVec::from> length must be in range 0..=u16::MAX");
1279 /// The error type returned when a conversion from a slice to an [`ArrayVec`]
1281 #[derive(Debug, Copy, Clone)]
1282 pub struct TryFromSliceError(());
1284 impl core
::fmt
::Display
for TryFromSliceError
{
1285 fn fmt(&self, f
: &mut Formatter
<'_
>) -> core
::fmt
::Result
{
1286 f
.write_str("could not convert slice to ArrayVec")
1290 #[cfg(feature = "std")]
1291 impl std
::error
::Error
for TryFromSliceError {}
1293 impl<T
, A
> TryFrom
<&'_
[T
]> for ArrayVec
<A
>
1298 type Error
= TryFromSliceError
;
1302 /// The output has a length equal to that of the slice, with the same capacity
1304 fn try_from(slice
: &[T
]) -> Result
<Self, Self::Error
> {
1305 if slice
.len() > A
::CAPACITY
{
1306 Err(TryFromSliceError(()))
1308 let mut arr
= ArrayVec
::new();
1309 // We do not use ArrayVec::extend_from_slice, because it looks like LLVM
1310 // fails to deduplicate all the length-checking logic between the
1311 // above if and the contents of that method, thus producing much
1312 // slower code. Unlike many of the other optimizations in this
1313 // crate, this one is worth keeping an eye on. I see no reason, for
1314 // any element type, that these should produce different code. But
1315 // they do. (rustc 1.51.0)
1316 arr
.set_len(slice
.len());
1317 arr
.as_mut_slice().clone_from_slice(slice
);
1323 impl<A
: Array
> FromIterator
<A
::Item
> for ArrayVec
<A
> {
1326 fn from_iter
<T
: IntoIterator
<Item
= A
::Item
>>(iter
: T
) -> Self {
1327 let mut av
= Self::default();
1335 /// Iterator for consuming an `ArrayVec` and returning owned elements.
1336 pub struct ArrayVecIterator
<A
: Array
> {
1342 impl<A
: Array
> ArrayVecIterator
<A
> {
1343 /// Returns the remaining items of this iterator as a slice.
1346 pub fn as_slice(&self) -> &[A
::Item
] {
1347 &self.data
.as_slice()[self.base
as usize..self.tail
as usize]
1350 impl<A
: Array
> FusedIterator
for ArrayVecIterator
<A
> {}
1351 impl<A
: Array
> Iterator
for ArrayVecIterator
<A
> {
1352 type Item
= A
::Item
;
1354 fn next(&mut self) -> Option
<Self::Item
> {
1356 &mut self.data
.as_slice_mut()[self.base
as usize..self.tail
as usize];
1357 let itemref
= slice
.first_mut()?
;
1359 return Some(take(itemref
));
1363 fn size_hint(&self) -> (usize, Option
<usize>) {
1364 let s
= self.tail
- self.base
;
1369 fn count(self) -> usize {
1373 fn last(mut self) -> Option
<Self::Item
> {
1377 fn nth(&mut self, n
: usize) -> Option
<A
::Item
> {
1378 let slice
= &mut self.data
.as_slice_mut();
1379 let slice
= &mut slice
[self.base
as usize..self.tail
as usize];
1381 if let Some(x
) = slice
.get_mut(n
) {
1382 /* n is in range [0 .. self.tail - self.base) so in u16 range */
1383 self.base
+= n
as u16 + 1;
1384 return Some(take(x
));
1387 self.base
= self.tail
;
1392 impl<A
: Array
> DoubleEndedIterator
for ArrayVecIterator
<A
> {
1394 fn next_back(&mut self) -> Option
<Self::Item
> {
1396 &mut self.data
.as_slice_mut()[self.base
as usize..self.tail
as usize];
1397 let item
= slice
.last_mut()?
;
1399 return Some(take(item
));
1401 #[cfg(feature = "rustc_1_40")]
1403 fn nth_back(&mut self, n
: usize) -> Option
<Self::Item
> {
1404 let base
= self.base
as usize;
1405 let tail
= self.tail
as usize;
1406 let slice
= &mut self.data
.as_slice_mut()[base
..tail
];
1407 let n
= n
.saturating_add(1);
1409 if let Some(n
) = slice
.len().checked_sub(n
) {
1410 let item
= &mut slice
[n
];
1411 /* n is in [0..self.tail - self.base] range, so in u16 range */
1412 self.tail
= self.base
+ n
as u16;
1413 return Some(take(item
));
1416 self.tail
= self.base
;
1421 impl<A
: Array
> Debug
for ArrayVecIterator
<A
>
1425 #[allow(clippy::missing_inline_in_public_items)]
1426 fn fmt(&self, f
: &mut Formatter
<'_
>) -> core
::fmt
::Result
{
1427 f
.debug_tuple("ArrayVecIterator").field(&self.as_slice()).finish()
1431 impl<A
: Array
> IntoIterator
for ArrayVec
<A
> {
1432 type Item
= A
::Item
;
1433 type IntoIter
= ArrayVecIterator
<A
>;
1436 fn into_iter(self) -> Self::IntoIter
{
1437 ArrayVecIterator { base: 0, tail: self.len, data: self.data }
1441 impl<'a
, A
: Array
> IntoIterator
for &'a
mut ArrayVec
<A
> {
1442 type Item
= &'a
mut A
::Item
;
1443 type IntoIter
= core
::slice
::IterMut
<'a
, A
::Item
>;
1446 fn into_iter(self) -> Self::IntoIter
{
1451 impl<'a
, A
: Array
> IntoIterator
for &'a ArrayVec
<A
> {
1452 type Item
= &'a A
::Item
;
1453 type IntoIter
= core
::slice
::Iter
<'a
, A
::Item
>;
1456 fn into_iter(self) -> Self::IntoIter
{
1461 impl<A
: Array
> PartialEq
for ArrayVec
<A
>
1467 fn eq(&self, other
: &Self) -> bool
{
1468 self.as_slice().eq(other
.as_slice())
1471 impl<A
: Array
> Eq
for ArrayVec
<A
> where A
::Item
: Eq {}
1473 impl<A
: Array
> PartialOrd
for ArrayVec
<A
>
1475 A
::Item
: PartialOrd
,
1479 fn partial_cmp(&self, other
: &Self) -> Option
<core
::cmp
::Ordering
> {
1480 self.as_slice().partial_cmp(other
.as_slice())
1483 impl<A
: Array
> Ord
for ArrayVec
<A
>
1489 fn cmp(&self, other
: &Self) -> core
::cmp
::Ordering
{
1490 self.as_slice().cmp(other
.as_slice())
1494 impl<A
: Array
> PartialEq
<&A
> for ArrayVec
<A
>
1500 fn eq(&self, other
: &&A
) -> bool
{
1501 self.as_slice().eq(other
.as_slice())
1505 impl<A
: Array
> PartialEq
<&[A
::Item
]> for ArrayVec
<A
>
1511 fn eq(&self, other
: &&[A
::Item
]) -> bool
{
1512 self.as_slice().eq(*other
)
1516 impl<A
: Array
> Hash
for ArrayVec
<A
>
1521 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
1522 self.as_slice().hash(state
)
1526 #[cfg(feature = "experimental_write_impl")]
1527 impl<A
: Array
<Item
= u8>> core
::fmt
::Write
for ArrayVec
<A
> {
1528 fn write_str(&mut self, s
: &str) -> core
::fmt
::Result
{
1529 let my_len
= self.len();
1530 let str_len
= s
.as_bytes().len();
1531 if my_len
+ str_len
<= A
::CAPACITY
{
1532 let remainder
= &mut self.data
.as_slice_mut()[my_len
..];
1533 let target
= &mut remainder
[..str_len
];
1534 target
.copy_from_slice(s
.as_bytes());
1537 Err(core
::fmt
::Error
)
1542 // // // // // // // //
1544 // // // // // // // //
1546 impl<A
: Array
> Binary
for ArrayVec
<A
>
1550 #[allow(clippy::missing_inline_in_public_items)]
1551 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1556 for (i
, elem
) in self.iter().enumerate() {
1558 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1560 Binary
::fmt(elem
, f
)?
;
1569 impl<A
: Array
> Debug
for ArrayVec
<A
>
1573 #[allow(clippy::missing_inline_in_public_items)]
1574 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1579 for (i
, elem
) in self.iter().enumerate() {
1581 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1583 Debug
::fmt(elem
, f
)?
;
1592 impl<A
: Array
> Display
for ArrayVec
<A
>
1596 #[allow(clippy::missing_inline_in_public_items)]
1597 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1602 for (i
, elem
) in self.iter().enumerate() {
1604 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1606 Display
::fmt(elem
, f
)?
;
1615 impl<A
: Array
> LowerExp
for ArrayVec
<A
>
1619 #[allow(clippy::missing_inline_in_public_items)]
1620 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1625 for (i
, elem
) in self.iter().enumerate() {
1627 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1629 LowerExp
::fmt(elem
, f
)?
;
1638 impl<A
: Array
> LowerHex
for ArrayVec
<A
>
1642 #[allow(clippy::missing_inline_in_public_items)]
1643 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1648 for (i
, elem
) in self.iter().enumerate() {
1650 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1652 LowerHex
::fmt(elem
, f
)?
;
1661 impl<A
: Array
> Octal
for ArrayVec
<A
>
1665 #[allow(clippy::missing_inline_in_public_items)]
1666 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1671 for (i
, elem
) in self.iter().enumerate() {
1673 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1675 Octal
::fmt(elem
, f
)?
;
1684 impl<A
: Array
> Pointer
for ArrayVec
<A
>
1688 #[allow(clippy::missing_inline_in_public_items)]
1689 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1694 for (i
, elem
) in self.iter().enumerate() {
1696 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1698 Pointer
::fmt(elem
, f
)?
;
1707 impl<A
: Array
> UpperExp
for ArrayVec
<A
>
1711 #[allow(clippy::missing_inline_in_public_items)]
1712 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1717 for (i
, elem
) in self.iter().enumerate() {
1719 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1721 UpperExp
::fmt(elem
, f
)?
;
1730 impl<A
: Array
> UpperHex
for ArrayVec
<A
>
1734 #[allow(clippy::missing_inline_in_public_items)]
1735 fn fmt(&self, f
: &mut Formatter
) -> core
::fmt
::Result
{
1740 for (i
, elem
) in self.iter().enumerate() {
1742 write
!(f
, ",{}", if f
.alternate() { "\n " }
else { " " }
)?
;
1744 UpperHex
::fmt(elem
, f
)?
;
1753 #[cfg(feature = "alloc")]
1754 use alloc
::vec
::Vec
;
1756 #[cfg(all(feature = "alloc", feature = "rustc_1_57"))]
1757 use alloc
::collections
::TryReserveError
;
1759 #[cfg(feature = "alloc")]
1760 impl<A
: Array
> ArrayVec
<A
> {
1761 /// Drains all elements to a Vec, but reserves additional space
1763 /// # use tinyvec::*;
1764 /// let mut av = array_vec!([i32; 7] => 1, 2, 3);
1765 /// let v = av.drain_to_vec_and_reserve(10);
1766 /// assert_eq!(v, &[1, 2, 3]);
1767 /// assert_eq!(v.capacity(), 13);
1769 pub fn drain_to_vec_and_reserve(&mut self, n
: usize) -> Vec
<A
::Item
> {
1770 let cap
= n
+ self.len();
1771 let mut v
= Vec
::with_capacity(cap
);
1772 let iter
= self.iter_mut().map(take
);
1778 /// Tries to drain all elements to a Vec, but reserves additional space.
1782 /// If the allocator reports a failure, then an error is returned.
1785 /// # use tinyvec::*;
1786 /// let mut av = array_vec!([i32; 7] => 1, 2, 3);
1787 /// let v = av.try_drain_to_vec_and_reserve(10);
1788 /// assert!(matches!(v, Ok(_)));
1789 /// let v = v.unwrap();
1790 /// assert_eq!(v, &[1, 2, 3]);
1791 /// assert_eq!(v.capacity(), 13);
1793 #[cfg(feature = "rustc_1_57")]
1794 pub fn try_drain_to_vec_and_reserve(
1795 &mut self, n
: usize,
1796 ) -> Result
<Vec
<A
::Item
>, TryReserveError
> {
1797 let cap
= n
+ self.len();
1798 let mut v
= Vec
::new();
1799 v
.try_reserve(cap
)?
;
1800 let iter
= self.iter_mut().map(take
);
1806 /// Drains all elements to a Vec
1808 /// # use tinyvec::*;
1809 /// let mut av = array_vec!([i32; 7] => 1, 2, 3);
1810 /// let v = av.drain_to_vec();
1811 /// assert_eq!(v, &[1, 2, 3]);
1812 /// assert_eq!(v.capacity(), 3);
1814 pub fn drain_to_vec(&mut self) -> Vec
<A
::Item
> {
1815 self.drain_to_vec_and_reserve(0)
1818 /// Tries to drain all elements to a Vec.
1822 /// If the allocator reports a failure, then an error is returned.
1825 /// # use tinyvec::*;
1826 /// let mut av = array_vec!([i32; 7] => 1, 2, 3);
1827 /// let v = av.try_drain_to_vec();
1828 /// assert!(matches!(v, Ok(_)));
1829 /// let v = v.unwrap();
1830 /// assert_eq!(v, &[1, 2, 3]);
1831 /// // Vec may reserve more than necessary in order to prevent more future allocations.
1832 /// assert!(v.capacity() >= 3);
1834 #[cfg(feature = "rustc_1_57")]
1835 pub fn try_drain_to_vec(&mut self) -> Result
<Vec
<A
::Item
>, TryReserveError
> {
1836 self.try_drain_to_vec_and_reserve(0)
1840 #[cfg(feature = "serde")]
1841 struct ArrayVecVisitor
<A
: Array
>(PhantomData
<A
>);
1843 #[cfg(feature = "serde")]
1844 impl<'de
, A
: Array
> Visitor
<'de
> for ArrayVecVisitor
<A
>
1846 A
::Item
: Deserialize
<'de
>,
1848 type Value
= ArrayVec
<A
>;
1851 &self, formatter
: &mut core
::fmt
::Formatter
,
1852 ) -> core
::fmt
::Result
{
1853 formatter
.write_str("a sequence")
1856 fn visit_seq
<S
>(self, mut seq
: S
) -> Result
<Self::Value
, S
::Error
>
1860 let mut new_arrayvec
: ArrayVec
<A
> = Default
::default();
1862 let mut idx
= 0usize
;
1863 while let Some(value
) = seq
.next_element()?
{
1864 if new_arrayvec
.len() >= new_arrayvec
.capacity() {
1865 return Err(DeserializeError
::invalid_length(idx
, &self));
1867 new_arrayvec
.push(value
);