What changed, and why it matters
This commit adds a new low-level helper function called `set_len` to an internal array-like data structure (`ArrayVec`). It is marked `unsafe` and is intended to let other internal code manipulate the length of the array directly, similar to how Rust's standard `Vec::set_len` works. The commit itself does not change any public behavior or fix a known bug; it is a small internal API addition.
Review future commits that call `set_len` to verify callers uphold the safety contract (capacity bound and initialized elements). Consider whether an additional `debug_assert!` for initialized elements is feasible, though this is typically caller-responsibility for `set_len`-style APIs.
Security signals we found
New unsafe API with documented safety preconditions
Potential for memory safety violations if safety contract is violated by future callers
No immediate caller introduced in this commit
Evidence from the diff
The patch introduces ArrayVec::set_len, an unsafe method that assigns self.len = new_len with a debug_assert! that new_len <= CAP. The safety contract requires callers to ensure new_len is within capacity and that all elements up to new_len are initialized. This mirrors Vec::set_len and moves length manipulation out of the module’s safety boundary, allowing callers to take responsibility for invariants. No call sites are added or modified in this commit.
Changed components
internals/src/array_vec.rsArrayVec::set_len methodInspect captured patch +11 / −0
diff --git a/internals/src/array_vec.rs b/internals/src/array_vec.rs
index c7e2be3b..090638a0 100644
--- a/internals/src/array_vec.rs
+++ b/internals/src/array_vec.rs
@@ -66,6 +66,17 @@ mod safety_boundary {
&mut self.data[self.len..]
}
+ /// Forces the length to `new_len`.
+ ///
+ /// # Safety
+ ///
+ /// * `new_len` must be less than or equal to `CAP`.
+ /// * All elements up to `new_len` must be initialized.
+ pub unsafe fn set_len(&mut self, new_len: usize) {
+ debug_assert!(new_len <= CAP);
+ self.len = new_len;
+ }
+
/// Adds an element into `self`.
///
/// # Panics
Why this scored 29/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.