[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAJ-ks9kAROOfyPtxMe6LE4-UPsvXca2sQ2VDjhRchZp3HLddGg@mail.gmail.com>
Date: Tue, 18 Mar 2025 16:12:53 -0400
From: Tamir Duberstein <tamird@...il.com>
To: Andrew Ballance <andrewjballance@...il.com>
Cc: dakr@...nel.org, airlied@...il.com, simona@...ll.ch,
maarten.lankhorst@...ux.intel.com, mripard@...nel.org, tzimmermann@...e.de,
corbet@....net, ojeda@...nel.org, alex.gaynor@...il.com, boqun.feng@...il.com,
gary@...yguo.net, bjorn3_gh@...tonmail.com, benno.lossin@...ton.me,
a.hindborg@...nel.org, aliceryhl@...gle.com, tmgross@...ch.edu,
acourbot@...dia.com, nouveau@...ts.freedesktop.org,
dri-devel@...ts.freedesktop.org, linux-doc@...r.kernel.org,
linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v2 2/3] rust: alloc: add Vec::resize method
On Sun, Mar 16, 2025 at 7:17 AM Andrew Ballance
<andrewjballance@...il.com> wrote:
>
> implement the equivalent of the rust std's Vec::resize
> on the kernel's Vec type.
>
> Signed-off-by: Andrew Ballance <andrewjballance@...il.com>
> ---
> rust/kernel/alloc/kvec.rs | 26 ++++++++++++++++++++++++++
> 1 file changed, 26 insertions(+)
>
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 18bcc59f0b38..eb6d40a1bf8b 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -554,6 +554,32 @@ 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`.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// 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(())
> + }
> + }
You can avoid underflow checking in debug builds by using `checked_sub`:
match new_len.checked_sub(self.len()) {
Some(n) => self.extend_with(n, value, flags),
None => {
self.truncate(new_len);
Ok(())
}
}
> }
>
> impl<T, A> Drop for Vec<T, A>
> --
> 2.48.1
>
>
Either way:
Reviewed-by: Tamir Duberstein <tamird@...il.com>
Powered by blists - more mailing lists