From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Eliot Courtney" <ecourtney@nvidia.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Onur Özkan" <work@onurozkan.dev>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"John Hubbard" <jhubbard@nvidia.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Timur Tabi" <ttabi@nvidia.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v4 2/7] rust: alloc: add ArrayVec
Date: Mon, 26 Oct 2026 14:25:08 +0900 [thread overview]
Message-ID: <DMEIQ7CIQSGP.2HW2RV3W54MTV@nvidia.com> (raw)
In-Reply-To: <20261016-b4-nvkv-v4-2-e8b92b91aa5b@nvidia.com>
On Fri Oct 16, 2026 at 10:53 AM JST, Eliot Courtney wrote:
<...>
> +/// A fixed capacity vector that holds at most `N` elements.
> +///
> +/// # Invariants
> +///
> +/// - `len` is at most `N`.
> +/// - The first `len` elements of `data` are initialized.
> +///
> +/// # Examples
> +///
> +/// ```
> +/// use kernel::alloc::ArrayVec;
> +///
> +/// let mut v = ArrayVec::<u8, 4>::new();
> +/// v.extend_from_slice(b"abc")?;
> +/// assert_eq!(*v, *b"abc");
> +///
> +/// assert!(v.extend_from_slice(b"ab").is_err());
> +///
> +/// v.push(4u8)?;
> +/// assert_eq!(*v, *b"abc\x04");
> +/// assert!(v.push(5u8).is_err());
> +///
> +/// v.clear();
> +/// assert!(v.is_empty());
> +/// # Ok::<(), Error>(())
> +/// ```
> +#[derive(Zeroable)]
Do we need to derive `Zeroable` here? AFAICT we have no user and this
forces `T` to implement `Zeroable` as well. Worse, the derived
implementaiton does not check against `MAX_STACK_SIZE` before returning
the built value on the stack.
We also have `new` and `Default` that do basically the same thing,
without the constraint on `T`.
On a related note, it would be nice to have a method to initialize an
empty ArrayVec in-place. Currently we have two instances of
`ArrayVec::init_with::<Infallible>(|_| Ok(()))` in this series, which
imho justifies having a dedicated `init_empty` method for that.
> +pub struct ArrayVec<T, const N: usize> {
> + data: [MaybeUninit<T>; N],
> + len: usize,
> +}
> +
> +/// The maximum size in bytes of an [`ArrayVec`] returned by value from [`ArrayVec::new`].
> +const MAX_STACK_SIZE: usize = 512;
> +
> +impl<T, const N: usize> ArrayVec<T, N> {
> + /// Creates an empty [`ArrayVec`].
> + #[inline]
> + pub const fn new() -> Self {
> + const_assert!(
> + size_of::<Self>() <= MAX_STACK_SIZE,
> + "use `init_with` instead of constructing a large ArrayVec on the stack"
> + );
> +
> + // INVARIANT: An empty ArrayVec trivially has all its elements initialized.
> + Self {
> + data: [const { MaybeUninit::uninit() }; N],
> + len: 0,
> + }
> + }
> +
> + /// Creates an initializer for an [`ArrayVec`] populated by `f`.
> + ///
> + /// `f` gets an empty [`ArrayVec`] and can fill it in place.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::alloc::ArrayVec;
> + ///
> + /// let v = KBox::init(
> + /// ArrayVec::<u8, 4096>::init_with(|v| v.extend_from_slice(b"abc")),
> + /// GFP_KERNEL,
> + /// )?;
> + /// assert_eq!(**v, *b"abc");
> + /// # Ok::<(), Error>(())
> + /// ```
> + #[inline]
> + pub fn init_with<E>(f: impl FnOnce(&mut Self) -> Result<(), E>) -> impl Init<Self, E> {
> + let init_empty = |slot: *mut Self| {
> + // SAFETY: By the initializer contract `slot` is valid for writes. Once `len` is zero
> + // the slot holds a valid empty ArrayVec, since `data` requires no initialization.
> + // INVARIANT: An empty ArrayVec trivially has all its elements initialized.
> + unsafe { (&raw mut (*slot).len).write(0) };
> + Ok(())
> + };
> +
> + // SAFETY: `init_empty` fully initializes the slot and never fails.
> + unsafe { init_from_closure(init_empty) }.chain(f)
> + }
> +
> + /// Appends an element to the back of the [`ArrayVec`].
> + ///
> + /// Fails when the [`ArrayVec`] is full, handing the element back in [`PushError`].
> + #[inline]
> + pub fn push(&mut self, v: T) -> Result<(), PushError<T>> {
> + self.try_push_init(v)
> + .map_err(|TryPushInitError::Full(v)| PushError(v))
nit: could also be `self.push_init(v)`.
Pretty minor stuff, so as long as my first comment is somehow addressed:
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
next prev parent reply other threads:[~2026-10-26 5:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-16 1:53 [PATCH v4 0/7] gpu: nova-core: add NVKV codec Eliot Courtney
2026-10-16 1:53 ` [PATCH v4 1/7] rust: alloc: add Vec::try_push_init and Vec::push_init Eliot Courtney
2026-10-21 12:30 ` Alexandre Courbot
2026-10-16 1:53 ` [PATCH v4 2/7] rust: alloc: add ArrayVec Eliot Courtney
2026-10-26 5:25 ` Alexandre Courbot [this message]
2026-10-16 1:53 ` [PATCH v4 3/7] gpu: nova-core: add NVKV encoder Eliot Courtney
2026-10-26 6:00 ` Alexandre Courbot
2026-10-16 1:53 ` [PATCH v4 4/7] gpu: nova-core: add NVKV decoder Eliot Courtney
2026-10-26 6:29 ` Alexandre Courbot
2026-10-16 1:53 ` [PATCH v4 5/7] gpu: nova-core: add NVKV typed encoding Eliot Courtney
2026-10-26 6:45 ` Alexandre Courbot
2026-10-16 1:53 ` [PATCH v4 6/7] gpu: nova-core: add NVKV typed decoding Eliot Courtney
2026-10-26 12:29 ` Alexandre Courbot
2026-10-16 1:53 ` [PATCH v4 7/7] gpu: nova-core: add NVKV GSP_INIT schemas Eliot Courtney
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=DMEIQ7CIQSGP.2HW2RV3W54MTV@nvidia.com \
--to=acourbot@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=apopple@nvidia.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=ecourtney@nvidia.com \
--cc=gary@garyguo.net \
--cc=jhubbard@nvidia.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
--cc=urezki@gmail.com \
--cc=vbabka@kernel.org \
--cc=work@onurozkan.dev \
/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
all inboxes | Powered by JetHome®