rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Ballance <andrewjballance@gmail.com>
To: benno.lossin@proton.me
Cc: a.hindborg@kernel.org, acourbot@nvidia.com, airlied@gmail.com,
	alex.gaynor@gmail.com, aliceryhl@google.com,
	andrewjballance@gmail.com, bjorn3_gh@protonmail.com,
	boqun.feng@gmail.com, corbet@lwn.net, dakr@kernel.org,
	dri-devel@lists.freedesktop.org, gary@garyguo.net,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	maarten.lankhorst@linux.intel.com, mripard@kernel.org,
	nouveau@lists.freedesktop.org, ojeda@kernel.org,
	rust-for-linux@vger.kernel.org, simona@ffwll.ch,
	tmgross@umich.edu, tzimmermann@suse.de
Subject: Re: [PATCH 1/3] rust: alloc: add Vec::truncate method
Date: Sat, 15 Mar 2025 06:15:11 -0500	[thread overview]
Message-ID: <20250315111511.107047-1-andrewjballance@gmail.com> (raw)
In-Reply-To: <D8GRAC8YQIVC.2LS1EIIIRZU3I@proton.me>

On Sat, Mar 15, 2025 at 10:09:26AM +0000, Benno Lossin wrote:
> On Sat Mar 15, 2025 at 3:42 AM CET, Andrew Ballance wrote:
> > implements the equivalent to the std's Vec::truncate
> > on the kernel's Vec type.
> >
> > Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
> > ---
> >  rust/kernel/alloc/kvec.rs | 36 ++++++++++++++++++++++++++++++++++++
> >  1 file changed, 36 insertions(+)
> >
> > diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> > index ae9d072741ce..75e9feebb81f 100644
> > --- a/rust/kernel/alloc/kvec.rs
> > +++ b/rust/kernel/alloc/kvec.rs
> > @@ -452,6 +452,42 @@ pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocEr
> >  
> >          Ok(())
> >      }
> > +
> > +    /// Shortens the vector, setting the length to `len` and drops the removed values.
> > +    /// If `len` is greater than or equal to the current length, this does nothing.
> > +    ///
> > +    /// This has no effect on the capacity and will not allocate.
> > +    /// # Examples
> > +    /// ```
> > +    /// let mut v = kernel::kvec![1, 2, 3]?;
> > +    /// v.truncate(1);
> > +    /// assert_eq!(v.len(), 1);
> > +    /// assert_eq!(&v, &[1]);
> > +    ///
> > +    /// # Ok::<(), Error>(())
> > +    /// ```
> > +    pub fn truncate(&mut self, len: usize) {
> > +        if len >= self.len() {
> > +            return;
> > +        }
> > +
> > +        // [new_len, len) is guaranteed to be valid because [0, len) is guaranteed to be valid
> > +        let drop_range = len..self.len();
> > +
> > +        // SAFETY:
> > +        // we can safely ignore the bounds check because we already did our own check
> > +        let ptr: *mut [T] = unsafe { self.get_unchecked_mut(drop_range) };
> 
> What's this `get_unchecked_mut` method, I don't see it in `rust-next` or
> `alloc-next`.

Vec derefs into a slice which implements get_uncheked_mut
https://rust.docs.kernel.org/next/kernel/alloc/kvec/struct.Vec.html#method.get_unchecked_mut

> > +
> > +        // SAFETY:
> > +        // it is safe to shrink the length because the new length is
> > +        // guaranteed to be less than the old length
> 
> Please take a look at the documentation of `set_len`, in the safety
> section you'll find what you need to justify here.
> 
> > +        unsafe { self.set_len(len) };
> > +
> > +        // SAFETY:
> 
> A couple points missing:
> - why is the pointer valid?
> 
> > +        // - the dropped values are valid `T`s
> > +        // - we are allowed to invalidate [new_len, old_len) because we just changed the len
> 
> This should justify why the value will never be accessed again.
> 

I will fixup the safety comments for the v2. Thanks.

> ---
> Cheers,
> Benno
> 
> > +        unsafe { ptr::drop_in_place(ptr) };
> > +    }
> >  }
> >  
> >  impl<T: Clone, A: Allocator> Vec<T, A> {
> 
> 

  reply	other threads:[~2025-03-15 11:15 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 [this message]
2025-03-15 18:04       ` Benno Lossin
2025-03-15 15:49   ` Danilo Krummrich
2025-03-15  2:42 ` [PATCH 2/3] rust: alloc: add Vec::resize method Andrew Ballance
2025-03-15 10:10   ` 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=20250315111511.107047-1-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 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).