rust: pin-init: Implement `InPlaceWrite<T>` for `&'static mut MaybeUninit<T>`
Subsystems (2)
Benno Lossin
maintainerPATCH 1/1 rust: pin-init: Implement `InPlaceWrite<T>` for `&'static mut MaybeUninit<T>`
From: Oleksandr Babak <[email protected]>
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].
---
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(+)
rust/pin-init/src/lib.rs +27 -0
@@ -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 &'staticmut MaybeUninit <T > { +type Initialized = &'staticmut 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:
Signature · 1 line
2.51.2
Quoted 11 lines · Benno Lossin
From: Oleksandr Babak <[email protected]>
This feature allows users to use
&'static mut MaybeUninit<T>as a place to initialize the value. It mirrors an existing implemetation forBox<MaybeUninit>, but enables users to use external allocation mechanisms such asstatic_cell[1].Signed-off-by Oleksandr Babak <[email protected]>Link https://crates.io/crates/static_cell [1][ Added link to `static_cell` - Benno ]Signed-off-by Benno Lossin <[email protected]>
Quoted 6 lines
---
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