Skip to content

Commit

Permalink
Fix ArrayVec capacity not taking u16::MAX into account (#178)
Browse files Browse the repository at this point in the history
The capacity is limited by the backing array and u16::MAX because the
length is stored as a u16. This was not taken into account in the
`fn capacity` implementation.
  • Loading branch information
e00E committed Feb 22, 2023
1 parent 3515768 commit 48c004d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ macro_rules! array_vec {

/// An array-backed, vector-like data structure.
///
/// * `ArrayVec` has a fixed capacity, equal to the array size. Note that not
/// all capacities are necessarily supported by default. See comments in
/// [`Array`].
/// * `ArrayVec` has a fixed capacity, equal to the minimum of the array size
/// and `u16::MAX`. Note that not all capacities are necessarily supported by
/// default. See comments in [`Array`].
/// * `ArrayVec` has a variable length, as you add and remove elements. Attempts
/// to fill the vec beyond its capacity will cause a panic.
/// * All of the vec's array slots are always initialized in terms of Rust's
Expand Down Expand Up @@ -340,7 +340,7 @@ impl<A: Array> ArrayVec<A> {
// 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()
self.data.as_slice().len().min(u16::MAX as usize)
}

/// Truncates the `ArrayVec` down to length 0.
Expand Down

0 comments on commit 48c004d

Please sign in to comment.