[PATCH 2/2] rust: alloc: use unsafe_precondition_assert! in Vec length helpers

From: JX

Date: Tue Sep 08 2026 - 04:17:31 EST


Vec::inc_len and Vec::dec_len are unsafe functions whose debug
assertions directly check their documented safety preconditions.

Use unsafe_precondition_assert! for these checks so violations are
identified as unsafe precondition failures. The inc_len use also keeps
the const-compatible macro path covered by kernel compilation.

Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Link: https://github.com/Rust-for-Linux/linux/issues/1232
Signed-off-by: JX <1239989762@xxxxxx>
---
rust/kernel/alloc/kvec.rs | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index c7546b9da4..9216a4a304 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -23,7 +23,8 @@
page::{
AsPageIter,
PAGE_SIZE, //
- }, //
+ },
+ unsafe_precondition_assert, //
};

use core::{
@@ -230,7 +231,7 @@ pub const fn len(&self) -> usize {
#[inline]
pub const unsafe fn inc_len(&mut self, additional: usize) {
// Guaranteed by the type invariant to never underflow.
- debug_assert!(additional <= self.capacity() - self.len());
+ unsafe_precondition_assert!(additional <= self.capacity() - self.len());
// INVARIANT: By the safety requirements of this method this represents the exact number of
// elements stored within `self`.
self.len += additional;
@@ -245,7 +246,7 @@ pub const fn len(&self) -> usize {
///
/// - `count` must be less than or equal to `self.len`.
unsafe fn dec_len(&mut self, count: usize) -> &mut [T] {
- debug_assert!(count <= self.len());
+ unsafe_precondition_assert!(count <= self.len());
// INVARIANT: We relinquish ownership of the elements within the range `[self.len - count,
// self.len)`, hence the updated value of `set.len` represents the exact number of elements
// stored within `self`.
--
2.54.0