diff --git a/src/arrayvec.rs b/src/arrayvec.rs index 1ab2f2c..cd67445 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -96,10 +96,13 @@ macro_rules! array_vec { /// /// let more_ints = ArrayVec::from_array_len([5, 6, 7, 8], 2); /// assert_eq!(more_ints.len(), 2); +/// +/// let no_ints: ArrayVec<[u8; 5]> = ArrayVec::from_array_empty([1, 2, 3, 4, 5]); +/// assert_eq!(no_ints.len(), 0); /// ``` #[repr(C)] #[derive(Clone, Copy)] -pub struct ArrayVec { +pub struct ArrayVec { len: u16, pub(crate) data: A, } @@ -954,6 +957,37 @@ impl ArrayVec { } } +impl ArrayVec { + /// Wraps up an array as a new empty `ArrayVec`. + /// + /// If you want to simply use the full array, use `from` instead. + /// + /// ## Examples + /// + /// This method in particular allows to create values for statics: + /// + /// ```rust + /// # use tinyvec::ArrayVec; + /// static DATA: ArrayVec<[u8; 5]> = ArrayVec::from_array_empty([0; 5]); + /// assert_eq!(DATA.len(), 0); + /// ``` + /// + /// But of course it is just an normal empty `ArrayVec`: + /// + /// ```rust + /// # use tinyvec::ArrayVec; + /// let mut data = ArrayVec::from_array_empty([1, 2, 3, 4]); + /// assert_eq!(&data[..], &[]); + /// data.push(42); + /// assert_eq!(&data[..], &[42]); + /// ``` + #[inline] + #[must_use] + pub const fn from_array_empty(data: A) -> Self { + Self { data, len: 0 } + } +} + #[cfg(feature = "grab_spare_slice")] impl ArrayVec { /// Obtain the shared slice of the array _after_ the active memory.