diff --git a/src/arrayvec.rs b/src/arrayvec.rs index 8c3c859..2d8dadb 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -194,7 +194,10 @@ impl ArrayVec { #[inline(always)] #[must_use] pub fn capacity(&self) -> usize { - A::CAPACITY + // Note: This shouldn't use A::CAPACITY, because unsafe code can't rely on + // any Array invariants. This ensures that at the very least, the returned + // value is a valid length for a subslice of the backing array. + self.data.as_slice().len() } /// Truncates the `ArrayVec` down to length 0. diff --git a/src/tinyvec.rs b/src/tinyvec.rs index 5bf9e05..5512704 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -212,7 +212,8 @@ impl TinyVec { #[must_use] pub fn capacity(&self) -> usize { match self { - TinyVec::Inline(_) => A::CAPACITY, + // Note: this shouldn't use A::CAPACITY. See ArrayVec::capacity(). + TinyVec::Inline(v) => v.capacity(), TinyVec::Heap(v) => v.capacity(), } }