Rust for Linux List
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Francis Laniel" <laniel_francis@npnm.eu>
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>,
	"Asahi Lina" <lina+kernel@asahilina.net>,
	"Joel Fernandes" <joelagnelf@nvidia.com>,
	"Alvin Sun" <alvin.sun@linux.dev>,
	"Thorsten Blum" <blum@kernel.org>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [RFC PATCH v1 1/2] rust: add NonEmptySlice
Date: Mon, 12 Oct 2026 23:36:41 +0900	[thread overview]
Message-ID: <DM2XOVH1P1FU.2FHNOM9U66YPS@nvidia.com> (raw)
In-Reply-To: <20261011191540.56199-2-laniel_francis@npnm.eu>

Looks like we are converging towards the series sent by Lorenzo, but
wanted to share a couple a quick comments nonetheless:

On Mon Oct 12, 2026 at 4:15 AM JST, Francis Laniel wrote:
> Add NonEmptySlice<T>, a [T] wrapper that is guaranteed to contain at
> least one element.
> This lets methods such as last_mut() return a plain reference instead
> of an Option, so callers that have already checked for emptiness no
> longer need unwrap() or unwrap_unchecked().
>
> Suggested-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Francis Laniel <laniel_francis@npnm.eu>
> ---
>  rust/kernel/lib.rs   |  1 +
>  rust/kernel/slice.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 77 insertions(+)
>  create mode 100644 rust/kernel/slice.rs
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 7225abc64084..8cc01aceb3b2 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -130,6 +130,7 @@
>  #[cfg(CONFIG_RUST_SERIAL_DEV_BUS_ABSTRACTIONS)]
>  pub mod serdev;
>  pub mod sizes;
> +pub mod slice;
>  #[cfg(CONFIG_SOC_BUS)]
>  pub mod soc;
>  #[doc(hidden)]
> diff --git a/rust/kernel/slice.rs b/rust/kernel/slice.rs
> new file mode 100644
> index 000000000000..7e467cb952c3
> --- /dev/null
> +++ b/rust/kernel/slice.rs
> @@ -0,0 +1,76 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Slice abstractions to ease development.
> +
> +use core::ops::{Deref, DerefMut};
> +
> +/// A slice that is guaranteed to contain at least one element.
> +///
> +/// # Invariants
> +///
> +/// `self.0` is not empty.
> +#[repr(transparent)]
> +pub struct NonEmptySlice<T>([T]);
> +
> +impl<T> NonEmptySlice<T> {
> +    /// Returns `None` if `s` is empty.

This is not a standard doccomment. Doccomments first start by stating
what the function does before explaining the return value.

> +    pub fn new(s: &mut [T]) -> Option<&Self> {

It looks strange to take a `&mut` and return a `&Self`. There should be
no reason to take a mutable reference here, and you can use
`ptr::from_ref` to create the reference.

> +        if s.is_empty() {
> +            return None;
> +        }
> +        // SAFETY: `Self` is `repr(transparent)` over `[T]`, so the cast keeps the slice length.
> +        // INVARIANT: `s` is not empty, checked above.
> +        Some(unsafe { &*(core::ptr::from_mut(s) as *const Self) })
> +    }
> +
> +    /// Returns `None` if `s` is empty.
> +    pub fn new_mut(s: &mut [T]) -> Option<&mut Self> {
> +        if s.is_empty() {
> +            return None;
> +        }
> +        // SAFETY: `Self` is `repr(transparent)` over `[T]`, so the cast keeps the slice length.
> +        // INVARIANT: `s` is not empty, checked above.
> +        Some(unsafe { &mut *(core::ptr::from_mut(s) as *mut Self) })
> +    }
> +
> +    /// Returns a reference to the first element.
> +    pub fn first(&mut self) -> &T {

Same here, `first` and `last` should take a non-mutable reference as
their standard library counterparts do. They do not allow to mutate the
slice.

  reply	other threads:[~2026-10-12 14:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-11 19:15 [RFC PATCH v1 0/2] Add NonEmptySlice Francis Laniel
2026-10-11 19:15 ` [RFC PATCH v1 1/2] rust: add NonEmptySlice Francis Laniel
2026-10-12 14:36   ` Alexandre Courbot [this message]
2026-10-12 15:11     ` Francis Laniel
2026-10-11 19:15 ` [RFC PATCH v1 2/2] rust: uaccess: Use slice::NonEmptySlice in strcpy_into_buf() Francis Laniel
2026-10-12  0:21 ` [RFC PATCH v1 0/2] Add NonEmptySlice Alexandre Courbot
2026-10-12  7:11   ` Francis Laniel

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=DM2XOVH1P1FU.2FHNOM9U66YPS@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=alvin.sun@linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=blum@kernel.org \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=joelagnelf@nvidia.com \
    --cc=laniel_francis@npnm.eu \
    --cc=lina+kernel@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --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