From: Andrew Ballance <andrewjballance@gmail.com>
To: dakr@kernel.org, airlied@gmail.com, simona@ffwll.ch,
maarten.lankhorst@linux.intel.com, mripard@kernel.org,
tzimmermann@suse.de, corbet@lwn.net, ojeda@kernel.org,
alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
bjorn3_gh@protonmail.com, benno.lossin@proton.me,
a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
andrewjballance@gmail.com, acourbot@nvidia.com,
nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
rust-for-linux@vger.kernel.org
Subject: [PATCH 2/3] rust: alloc: add Vec::resize method
Date: Fri, 14 Mar 2025 21:42:34 -0500 [thread overview]
Message-ID: <20250315024235.5282-3-andrewjballance@gmail.com> (raw)
In-Reply-To: <20250315024235.5282-1-andrewjballance@gmail.com>
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
next prev parent reply other threads:[~2025-03-15 2:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-15 2:42 [PATCH 0/3] rust: alloc: add Vec::resize and Vec::truncate Andrew Ballance
2025-03-15 2:42 ` [PATCH 1/3] rust: alloc: add Vec::truncate method Andrew Ballance
2025-03-15 10:09 ` Benno Lossin
2025-03-15 11:15 ` Andrew Ballance
2025-03-15 18:04 ` Benno Lossin
2025-03-15 15:49 ` Danilo Krummrich
2025-03-15 2:42 ` Andrew Ballance [this message]
2025-03-15 10:10 ` [PATCH 2/3] rust: alloc: add Vec::resize method Benno Lossin
2025-03-15 14:58 ` Danilo Krummrich
2025-03-15 19:52 ` Miguel Ojeda
2025-03-15 2:42 ` [PATCH 3/3] gpu: nova-core: remove completed Vec extentions from task list Andrew Ballance
2025-03-15 14:50 ` [PATCH 0/3] rust: alloc: add Vec::resize and Vec::truncate Danilo Krummrich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250315024235.5282-3-andrewjballance@gmail.com \
--to=andrewjballance@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=nouveau@lists.freedesktop.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.