Skip to content

Commit 3a2d9f7

Browse files
tamirdjannau
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 <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent a95d938 commit 3a2d9f7

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
@@ -188,20 +188,19 @@ where
188188
self.len
189189
}
190190

191-
/// Forcefully sets `self.len` to `new_len`.
191+
/// Increments `self.len` by `additional`.
192192
///
193193
/// # Safety
194194
///
195-
/// - `new_len` must be less than or equal to [`Self::capacity`].
196-
/// - If `new_len` is greater than `self.len`, all elements within the interval
197-
/// [`self.len`,`new_len`) must be initialized.
195+
/// - `additional` must be less than or equal to `self.capacity - self.len`.
196+
/// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized.
198197
#[inline]
199-
pub unsafe fn set_len(&mut self, new_len: usize) {
200-
debug_assert!(new_len <= self.capacity());
201-
202-
// INVARIANT: By the safety requirements of this method `new_len` represents the exact
203-
// number of elements stored within `self`.
204-
self.len = new_len;
198+
pub unsafe fn inc_len(&mut self, additional: usize) {
199+
// Guaranteed by the type invariant to never underflow.
200+
debug_assert!(additional <= self.capacity() - self.len());
201+
// INVARIANT: By the safety requirements of this method this represents the exact number of
202+
// elements stored within `self`.
203+
self.len += additional;
205204
}
206205

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

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

529528
Ok(())
530529
}
@@ -555,7 +554,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
555554
// the length by the same number.
556555
// - `self.len() + other.len() <= self.capacity()` is guaranteed by the preceding `reserve`
557556
// call.
558-
unsafe { self.set_len(self.len() + other.len()) };
557+
unsafe { self.inc_len(other.len()) };
559558
Ok(())
560559
}
561560

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
@@ -288,7 +288,7 @@ impl UserSliceReader {
288288

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

0 commit comments

Comments
 (0)