mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Lorenzo Delgado" <lnsdev@proton.me>
Cc: "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>,
	"Onur Özkan" <work@onurozkan.dev>,
	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 v3 0/2] rust: add NonEmpty
Date: Thu, 15 Oct 2026 15:08:22 +0100	[thread overview]
Message-ID: <DM5GYU1H0Q9I.308S84Y1RV33P@garyguo.net> (raw)
In-Reply-To: <DM5FG6VMG3U7.1XMZW8RJI2KLM@nvidia.com>

On Thu Oct 15, 2026 at 1:57 PM BST, Alexandre Courbot wrote:
> On Wed Oct 14, 2026 at 5:30 PM JST, Lorenzo Delgado wrote:
>> Some functions check that a container is not empty and later rely on it
>> through `unwrap_unchecked()`, `unwrap()` or an error path that can't be
>> taken. Gary suggested a non-empty type for this while reviewing Thorsten
>> Blum's uaccess patch [1], and Miguel filed it as #1262 [2].
>>
>> Patch 1 adds `NonEmpty<C>` in `kernel::non_empty`, with only what the
>> first user needs: slices, `try_from_mut()`, `from_mut()` for arrays
>> and `last_mut()`. Patch 2 makes `strcpy_into_buf()` take a
>> `NonEmpty<[u8]>`, which drops an `unsafe` block and the `EINVAL` case.
>>
>> Some notes:
>>
>> - `IsEmpty` is a safe trait. Unsafe code only relies on the
>>   implementations in `kernel::non_empty`, and the docs require the
>>   result not to change while the container is only reachable through a
>>   shared reference. If a generic accessor ever needs to rely on it, it
>>   can become an `unsafe trait` then.
>>
>> - Since APIs aren't added without users, other accessors (`first()`,
>>   `try_from_ref()`, ...) and containers are left for later. I plan to
>>   send follow-ups for `net/phy.rs` and `scatterlist.rs`, which both
>>   reject an empty slice and then rely on it.
>
> Having `last` without `first` is weird. Please add the minimal set that
> makes sense from the get-go, these are `pub` anyway so clippy won't
> complain if they are unused.
>
>>
>> - I put it in its own `kernel::non_empty` module. Would a broader home
>>   like `kernel::collections` be better?
>
> `kernel::types` sounds like a proper place? Or if we want a dedicated
> module, `kernel::slice` to match the standard library? `non_empty` is
> too ad-hoc and specific.

`kernel::types` is too generic and becomes a dumping ground of things. I wonder
if we should group things that are new types with just range restrictions
(together things like NonZero, Bounded, and perhaps eventually Range, NonNeg and
others).

Best,
Gary


  reply	other threads:[~2026-10-15 14:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  8:30 Lorenzo Delgado
2026-10-14  8:30 ` [PATCH v3 1/2] rust: non_empty: " Lorenzo Delgado
2026-10-14 11:55   ` Miguel Ojeda
2026-10-14 14:25     ` Gary Guo
2026-10-15 12:43       ` Alexandre Courbot
2026-10-15 14:02         ` Gary Guo
2026-10-15 14:33           ` Alexandre Courbot
2026-10-15 14:08       ` Miguel Ojeda
2026-10-14  8:30 ` [PATCH v3 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf() Lorenzo Delgado
2026-10-15 13:06   ` Alexandre Courbot
2026-10-15 14:04     ` Gary Guo
2026-10-15 12:57 ` [PATCH v3 0/2] rust: add NonEmpty Alexandre Courbot
2026-10-15 14:08   ` Gary Guo [this message]
2026-10-15 14:11     ` Miguel Ojeda
2026-10-15 14:35       ` Alexandre Courbot

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=DM5GYU1H0Q9I.308S84Y1RV33P@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®