Skip to content

Commit a1e4d5c

Browse files
DarksonnDanilo Krummrich
authored andcommitted
rust: alloc: add Vec::clear
Our custom Vec type is missing the stdlib method `clear`, thus add it. It will be used in the miscdevice sample. Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Tamir Duberstein <tamird@gmail.com> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20250502-vec-methods-v5-1-06d20ad9366f@google.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 88d5d6a commit a1e4d5c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

rust/kernel/alloc/kvec.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,26 @@ where
413413
(ptr, len, capacity)
414414
}
415415

416+
/// Clears the vector, removing all values.
417+
///
418+
/// Note that this method has no effect on the allocated capacity
419+
/// of the vector.
420+
///
421+
/// # Examples
422+
///
423+
/// ```
424+
/// let mut v = kernel::kvec![1, 2, 3]?;
425+
///
426+
/// v.clear();
427+
///
428+
/// assert!(v.is_empty());
429+
/// # Ok::<(), Error>(())
430+
/// ```
431+
#[inline]
432+
pub fn clear(&mut self) {
433+
self.truncate(0);
434+
}
435+
416436
/// Ensures that the capacity exceeds the length by at least `additional` elements.
417437
///
418438
/// # Examples

0 commit comments

Comments
 (0)