Rust for Linux List
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@kernel.org>
To: Bernhard Kauer <bk@alpico.io>
Cc: rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: introduce KVec::pop()
Date: Mon, 2 Dec 2024 10:54:08 +0100	[thread overview]
Message-ID: <Z02DwAhVx6k7NRXr@pollux> (raw)
In-Reply-To: <20241202085758.67319-1-bk@alpico.io>

On Mon, Dec 02, 2024 at 09:57:58AM +0100, Bernhard Kauer wrote:
> Provides a simple stack together with push().

Your commit message should also explain the motivation of the patch, i.e. what
do you need this new function for.

> 
> Signed-off-by: Bernhard Kauer <bk@alpico.io>
> ---
>  rust/kernel/alloc/kvec.rs | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
> 
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index ae9d072741ce..ba6c265ad5c5 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -302,6 +302,34 @@ pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
>          Ok(())
>      }
>  
> +    /// Removes the last element from the [`Vec`] instance.

It not only removes it, but also returns the corresponding element (or `None` if
the vector is empty). Please add this to the documentation.

It would also be worth noting that users may want to shrink the vector after
removing elements, however, we don't implement the `shrink_*` functions yet.

> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// let mut v = KVec::new();
> +    /// v.push(1, GFP_KERNEL)?;
> +    /// v.push(2, GFP_KERNEL)?;
> +    /// assert_eq!(Some(2), v.pop());
> +    /// assert_eq!(Some(1), v.pop());
> +    /// assert_eq!(None, v.pop());
> +    /// assert!(v.is_empty());
> +    /// ```
> +    pub fn pop(&mut self) -> Option<T> {
> +        if self.len() == 0 {
> +            return None;
> +        }
> +        self.len -= 1;
> +
> +        // SAFETY:
> +        // - `ptr` is valid because there is at least one entry stored.
> +        let ptr = unsafe { self.as_mut_ptr().add(self.len) };
> +
> +        // SAFETY:
> +        // - `ptr` is properly aligned and valid for reads.
> +        Some(unsafe { core::ptr::read(ptr) })
> +    }
> +
>      /// Creates a new [`Vec`] instance with at least the given capacity.
>      ///
>      /// # Examples
> -- 
> 2.45.2
> 

  parent reply	other threads:[~2024-12-02  9:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-02  8:57 [PATCH] rust: introduce KVec::pop() Bernhard Kauer
2024-12-02  9:06 ` Greg KH
2024-12-02  9:54   ` Bernhard Kauer
2024-12-02 10:22     ` Danilo Krummrich
2024-12-02 18:30       ` Miguel Ojeda
2024-12-02  9:54 ` Danilo Krummrich [this message]
2024-12-02 18:28 ` Miguel Ojeda
2024-12-03  2:55 ` kernel test robot

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=Z02DwAhVx6k7NRXr@pollux \
    --to=dakr@kernel.org \
    --cc=bk@alpico.io \
    --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