Thread (27 messages) flat view 27 messages, 6 authors, 10d ago
COOLING10d REVIEWED: 16 (16M)

Revision v20 of 8 in this series; 2 review trailers (2 from subsystem maintainers).

Revisions (8)
  1. v14 [diff vs current]
  2. v15 [diff vs current]
  3. v16 [diff vs current]
  4. v17 [diff vs current]
  5. v18 [diff vs current]
  6. v19 [diff vs current]
  7. v20 current
  8. v21 [diff vs current]

[PATCH v20 1/8] rust: alloc: add `KBox::into_non_null`

From: Andreas Hindborg <a.hindborg@kernel.org>
Date: 2026-08-24 11:19:29
Also in: dri-devel, driver-core, linux-fsdevel, linux-mm, linux-pci, linux-pm, linux-pwm, linux-security-module, linux-usb, lkml, rust-for-linux
Subsystem: rust, rust [alloc], the rest · Maintainers: Miguel Ojeda, Danilo Krummrich, Linus Torvalds

Add a method to consume a `Box<T, A>` and return a `NonNull<T>`. This
is a convenience wrapper around `Self::into_raw` for callers that need
a `NonNull` pointer rather than a raw pointer.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
---
 rust/kernel/alloc/kbox.rs | 9 +++++++++
 1 file changed, 9 insertions(+)
diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
index 35d1e015848dd..e6c637bbe2009 100644
--- a/rust/kernel/alloc/kbox.rs
+++ b/rust/kernel/alloc/kbox.rs
@@ -211,6 +211,15 @@ pub fn leak<'a>(b: Self) -> &'a mut T {
         // which points to an initialized instance of `T`.
         unsafe { &mut *Box::into_raw(b) }
     }
+
+    /// Consumes the `Box<T, A>` and returns a `NonNull<T>`.
+    ///
+    /// Like [`Self::into_raw`], but returns a `NonNull`.
+    #[inline]
+    pub fn into_non_null(b: Self) -> NonNull<T> {
+        // SAFETY: `Box::into_raw` returns a valid pointer.
+        unsafe { NonNull::new_unchecked(Self::into_raw(b)) }
+    }
 }
 
 impl<T, A> Box<MaybeUninit<T>, A>
-- 
2.51.2

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help