From: "Gary Guo" <gary@garyguo.net>
To: "Lorenzo Delgado" <lnsdev@proton.me>,
"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>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: <linux-kernel@vger.kernel.org>, <rust-for-linux@vger.kernel.org>,
"Thorsten Blum" <blum@kernel.org>,
"Francis Laniel" <laniel_francis@npnm.eu>
Subject: Re: [PATCH v2 1/2] rust: non_empty: add NonEmpty
Date: Tue, 13 Oct 2026 14:54:39 +0100 [thread overview]
Message-ID: <DM3RF8H2M4WU.1CB0H9Z2A6WQY@garyguo.net> (raw)
In-Reply-To: <20261012-nonempty-slice-v2-1-0f2b67d9463e@proton.me>
On Tue Oct 13, 2026 at 2:49 PM BST, Lorenzo Delgado wrote:
> Some functions reject an empty container up front and later rely on it
> having at least one element. The type system does not see that check,
> so the later access ends up as an `unwrap_unchecked()` with a SAFETY
> comment pointing back at it, or as an `unwrap()` or an error path that
> can never be taken. `strcpy_into_buf()` in uaccess is one such case.
>
> Add `NonEmpty<C>`, a transparent wrapper that can only be created from
> a container its `IsEmpty` implementation reports as non-empty. It
> dereferences to the container, and a method whose `Option` only exists
> for the empty case can be shadowed by a version that returns the value
> directly.
>
> `DerefMut` is implemented per container, only where a mutable reference
> cannot change the length. For a growable container it would let
> `clear()` break the invariant.
>
> For now only slices are supported. A slice of unknown length goes
> through `try_from_mut()`, which returns `None` if it is empty. An array
> goes through `from_array_mut()`, which checks the length at compile
> time instead of returning an `Option`, so a caller with a fixed-size
> buffer needs no runtime check. The only accessor is `last_mut()`,
> which uaccess needs. Other containers and accessors can follow with
> their users.
>
> Suggested-by: Gary Guo <gary@garyguo.net>
> Link: https://lore.kernel.org/rust-for-linux/DM0D9TERQAM3.2Y3HV6P68E2N8@garyguo.net/
> Link: https://github.com/Rust-for-Linux/linux/issues/1262
> Signed-off-by: Lorenzo Delgado <lnsdev@proton.me>
> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/non_empty.rs | 122 +++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 123 insertions(+)
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 7225abc64084..8e1928deac35 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
>
> +impl<T> NonEmpty<[T]> {
> + /// Creates a [`NonEmpty`] slice from a mutable reference to an array.
> + ///
> + /// Unlike [`NonEmpty::try_from_mut`], this does not return an [`Option`]: the array length is
> + /// checked at compile time, and using it with an empty array is a build error.
> + #[inline]
> + pub fn from_array_mut<const N: usize>(array: &mut [T; N]) -> &mut Self {
Construction of array should be of form
&&mut [T; N] --construct--> &mut NonEmpty<[T; N]> --unsize coercion--> &mut NonEmpty<[T]>
Best,
Gary
> + const_assert!(N > 0);
> +
> + let slice: &mut [T] = array;
> + // INVARIANT: `slice` has `N` elements, and `N > 0` was checked at compile time.
> + // SAFETY: `NonEmpty<[T]>` is transparent to `[T]`, so the cast keeps both the layout and
> + // the pointer metadata, and the pointer comes from a valid `&mut [T]` with the same
> + // lifetime.
> + unsafe { &mut *(core::ptr::from_mut(slice) as *mut Self) }
> + }
next prev parent reply other threads:[~2026-10-13 13:54 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 13:49 [PATCH v2 0/2] rust: " Lorenzo Delgado
2026-10-13 13:49 ` [PATCH v2 1/2] rust: non_empty: " Lorenzo Delgado
2026-10-13 13:54 ` Gary Guo [this message]
2026-10-13 15:11 ` Lorenzo Delgado
2026-10-13 13:49 ` [PATCH v2 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf() Lorenzo Delgado
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=DM3RF8H2M4WU.1CB0H9Z2A6WQY@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=blum@kernel.org \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=laniel_francis@npnm.eu \
--cc=linux-kernel@vger.kernel.org \
--cc=lnsdev@proton.me \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--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®