Skip to contentSkip to conversation

rust: add NonEmpty

PATCH In review Lorenzo Delgado 4 participants 16 messages Oct 14 – Oct 16 Rust

lore
Share & tools
lore
#0 lore raw

v3 cover rust: add NonEmpty

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.

- I put it in its own kernel::non_empty module. Would a broader home like kernel::collections be better?

Francis Laniel posted a slice-only RFC [3]. Following Gary's suggestion on the issue, this version is generic over the container.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://github.com/Rust-for-Linux/linux/issues/1262
[3] https://lore.kernel.org/rust-for-linux/[email protected]/

---
Changes in v3:
- Replace from_array_mut() with a const fn from_mut() on
NonEmpty<[T; N]>, and get NonEmpty<[T]> from it by unsize
coercion instead of a slice pointer cast (Gary).
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Take &mut NonEmpty<[u8]> in strcpy_into_buf() and drop the
EINVAL case for an empty buffer (Miguel).
- Add NonEmpty::from_array_mut(), which checks the array length at compile time, so callers of strcpy_into_buf() with an array buffer
need no runtime check.
- Link to v1: https://patch.msgid.link/[email protected]

---
Lorenzo Delgado (2):
rust: non_empty: add NonEmpty
rust: uaccess: use NonEmpty in strcpy_into_buf()

 rust/kernel/lib.rs       |   1 +
 rust/kernel/non_empty.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/uaccess.rs   |  15 ++----
 3 files changed, 129 insertions(+), 10 deletions(-)

---
base-commit: 2dee3d3adcadcc57d62ba6608cd02f50b0c10264
change-id: 20261011-nonempty-slice-022084ba9a5a

Best regards,

Signature · 1 line
Lorenzo Delgado <[email protected]>
#1 lore raw

PATCH v3 1/2 rust: non_empty: add NonEmpty

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 whose constructors check that the container is not 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 and arrays are supported. A slice of unknown length goes through try_from_mut(), which returns None if it is empty. An array goes through from_mut(), which checks the length at compile time instead of returning an Option. The resulting &mut NonEmpty<[T; N]> coerces to &mut NonEmpty<[T]>, so a caller with a fixed-size buffer needs no runtime check and no extra cast. The only accessor is last_mut(), which uaccess needs. Other containers and accessors can follow with their users.

---
 rust/kernel/lib.rs       |   1 +
 rust/kernel/non_empty.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 124 insertions(+)
rust/kernel/lib.rs +1 -0
index 7225abc64084..8e1928deac35 100644
@@ -105,6 +105,7 @@
 pub mod module_param;
 #[cfg(CONFIG_NET)]
 pub mod net;
+pub mod non_empty;
 pub mod num;
 pub mod of;
 #[cfg(CONFIG_PM_OPP)]
rust/kernel/non_empty.rs new file +123 -0

123 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v3 1/2] rust: non_empty: add NonEmpty

rust/kernel/non_empty.rs line 20

Quoted 11 lines · Lorenzo Delgado
rust/kernel/non_empty.rs new file +10 -0
+/// A container that can report whether it holds any element.
+///
+/// The result must not change while the container is only reachable through a shared reference.
+///
+/// Unsafe code may only rely on the result for a container type whose implementation it trusts,
+/// such as the ones in this module, since a safe implementation can return anything.
+pub trait IsEmpty {
+    /// Returns `true` if the container holds no element.
+    fn is_empty(&self) -> bool;
+}

Do we need this to be a safe trait? If it is unsafe, then we can simplify the invariant and more easily think about the type, like other Non*s, no?

Thanks!

Cheers,
Miguel

reviewer
#3 lore raw

Re: [PATCH v3 1/2] rust: non_empty: add NonEmpty

Quoted 16 lines · Miguel Ojeda

rust/kernel/non_empty.rs line 20

Quoted 11 lines · Lorenzo Delgado
rust/kernel/non_empty.rs new file +10 -0
+/// A container that can report whether it holds any element.
+///
+/// The result must not change while the container is only reachable through a shared reference.
+///
+/// Unsafe code may only rely on the result for a container type whose implementation it trusts,
+/// such as the ones in this module, since a safe implementation can return anything.
+pub trait IsEmpty {
+    /// Returns `true` if the container holds no element.
+    fn is_empty(&self) -> bool;
+}

Do we need this to be a safe trait? If it is unsafe, then we can simplify the invariant and more easily think about the type, like other Non*s, no?

I think in this case the meaning of "empty" depends on the container itself. Unsafe code that relies on "is_empty" would be the NonEmpty<Container> itself, so it just needs to trust itself (as boundary of unsafe is module level).

I think of almost all of the text above can be removed. The invariant of NonEmpty can just be "IsEmpty::is_empty has returned true" and how it is interpreted is up to the container type.

Best,
Gary

#4 lore raw

Re: [PATCH v3 1/2] rust: non_empty: add NonEmpty

Quoted 18 lines · Gary Guo
Quoted 16 lines · Miguel Ojeda

rust/kernel/non_empty.rs line 20

Quoted 11 lines · Lorenzo Delgado
rust/kernel/non_empty.rs new file +10 -0
+/// A container that can report whether it holds any element.
+///
+/// The result must not change while the container is only reachable through a shared reference.
+///
+/// Unsafe code may only rely on the result for a container type whose implementation it trusts,
+/// such as the ones in this module, since a safe implementation can return anything.
+pub trait IsEmpty {
+    /// Returns `true` if the container holds no element.
+    fn is_empty(&self) -> bool;
+}

Do we need this to be a safe trait? If it is unsafe, then we can simplify the invariant and more easily think about the type, like other Non*s, no?

Quoted 7 lines

I think in this case the meaning of "empty" depends on the container itself. Unsafe code that relies on "is_empty" would be the NonEmpty<Container> itself, so it just needs to trust itself (as boundary of unsafe is module level).

I think of almost all of the text above can be removed. The invariant of NonEmpty can just be "IsEmpty::is_empty has returned true" and how it is interpreted is up to the container type.

Is IsEmpty actually useful? It needs to be implemented for every container we want to wrap with essentially the same boilerplate code, which looks a bit overkill to me. Couldn't we just do:

impl<T, C: Deref<Target = [T]>> NonEmpty<C>

and have a single constructor method that supports all container types by reusing the slice's is_empty method?

That way we could get rid of IsEmpty entirely. That would limit NonEmpty to types that directly deref to [T], but I guess we are going to use it on slices most of the time, with the exception of when we want to return an owned container that is not empty, and those we have already deref to a slice.

reviewer
#5 lore raw

Re: [PATCH v3 1/2] rust: non_empty: add NonEmpty

Quoted 27 lines · Alexandre Courbot
Quoted 18 lines · Gary Guo
Quoted 16 lines · Miguel Ojeda

rust/kernel/non_empty.rs line 20

Quoted 11 lines · Lorenzo Delgado
rust/kernel/non_empty.rs new file +10 -0
+/// A container that can report whether it holds any element.
+///
+/// The result must not change while the container is only reachable through a shared reference.
+///
+/// Unsafe code may only rely on the result for a container type whose implementation it trusts,
+/// such as the ones in this module, since a safe implementation can return anything.
+pub trait IsEmpty {
+    /// Returns `true` if the container holds no element.
+    fn is_empty(&self) -> bool;
+}

Do we need this to be a safe trait? If it is unsafe, then we can simplify the invariant and more easily think about the type, like other Non*s, no?

Quoted 7 lines

I think in this case the meaning of "empty" depends on the container itself. Unsafe code that relies on "is_empty" would be the NonEmpty<Container> itself, so it just needs to trust itself (as boundary of unsafe is module level).

I think of almost all of the text above can be removed. The invariant of NonEmpty can just be "IsEmpty::is_empty has returned true" and how it is interpreted is up to the container type.

Quoted 5 lines

Is IsEmpty actually useful? It needs to be implemented for every container we want to wrap with essentially the same boilerplate code, which looks a bit overkill to me. Couldn't we just do:

impl<T, C: Deref<Target = [T]>> NonEmpty<C>

Slice does not dereference to a slice, and arrays doesn't dereference to slices either.

Best,
Gary

#6 lore raw

Re: [PATCH v3 1/2] rust: non_empty: add NonEmpty

Quoted 28 lines · Gary Guo
Quoted 27 lines · Alexandre Courbot
Quoted 25 lines · Gary Guo
Quoted 16 lines · Miguel Ojeda

rust/kernel/non_empty.rs line 20

Quoted 11 lines · Lorenzo Delgado
rust/kernel/non_empty.rs new file +10 -0
+/// A container that can report whether it holds any element.
+///
+/// The result must not change while the container is only reachable through a shared reference.
+///
+/// Unsafe code may only rely on the result for a container type whose implementation it trusts,
+/// such as the ones in this module, since a safe implementation can return anything.
+pub trait IsEmpty {
+    /// Returns `true` if the container holds no element.
+    fn is_empty(&self) -> bool;
+}

Do we need this to be a safe trait? If it is unsafe, then we can simplify the invariant and more easily think about the type, like other Non*s, no?

I think in this case the meaning of "empty" depends on the container itself. Unsafe code that relies on "is_empty" would be the NonEmpty<Container> itself, so it just needs to trust itself (as boundary of unsafe is module level).

I think of almost all of the text above can be removed. The invariant of NonEmpty can just be "IsEmpty::is_empty has returned true" and how it is interpreted is up to the container type.

Quoted 8 lines
Quoted 5 lines

Is IsEmpty actually useful? It needs to be implemented for every container we want to wrap with essentially the same boilerplate code, which looks a bit overkill to me. Couldn't we just do:

impl<T, C: Deref<Target = [T]>> NonEmpty<C>

Slice does not dereference to a slice, and arrays doesn't dereference to slices either.

How about C: AsRef<[T]> (would require an implementation for KVec) or C: Borrow<[T]>?

#7 lore raw

Re: [PATCH v3 1/2] rust: non_empty: add NonEmpty

In reply to Gary Guo
Quoted 8 lines · Gary Guo

I think in this case the meaning of "empty" depends on the container itself. Unsafe code that relies on "is_empty" would be the NonEmpty<Container> itself, so it just needs to trust itself (as boundary of unsafe is module level).

I think of almost all of the text above can be removed. The invariant of NonEmpty can just be "IsEmpty::is_empty has returned true" and how it is interpreted is up to the container type.

That sounds good to me, i.e. my main concern was the invariant text.

(Though we still mark things as unsafe even if private and within a module, so if we are actually trusting it, shouldn't we mark it?)

Cheers,
Miguel

#8 lore raw

PATCH v3 2/2 rust: uaccess: use NonEmpty in strcpy_into_buf()

In reply to Lorenzo Delgado

strcpy_into_buf() returns EINVAL for an empty buffer and later writes the NUL terminator through last_mut().unwrap_unchecked(), relying on that earlier check.

Take the buffer as a NonEmpty<[u8]> instead, so the requirement is in the signature. The EINVAL case goes away, and last_mut() returns the element directly, which removes the unsafe block. There are no callers to convert. A caller with an array buffer can pass it through NonEmpty::from_mut() without a runtime check.

The MaybeUninit cast still starts from [u8], so its SAFETY comment does not depend on the layout of the wrapper.

---
 rust/kernel/uaccess.rs | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
rust/kernel/uaccess.rs +5 -10
index 5f6c4d7a1a51..1cc5c7580d3a 100644
@@ -11,6 +11,7 @@
     error::Result,
     ffi::{c_char, c_void},
     fs::file,
+    non_empty::NonEmpty,
     prelude::*,
     ptr::KnownSize,
     transmute::{AsBytes, FromBytes},
@@ -383,20 +384,16 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
     /// Read a NUL-terminated string from userspace and return it.
     ///
     /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
-    /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
+    /// Since there must be space to add a NUL-terminator, `buf` is a [`NonEmpty`] slice. The
     /// returned `&CStr` points into `buf`.
     ///
     /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
     /// copied).
     #[doc(alias = "strncpy_from_user")]
-    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
-        if buf.is_empty() {
-            return Err(EINVAL);
-        }
-
+    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut NonEmpty<[u8]>) -> Result<&'buf CStr> {
         // SAFETY: The types are compatible and `strncpy_from_user` doesn't write uninitialized
         // bytes to `buf`.
-        let mut dst = unsafe { &mut *(core::ptr::from_mut(buf) as *mut [MaybeUninit<u8>]) };
+        let mut dst = unsafe { &mut *(core::ptr::from_mut::<[u8]>(buf) as *mut [MaybeUninit<u8>]) };
 
         // We never read more than `self.length` bytes.
         if dst.len() > self.length {
@@ -422,9 +419,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut() = 0;
         }
 
         // This method consumes `self`, so it can only be called once, thus we do not need to
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH v3 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf()

On Wed Oct 14, 2026 at 5:30 PM JST, Lorenzo Delgado wrote:
<...>

Quoted 19 lines
rust/kernel/uaccess.rs +2 -1
index 5f6c4d7a1a51..1cc5c7580d3a 100644
@@ -11,6 +11,7 @@
     error::Result,
     ffi::{c_char, c_void},
     fs::file,
+    non_empty::NonEmpty,
     prelude::*,
     ptr::KnownSize,
     transmute::{AsBytes, FromBytes},
@@ -383,20 +384,16 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
     /// Read a NUL-terminated string from userspace and return it.
     ///
     /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
-    /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
+    /// Since there must be space to add a NUL-terminator, `buf` is a [`NonEmpty`] slice. The
     /// returned `&CStr` points into `buf`.

rust/kernel/uaccess.rs pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R · line 393

Quoted 10 lines
rust/kernel/uaccess.rs +1 -5
     ///
     /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
     /// copied).
     #[doc(alias = "strncpy_from_user")]
-    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
-        if buf.is_empty() {
-            return Err(EINVAL);
-        }
-
+    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut NonEmpty<[u8]>) -> Result<&'buf CStr> {

I understand this was done per the v1 feedback, but won't it make it more tedious for users to call this method? (hard to tell as there are no users at the moment :))

We could still benefit from NonEmpty if we replace the buf.is_empty by an attempt to build it and return EINVAL if that fails (what v1 did basically) and calling last_mut on it to remove the unwrap_unchecked.

Of course if callers are expected to already work with a NonEmpty then that's fine, but otherwise my concern is that every one of them would have to manually build one to perform a check that this method can easily do.

reviewer
#10 lore raw

Re: [PATCH v3 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf()

Quoted 41 lines · Alexandre Courbot

On Wed Oct 14, 2026 at 5:30 PM JST, Lorenzo Delgado wrote:
<...>

Quoted 19 lines
rust/kernel/uaccess.rs +2 -1
index 5f6c4d7a1a51..1cc5c7580d3a 100644
@@ -11,6 +11,7 @@
     error::Result,
     ffi::{c_char, c_void},
     fs::file,
+    non_empty::NonEmpty,
     prelude::*,
     ptr::KnownSize,
     transmute::{AsBytes, FromBytes},
@@ -383,20 +384,16 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
     /// Read a NUL-terminated string from userspace and return it.
     ///
     /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
-    /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
+    /// Since there must be space to add a NUL-terminator, `buf` is a [`NonEmpty`] slice. The
     /// returned `&CStr` points into `buf`.

rust/kernel/uaccess.rs pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R · line 393

Quoted 10 lines
rust/kernel/uaccess.rs +1 -5
     ///
     /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
     /// copied).
     #[doc(alias = "strncpy_from_user")]
-    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
-        if buf.is_empty() {
-            return Err(EINVAL);
-        }
-
+    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut NonEmpty<[u8]>) -> Result<&'buf CStr> {

I understand this was done per the v1 feedback, but won't it make it more tedious for users to call this method? (hard to tell as there are no users at the moment :))

We could still benefit from NonEmpty if we replace the buf.is_empty by an attempt to build it and return EINVAL if that fails (what v1 did basically) and calling last_mut on it to remove the unwrap_unchecked.

Quoted 4 lines

Of course if callers are expected to already work with a NonEmpty then that's fine, but otherwise my concern is that every one of them would have to manually build one to perform a check that this method can easily do.

I think it might also be reasonable to invoke errorenous behaviour here, by do a WARN and just return c""?

Best,
Gary

#11 lore raw

Re: [PATCH v3 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf()

Quoted 42 lines · Gary Guo
Quoted 41 lines · Alexandre Courbot

On Wed Oct 14, 2026 at 5:30 PM JST, Lorenzo Delgado wrote:
<...>

Quoted 19 lines
rust/kernel/uaccess.rs +2 -1
index 5f6c4d7a1a51..1cc5c7580d3a 100644
@@ -11,6 +11,7 @@
     error::Result,
     ffi::{c_char, c_void},
     fs::file,
+    non_empty::NonEmpty,
     prelude::*,
     ptr::KnownSize,
     transmute::{AsBytes, FromBytes},
@@ -383,20 +384,16 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
     /// Read a NUL-terminated string from userspace and return it.
     ///
     /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
-    /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
+    /// Since there must be space to add a NUL-terminator, `buf` is a [`NonEmpty`] slice. The
     /// returned `&CStr` points into `buf`.

rust/kernel/uaccess.rs pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R · line 393

Quoted 10 lines
rust/kernel/uaccess.rs +1 -5
     ///
     /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
     /// copied).
     #[doc(alias = "strncpy_from_user")]
-    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
-        if buf.is_empty() {
-            return Err(EINVAL);
-        }
-
+    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut NonEmpty<[u8]>) -> Result<&'buf CStr> {

I understand this was done per the v1 feedback, but won't it make it more tedious for users to call this method? (hard to tell as there are no users at the moment :))

We could still benefit from NonEmpty if we replace the buf.is_empty by an attempt to build it and return EINVAL if that fails (what v1 did basically) and calling last_mut on it to remove the unwrap_unchecked.

Quoted 7 lines
Quoted 4 lines

Of course if callers are expected to already work with a NonEmpty then that's fine, but otherwise my concern is that every one of them would have to manually build one to perform a check that this method can easily do.

I think it might also be reasonable to invoke errorenous behaviour here, by do a WARN and just return c""?

That would make the doccomment saying that "The returned &CStr points into buf" incorrect though.

If callers are expected to mostly pass slices into fixed-size arrays, NonEmpty::from_mut() makes building one trivial (and we could even envision a From conversion to makes it implicit), so I don't feel strongly about it as long as we can confirm that this is how this method will be used.

#12 lore raw
In reply to Lorenzo Delgado
Quoted 18 lines · Lorenzo Delgado

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.

Quoted 4 lines

- 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.

Quoted 3 lines

- 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.

Also as a general comment: the commit messages and comments read as LLM-generated and are insufferably mechanical and tedious. Please turn the LLM dial down a bit and spend some time writing or at least editing them yourself to save some time for your reviewers. The doccomment for from_mut is a good example of something that could be reduced to 2 lines max.

reviewer
#13 lore raw
Quoted 29 lines · Alexandre Courbot
Quoted 18 lines · Lorenzo Delgado

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.

Quoted 4 lines

- 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.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

- 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

#14 lore raw
Quoted 5 lines · Gary Guo

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).

Yeah, we indeed had that problem, which is why nowadays as you say we prefer things to be more split than less, because going into the "grouping" direction is usually easier (i.e. it is easier to comer up with better hierarchies when we have more types around, but it is annoying to have a big file growing with no boundaries meanwhile).

Cheers,
Miguel

#15 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · Gary Guo

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).

Quoted 5 lines

Yeah, we indeed had that problem, which is why nowadays as you say we prefer things to be more split than less, because going into the "grouping" direction is usually easier (i.e. it is easier to comer up with better hierarchies when we have more types around, but it is annoying to have a big file growing with no boundaries meanwhile).

In this case maybe we can resurrect the kernel::slice module for this?