1+ // The ArrayVecCopy and ArrayVecCopy implementation
2+ //
3+ // NOTE: arrayvec.rs is the original source of both arrayvec.rs and arrayvec_copy.rs.
4+ // NOTE: Do not modify arrayvec_copy.rs manually. It is generated using the script
5+ // ./generate_arrayvec_copy.
6+ //
7+ // Any lines marked with a comment and then `DIRECTIVE ArrayVecCopy` will have that prefix removed
8+ // and have the rest of the line active in the ArrayVecCopy implementation.
19
210use std:: cmp;
311use std:: iter;
@@ -23,12 +31,11 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
2331use crate :: LenUint ;
2432use crate :: errors:: CapacityError ;
2533use crate :: arrayvec_impl:: ArrayVecImpl ;
34+ #[ cfg( not_in_arrayvec_copy) ]
35+ use crate :: utils:: MakeMaybeUninit ;
2636
2737/// A vector with a fixed capacity.
2838///
29- /// **Its only difference to [`ArrayVec`](crate::ArrayVec) is that its elements
30- /// are constrained to be `Copy` which allows it to be `Copy` itself.**
31- ///
3239/// The `ArrayVecCopy` is a vector backed by a fixed size array. It keeps track of
3340/// the number of initialized elements. The `ArrayVecCopy<T, CAP>` is parameterized
3441/// by `T` for the element type and `CAP` for the maximum capacity.
@@ -41,13 +48,25 @@ use crate::arrayvec_impl::ArrayVecImpl;
4148///
4249/// It offers a simple API but also dereferences to a slice, so that the full slice API is
4350/// available. The ArrayVecCopy can be converted into a by value iterator.
51+ #[ doc = "" ]
52+ #[ doc = "**ArrayVecCopy's only difference to [`\x41 rrayVec`](crate::\x41 rrayVec) is that its" ]
53+ #[ doc = "elements are constrained to be `Copy` which allows it to be `Copy` itself.** " ]
4454#[ repr( C ) ]
4555pub struct ArrayVecCopy < T : Copy , const CAP : usize > {
4656 len : LenUint ,
4757 // the `len` first elements of the array are initialized
4858 xs : [ MaybeUninit < T > ; CAP ] ,
4959}
5060
61+ #[ cfg( not_in_arrayvec_copy) ]
62+ impl < T : Copy , const CAP : usize > Drop for ArrayVecCopy < T , CAP > {
63+ fn drop ( & mut self ) {
64+ self . clear ( ) ;
65+
66+ // MaybeUninit inhibits array's drop
67+ }
68+ }
69+
5170macro_rules! panic_oob {
5271 ( $method_name: expr, $index: expr, $len: expr) => {
5372 panic!( concat!( "ArrayVecCopy::" , $method_name, ": index {} is out of bounds in vector of length {}" ) ,
@@ -81,6 +100,21 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
81100 }
82101 }
83102
103+ #[ cfg( not_in_arrayvec_copy) ]
104+ /// Create a new empty `ArrayVecCopy` (fn).
105+ ///
106+ /// The maximum capacity is given by the generic parameter `CAP`.
107+ ///
108+ /// ```
109+ /// use arrayvec::ArrayVecCopy;
110+ ///
111+ /// static ARRAY: ArrayVecCopy<u8, 1024> = ArrayVecCopy::new_const();
112+ /// ```
113+ pub fn new_const ( ) -> ArrayVecCopy < T , CAP > {
114+ assert_capacity_limit_const ! ( CAP ) ;
115+ ArrayVecCopy { xs : MakeMaybeUninit :: ARRAY , len : 0 }
116+ }
117+
84118 /// Return the number of elements in the `ArrayVecCopy`.
85119 ///
86120 /// ```
@@ -944,6 +978,22 @@ impl<T: Copy, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> {
944978
945979impl < T : Copy , const CAP : usize > ExactSizeIterator for IntoIter < T , CAP > { }
946980
981+ #[ cfg( not_in_arrayvec_copy) ]
982+ impl < T : Copy , const CAP : usize > Drop for IntoIter < T , CAP > {
983+ fn drop ( & mut self ) {
984+ // panic safety: Set length to 0 before dropping elements.
985+ let index = self . index ;
986+ let len = self . v . len ( ) ;
987+ unsafe {
988+ self . v . set_len ( 0 ) ;
989+ let elements = slice:: from_raw_parts_mut (
990+ self . v . get_unchecked_ptr ( index) ,
991+ len - index) ;
992+ ptr:: drop_in_place ( elements) ;
993+ }
994+ }
995+ }
996+
947997impl < T : Copy , const CAP : usize > Clone for IntoIter < T , CAP >
948998where T : Clone ,
949999{
0 commit comments