mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/1] rust: pin-init: Implement `InPlaceWrite<T>` for `&'static mut MaybeUninit<T>`
@ 2026-01-08 12:43 Benno Lossin
  2026-01-17  9:54 ` Benno Lossin
  0 siblings, 1 reply; 2+ messages in thread
From: Benno Lossin @ 2026-01-08 12:43 UTC (permalink / raw)
  To: Benno Lossin, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Fiona Behrens, Christian Schrefl
  Cc: Oleksandr Babak, rust-for-linux, linux-kernel

From: Oleksandr Babak <alexanderbabak@proton.me>

This feature allows users to use `&'static mut MaybeUninit<T>` as a
place to initialize the value. It mirrors an existing implemetation
for `Box<MaybeUninit>`, but enables users to use external allocation
mechanisms such as `static_cell` [1].

Signed-off-by: Oleksandr Babak <alexanderbabak@proton.me>
Link: https://crates.io/crates/static_cell [1]
[ Added link to `static_cell` - Benno ]
Signed-off-by: Benno Lossin <lossin@kernel.org>
---
This is the entire pin-init update this cycle. (excluding the syn
patches, which I will send soon)
---
 rust/pin-init/src/lib.rs | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
index 8dc9dd5ac6fd..ea59c86a9108 100644
--- a/rust/pin-init/src/lib.rs
+++ b/rust/pin-init/src/lib.rs
@@ -1536,6 +1536,33 @@ pub trait InPlaceWrite<T> {
     fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E>;
 }
 
+impl<T> InPlaceWrite<T> for &'static mut MaybeUninit<T> {
+    type Initialized = &'static mut T;
+
+    fn write_init<E>(self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
+        let slot = self.as_mut_ptr();
+
+        // SAFETY: `slot` is a valid pointer to uninitialized memory.
+        unsafe { init.__init(slot)? };
+
+        // SAFETY: The above call initialized the memory.
+        unsafe { Ok(self.assume_init_mut()) }
+    }
+
+    fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
+        let slot = self.as_mut_ptr();
+
+        // SAFETY: `slot` is a valid pointer to uninitialized memory.
+        //
+        // The `'static` borrow guarantees the data will not be
+        // moved/invalidated until it gets dropped (which is never).
+        unsafe { init.__pinned_init(slot)? };
+
+        // SAFETY: The above call initialized the memory.
+        Ok(Pin::static_mut(unsafe { self.assume_init_mut() }))
+    }
+}
+
 /// Trait facilitating pinned destruction.
 ///
 /// Use [`pinned_drop`] to implement this trait safely:
-- 
2.51.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] rust: pin-init: Implement `InPlaceWrite<T>` for `&'static mut MaybeUninit<T>`
  2026-01-08 12:43 [PATCH 1/1] rust: pin-init: Implement `InPlaceWrite<T>` for `&'static mut MaybeUninit<T>` Benno Lossin
@ 2026-01-17  9:54 ` Benno Lossin
  0 siblings, 0 replies; 2+ messages in thread
From: Benno Lossin @ 2026-01-17  9:54 UTC (permalink / raw)
  To: Benno Lossin, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Fiona Behrens, Christian Schrefl
  Cc: Oleksandr Babak, rust-for-linux, linux-kernel

On Thu Jan 8, 2026 at 1:43 PM CET, Benno Lossin wrote:
> From: Oleksandr Babak <alexanderbabak@proton.me>
>
> This feature allows users to use `&'static mut MaybeUninit<T>` as a
> place to initialize the value. It mirrors an existing implemetation
> for `Box<MaybeUninit>`, but enables users to use external allocation
> mechanisms such as `static_cell` [1].
>
> Signed-off-by: Oleksandr Babak <alexanderbabak@proton.me>
> Link: https://crates.io/crates/static_cell [1]
> [ Added link to `static_cell` - Benno ]
> Signed-off-by: Benno Lossin <lossin@kernel.org>
> ---
> This is the entire pin-init update this cycle. (excluding the syn
> patches, which I will send soon)
> ---
>  rust/pin-init/src/lib.rs | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)

Applied to pin-init-next, thanks everyone!

Cheers,
Benno

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-01-17  9:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-08 12:43 [PATCH 1/1] rust: pin-init: Implement `InPlaceWrite<T>` for `&'static mut MaybeUninit<T>` Benno Lossin
2026-01-17  9:54 ` Benno Lossin

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®