Skip to content

Commit 88d5d6a

Browse files
tamirdDanilo Krummrich
authored andcommitted
rust: alloc: replace Vec::set_len with inc_len
Rename `set_len` to `inc_len` and simplify its safety contract. Note that the usage in `CString::try_from_fmt` remains correct as the receiver is known to have `len == 0`. 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-4-112b222604cd@gmail.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 1b04b46 commit 88d5d6a

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

rust/kernel/alloc/kvec.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,19 @@ where
185185
self.len
186186
}
187187

188-
/// Forcefully sets `self.len` to `new_len`.
188+
/// Increments `self.len` by `additional`.
189189
///
190190
/// # Safety
191191
///
192-
/// - `new_len` must be less than or equal to [`Self::capacity`].
193-
/// - If `new_len` is greater than `self.len`, all elements within the interval
194-
/// [`self.len`,`new_len`) must be initialized.
192+
/// - `additional` must be less than or equal to `self.capacity - self.len`.
193+
/// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized.
195194
#[inline]
196-
pub unsafe fn set_len(&mut self, new_len: usize) {
197-
debug_assert!(new_len <= self.capacity());
198-
199-
// INVARIANT: By the safety requirements of this method `new_len` represents the exact
200-
// number of elements stored within `self`.
201-
self.len = new_len;
195+
pub unsafe fn inc_len(&mut self, additional: usize) {
196+
// Guaranteed by the type invariant to never underflow.
197+
debug_assert!(additional <= self.capacity() - self.len());
198+
// INVARIANT: By the safety requirements of this method this represents the exact number of
199+
// elements stored within `self`.
200+
self.len += additional;
202201
}
203202

204203
/// Decreases `self.len` by `count`.
@@ -317,7 +316,7 @@ where
317316
// SAFETY: We just initialised the first spare entry, so it is safe to increase the length
318317
// by 1. We also know that the new length is <= capacity because of the previous call to
319318
// `reserve` above.
320-
unsafe { self.set_len(self.len() + 1) };
319+
unsafe { self.inc_len(1) };
321320
Ok(())
322321
}
323322

@@ -521,7 +520,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
521520
// SAFETY:
522521
// - `self.len() + n < self.capacity()` due to the call to reserve above,
523522
// - the loop and the line above initialized the next `n` elements.
524-
unsafe { self.set_len(self.len() + n) };
523+
unsafe { self.inc_len(n) };
525524

526525
Ok(())
527526
}
@@ -552,7 +551,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
552551
// the length by the same number.
553552
// - `self.len() + other.len() <= self.capacity()` is guaranteed by the preceding `reserve`
554553
// call.
555-
unsafe { self.set_len(self.len() + other.len()) };
554+
unsafe { self.inc_len(other.len()) };
556555
Ok(())
557556
}
558557

rust/kernel/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ impl CString {
886886

887887
// SAFETY: The number of bytes that can be written to `f` is bounded by `size`, which is
888888
// `buf`'s capacity. The contents of the buffer have been initialised by writes to `f`.
889-
unsafe { buf.set_len(f.bytes_written()) };
889+
unsafe { buf.inc_len(f.bytes_written()) };
890890

891891
// Check that there are no `NUL` bytes before the end.
892892
// SAFETY: The buffer is valid for read because `f.bytes_written()` is bounded by `size`

rust/kernel/uaccess.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl UserSliceReader {
290290

291291
// SAFETY: Since the call to `read_raw` was successful, so the next `len` bytes of the
292292
// vector have been initialized.
293-
unsafe { buf.set_len(buf.len() + len) };
293+
unsafe { buf.inc_len(len) };
294294
Ok(())
295295
}
296296
}

0 commit comments

Comments
 (0)