rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: Alice Ryhl <aliceryhl@google.com>, Danilo Krummrich <dakr@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 5/5] rust: alloc: add Vec::retain
Date: Thu, 20 Mar 2025 22:21:56 +0000	[thread overview]
Message-ID: <D8LFZZXODIR4.5XCGWBP93NNX@proton.me> (raw)

On Thu Mar 20, 2025 at 2:53 PM CET, Alice Ryhl wrote:
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 303198509885f5e24b74da5a92382b518de3e1c0..00dabea8ea6c8a742a7fc95954d8de58be124493 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -588,6 +588,20 @@ pub fn drain_all(&mut self) -> DrainAll<'_, T> {
>              elements: self.spare_capacity_mut()[..len].iter_mut(),
>          }
>      }
> +
> +    /// Removes all elements that don't match the provided closure.

Can you also add an example here? Otherwise the code looks good.

---
Cheers,
Benno

> +    pub fn retain(&mut self, mut f: impl FnMut(&mut T) -> bool) {
> +        let mut num_kept = 0;
> +        let mut next_to_check = 0;
> +        while let Some(to_check) = self.get_mut(next_to_check) {
> +            if f(to_check) {
> +                self.swap(num_kept, next_to_check);
> +                num_kept += 1;
> +            }
> +            next_to_check += 1;
> +        }
> +        self.truncate(num_kept);
> +    }
>  }
>  
>  impl<T: Clone, A: Allocator> Vec<T, A> {



             reply	other threads:[~2025-03-20 22:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-20 22:21 Benno Lossin [this message]
  -- strict thread matches above, loose matches on Subject: below --
2025-03-20 13:52 [PATCH 0/5] Additional methods for Vec 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

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=D8LFZZXODIR4.5XCGWBP93NNX@proton.me \
    --to=benno.lossin@proton.me \
    --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).