rust: alloc: add Vec::len() <= Vec::capacity invariant
authorTamir Duberstein <tamird@gmail.com>
Wed, 16 Apr 2025 17:15:40 +0000 (13:15 -0400)
committerDanilo Krummrich <dakr@kernel.org>
Wed, 23 Apr 2025 10:05:15 +0000 (12:05 +0200)
commit47a17a63f9e23f7e8f39d0965bcda8fee6c322f8
treef5c92037c954e0e3bae215c98ee8826af6ec6670
parent85f8e98dbb0135d2bc1999c6015cd374fe2c69fa
rust: alloc: add Vec::len() <= Vec::capacity invariant

Document the invariant that the vector's length is always less than or
equal to its capacity. This is already implied by these other
invariants:

- `self.len` always represents the exact number of elements stored in
  the vector.
- `self.layout` represents the absolute number of elements that can be
  stored within the vector without re-allocation.

but it doesn't hurt to spell it out. Note that the language references
`self.capacity` rather than `self.layout.len` as the latter is zero for
a vector of ZSTs.

Update a safety comment touched by this patch to correctly reference
`realloc` rather than `alloc` and replace "leaves" with "leave" to
improve grammar.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
Link: https://lore.kernel.org/r/20250416-vec-set-len-v4-1-112b222604cd@gmail.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/alloc/kvec.rs