From: Tamir Duberstein <tamird@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: Danilo Krummrich <dakr@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/5] rust: alloc: add Vec::drain_all
Date: Thu, 20 Mar 2025 18:12:50 -0400 [thread overview]
Message-ID: <CAJ-ks9mM6cb_b0G_ZZh+tnijOPo1imWv-bzhgrLpGcNXMsMjBA@mail.gmail.com> (raw)
In-Reply-To: <20250320-vec-methods-v1-4-7dff5cf25fe8@google.com>
On Thu, Mar 20, 2025 at 9:56 AM Alice Ryhl <aliceryhl@google.com> wrote:
>
> This is like the stdlib method drain, except that it's hard-coded to use
> the entire vector's range. Rust Binder uses it in the range allocator to
> take ownership of everything in a vector in a case where reusing the
> vector is desirable.
>
> Implementing `DrainAll` in terms of `slice::IterMut` lets us reuse some
> nice optimizations in core for the case where T is a ZST.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> rust/kernel/alloc/kvec.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 57 insertions(+)
>
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index df930ff0d0b85b8b03c9b7932a2b31dfb62612ed..303198509885f5e24b74da5a92382b518de3e1c0 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -564,6 +564,30 @@ pub fn truncate(&mut self, len: usize) {
> // len, therefore we have exclusive access to [`new_len`, `old_len`)
> unsafe { ptr::drop_in_place(ptr) };
> }
> +
> + /// Takes ownership of all items in this vector without consuming the allocation.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// let mut v = kernel::kvec![0, 1, 2, 3]?;
> + ///
> + /// for (i, j) in v.drain_all().enumerate() {
> + /// assert_eq!(i, j);
> + /// }
> + ///
> + /// assert!(v.capacity() >= 4);
> + /// ```
> + pub fn drain_all(&mut self) -> DrainAll<'_, T> {
> + let len = self.len();
> + // INVARIANT: The first 0 elements are valid.
> + self.len = 0;
Could you use `self.dec_len(self.len)` here? Then you'd have a &mut
[T] rather than `MaybeUninit`. Provided you agree `dec_len` is sound,
of course.
Link: https://lore.kernel.org/rust-for-linux/20250318-vec-set-len-v2-2-293d55f82d18@gmail.com/
next prev parent reply other threads:[~2025-03-20 22:13 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-20 13:52 [PATCH 0/5] Additional methods for Vec Alice Ryhl
2025-03-20 13:52 ` [PATCH 1/5] rust: alloc: add Vec::clear Alice Ryhl
2025-03-20 22:01 ` Benno Lossin
2025-03-20 22:04 ` Tamir Duberstein
2025-03-20 13:52 ` [PATCH 2/5] rust: alloc: add Vec::pop Alice Ryhl
2025-03-20 13:52 ` [PATCH 3/5] rust: alloc: add Vec::push_within_capacity Alice Ryhl
2025-03-20 22:17 ` Benno Lossin
2025-03-21 15:22 ` Tamir Duberstein
2025-03-20 13:52 ` [PATCH 4/5] rust: alloc: add Vec::drain_all Alice Ryhl
2025-03-20 22:12 ` Tamir Duberstein [this message]
2025-03-21 7:41 ` Alice Ryhl
2025-03-20 13:53 ` [PATCH 5/5] rust: alloc: add Vec::retain Alice Ryhl
2025-03-21 15:24 ` Tamir Duberstein
2025-04-22 9:48 ` Alice Ryhl
2025-03-21 12:11 ` [PATCH 0/5] Additional methods for Vec Alice Ryhl
-- strict thread matches above, loose matches on Subject: below --
2025-03-20 22:06 [PATCH 4/5] rust: alloc: add Vec::drain_all Benno Lossin
2025-03-21 7:54 ` Alice Ryhl
2025-03-21 9:52 ` Benno Lossin
2025-03-21 9:53 Benno Lossin
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=CAJ-ks9mM6cb_b0G_ZZh+tnijOPo1imWv-bzhgrLpGcNXMsMjBA@mail.gmail.com \
--to=tamird@gmail.com \
--cc=aliceryhl@google.com \
--cc=dakr@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rust-for-linux@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).