implemnts the equivalent of the rust std's Vec::resize
on the kernel's Vec type.
Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
---
rust/kernel/alloc/kvec.rs | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 75e9feebb81f..cbfef2e56f9c 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -554,6 +554,31 @@ pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError> {
Ok(v)
}
+
+ /// Resizes the [`Vec`] so that `len` is equal to `new_len`.
+ ///
+ /// If `new_len` is smaller than `len`, the `Vec` is [`Vec::truncate`]d.
+ /// If `new_len` is larger, each new slot is filled with clones of `value`.
+ ///
+ /// # Example
+ /// ```
+ /// let mut v = kernel::kvec![1, 2, 3]?;
+ /// v.resize(1, 42, GFP_KERNEL)?;
+ /// assert_eq!(&v, &[1]);
+ ///
+ /// v.resize(3, 42, GFP_KERNEL)?;
+ /// assert_eq!(&v, &[1, 42, 42]);
+ ///
+ /// # Ok::<(), Error>(())
+ /// ```
+ pub fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), AllocError> {
+ if new_len > self.len() {
+ self.extend_with(new_len - self.len(), value, flags)
+ } else {
+ self.truncate(new_len);
+ Ok(())
+ }
+ }
}
impl<T, A> Drop for Vec<T, A>
--
2.48.1
On Sat, Mar 15, 2025 at 3:43 AM Andrew Ballance <andrewjballance@gmail.com> wrote: > > + /// # Example Nit: please use the plural for section headers. Thanks! Cheers, Miguel
On Fri, Mar 14, 2025 at 09:42:34PM -0500, Andrew Ballance wrote:
> implemnts the equivalent of the rust std's Vec::resize
> on the kernel's Vec type.
Nit: It is preferred to use the imperative form, i.e. "Implement the equivalent
[...]".
>
> Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
> ---
> rust/kernel/alloc/kvec.rs | 25 +++++++++++++++++++++++++
> 1 file changed, 25 insertions(+)
>
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 75e9feebb81f..cbfef2e56f9c 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -554,6 +554,31 @@ pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError> {
>
> Ok(v)
> }
> +
> + /// Resizes the [`Vec`] so that `len` is equal to `new_len`.
> + ///
> + /// If `new_len` is smaller than `len`, the `Vec` is [`Vec::truncate`]d.
> + /// If `new_len` is larger, each new slot is filled with clones of `value`.
> + ///
> + /// # Example
Nit: Please add an empty line after the heading.
> + /// ```
> + /// let mut v = kernel::kvec![1, 2, 3]?;
> + /// v.resize(1, 42, GFP_KERNEL)?;
> + /// assert_eq!(&v, &[1]);
> + ///
> + /// v.resize(3, 42, GFP_KERNEL)?;
> + /// assert_eq!(&v, &[1, 42, 42]);
> + ///
> + /// # Ok::<(), Error>(())
> + /// ```
> + pub fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), AllocError> {
> + if new_len > self.len() {
> + self.extend_with(new_len - self.len(), value, flags)
> + } else {
> + self.truncate(new_len);
> + Ok(())
> + }
> + }
> }
>
> impl<T, A> Drop for Vec<T, A>
> --
> 2.48.1
>
On Sat Mar 15, 2025 at 3:42 AM CET, Andrew Ballance wrote: > implemnts the equivalent of the rust std's Vec::resize > on the kernel's Vec type. > > Signed-off-by: Andrew Ballance <andrewjballance@gmail.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> --- Cheers, Benno > --- > rust/kernel/alloc/kvec.rs | 25 +++++++++++++++++++++++++ > 1 file changed, 25 insertions(+)
© 2016 - 2025 Red Hat, Inc.