[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aBJC_Ef1lKHNx4dv@pollux>
Date: Wed, 30 Apr 2025 17:34:20 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: Matthew Maurer <mmaurer@...gle.com>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org, Boqun Feng <boqun.feng@...il.com>
Subject: Re: [PATCH v4 3/7] rust: alloc: add Vec::push_within_capacity
On Tue, Apr 29, 2025 at 02:44:23PM +0000, Alice Ryhl wrote:
>
> + /// Appends an element to the back of the [`Vec`] instance without reallocating.
> + ///
> + /// Fails if the vector does not have capacity for the new element.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// let mut v = KVec::with_capacity(10, GFP_KERNEL)?;
> + /// for i in 0..10 {
> + /// v.push_within_capacity(i).unwrap();
I'd prefer to make this
v.push_within_capacity(i).map_err(|_| ENOMEM)?;
instead.
> + /// }
> + ///
> + /// assert!(v.push_within_capacity(10).is_err());
> + /// # Ok::<(), Error>(())
> + /// ```
> + pub fn push_within_capacity(&mut self, v: T) -> Result<(), T> {
> + if self.len() < self.capacity() {
> + // SAFETY: The length is less than the capacity.
> + unsafe { self.push_within_capacity_unchecked(v) };
> + Ok(())
> + } else {
> + Err(v)
> + }
> + }
>
> + /// Appends an element to the back of the [`Vec`] instance without reallocating.
> + ///
> + /// # Safety
> + ///
> + /// The length must be less than the capacity.
NIT: Maybe be more specific and say:
"`self.len()` must be less than `self.capacity()`."
Powered by blists - more mailing lists