Skip to contentSkip to conversation

rust: alloc: allow different error types in `KBox::pin_slice`

PATCH Applied Andreas Hindborg 2 participants 2 messages Jun 5 – Sep 29

Subsystems (2)
lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH v2 1/1 rust: alloc: allow different error types in `KBox::pin_slice`

Previously, KBox::pin_slice required the initializer error type to match the return error type via E: From<AllocError>. This prevented using infallible initializers like new_mutex! inside pin_slice, because Infallible does not implement From<AllocError>.

Introduce a separate type parameter E2 for the initializer error type and require E: From<AllocError> and E: From<E2> instead. This allows the initializer to return a different error type that can be converted into the final error type, enabling use of infallible pin initializers in fallible allocation contexts.

---
Changes in v2:
- Use E: From<E2> (and keep E: From<AllocError>) instead of Into<E> bounds, which allows the body to use ? instead of explicit match blocks (Danilo).
- Add Benno's Co-developed-by and Signed-off-by tags (Benno).
- Link to v1: https://msgid.link/[email protected]

To: Danilo Krummrich <[email protected]>
To: Lorenzo Stoakes <[email protected]>
To: Vlastimil Babka <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Uladzislau Rezki <[email protected]>
To: Miguel Ojeda <[email protected]>
To: Boqun Feng <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Andreas Hindborg <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 rust/kernel/alloc/kbox.rs | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
rust/kernel/alloc/kbox.rs +4 -3
index bd6da02c7ab8..c2e89e523eae 100644
@@ -298,7 +298,7 @@ pub fn pin(x: T, flags: Flags) -> Result<Pin<Box<T, A>>, AllocError>
     /// }
     ///
     /// // Allocate a boxed slice of 10 `Example`s.
-    /// let s = KBox::pin_slice(
+    /// let s = KBox::pin_slice::<_, _, Error, _>(
     ///     | _i | Example::new(),
     ///     10,
     ///     GFP_KERNEL
@@ -308,15 +308,16 @@ pub fn pin(x: T, flags: Flags) -> Result<Pin<Box<T, A>>, AllocError>
     /// assert_eq!(s[3].d.lock().a, 20);
     /// # Ok::<(), Error>(())
     /// ```
-    pub fn pin_slice<Func, Item, E>(
+    pub fn pin_slice<Func, Item, E, E2>(
         mut init: Func,
         len: usize,
         flags: Flags,
     ) -> Result<Pin<Box<[T], A>>, E>
     where
         Func: FnMut(usize) -> Item,
-        Item: PinInit<T, E>,
+        Item: PinInit<T, E2>,
         E: From<AllocError>,
+        E: From<E2>,
     {
         let mut buffer = super::Vec::<T, A>::with_capacity(len, flags)?;
         for i in 0..len {

---
base-commit: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32
change-id: 20260214-pin-slice-init-e8ef96fc07b9

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
maintainer Applied
#1 lore raw
Quoted 1 line · Andreas Hindborg

[PATCH v2] rust: alloc: allow different error types in KBox::pin_slice

Applied, thanks!

Branch: alloc-next
Tree: [email protected]:Rust-for-Linux/linux.git

[1/1] rust: alloc: allow different error types in KBox::pin_slice

      commit: d190aa209416

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel release.