Skip to content

Commit a95d938

Browse files
tamirdjannau
authored andcommitted
rust: alloc: refactor Vec::truncate using dec_len
Use `checked_sub` to satisfy the safety requirements of `dec_len` and replace nearly the whole body of `truncate` with a call to `dec_len`. Reviewed-by: Andrew Ballance <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Remove #[expect(unused)] from dec_len(). - Danilo ] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent 5b10b0b commit a95d938

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

rust/kernel/alloc/kvec.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ where
212212
/// # Safety
213213
///
214214
/// - `count` must be less than or equal to `self.len`.
215-
#[expect(unused)]
216215
unsafe fn dec_len(&mut self, count: usize) -> &mut [T] {
217216
debug_assert!(count <= self.len());
218217
// INVARIANT: We relinquish ownership of the elements within the range `[self.len - count,
@@ -492,23 +491,15 @@ where
492491
/// # Ok::<(), Error>(())
493492
/// ```
494493
pub fn truncate(&mut self, len: usize) {
495-
if len >= self.len() {
496-
return;
494+
if let Some(count) = self.len().checked_sub(len) {
495+
// SAFETY: `count` is `self.len() - len` so it is guaranteed to be less than or
496+
// equal to `self.len()`.
497+
let ptr: *mut [T] = unsafe { self.dec_len(count) };
498+
499+
// SAFETY: the contract of `dec_len` guarantees that the elements in `ptr` are
500+
// valid elements whose ownership has been transferred to the caller.
501+
unsafe { ptr::drop_in_place(ptr) };
497502
}
498-
499-
let drop_range = len..self.len();
500-
501-
// SAFETY: `drop_range` is a subrange of `[0, len)` by the bounds check above.
502-
let ptr: *mut [T] = unsafe { self.get_unchecked_mut(drop_range) };
503-
504-
// SAFETY: By the above bounds check, it is guaranteed that `len < self.capacity()`.
505-
unsafe { self.set_len(len) };
506-
507-
// SAFETY:
508-
// - the dropped values are valid `T`s by the type invariant
509-
// - we are allowed to invalidate [`new_len`, `old_len`) because we just changed the
510-
// len, therefore we have exclusive access to [`new_len`, `old_len`)
511-
unsafe { ptr::drop_in_place(ptr) };
512503
}
513504
}
514505

0 commit comments

Comments
 (0)