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.
1
9
2
10
use std:: cmp;
3
11
use std:: iter;
@@ -23,12 +31,11 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
23
31
use crate :: LenUint ;
24
32
use crate :: errors:: CapacityError ;
25
33
use crate :: arrayvec_impl:: ArrayVecImpl ;
34
+ #[ cfg( not_in_arrayvec_copy) ]
35
+ use crate :: utils:: MakeMaybeUninit ;
26
36
27
37
/// A vector with a fixed capacity.
28
38
///
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
- ///
32
39
/// The `ArrayVecCopy` is a vector backed by a fixed size array. It keeps track of
33
40
/// the number of initialized elements. The `ArrayVecCopy<T, CAP>` is parameterized
34
41
/// by `T` for the element type and `CAP` for the maximum capacity.
@@ -41,13 +48,25 @@ use crate::arrayvec_impl::ArrayVecImpl;
41
48
///
42
49
/// It offers a simple API but also dereferences to a slice, so that the full slice API is
43
50
/// 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.** " ]
44
54
#[ repr( C ) ]
45
55
pub struct ArrayVecCopy < T : Copy , const CAP : usize > {
46
56
len : LenUint ,
47
57
// the `len` first elements of the array are initialized
48
58
xs : [ MaybeUninit < T > ; CAP ] ,
49
59
}
50
60
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
+
51
70
macro_rules! panic_oob {
52
71
( $method_name: expr, $index: expr, $len: expr) => {
53
72
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> {
81
100
}
82
101
}
83
102
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
+
84
118
/// Return the number of elements in the `ArrayVecCopy`.
85
119
///
86
120
/// ```
@@ -944,6 +978,22 @@ impl<T: Copy, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> {
944
978
945
979
impl < T : Copy , const CAP : usize > ExactSizeIterator for IntoIter < T , CAP > { }
946
980
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
+
947
997
impl < T : Copy , const CAP : usize > Clone for IntoIter < T , CAP >
948
998
where T : Clone ,
949
999
{
0 commit comments