Skip to content

Commit ce11e2b

Browse files
committed
feat: imple InPlaceWrite<T> for &'static mut MaybeUninit<T>
Signed-off-by: Oleksandr Babak <[email protected]>
1 parent 871cf88 commit ce11e2b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,25 @@ pub trait InPlaceWrite<T> {
14491449
fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E>;
14501450
}
14511451

1452+
impl<'a, T> InPlaceWrite<T> for &'static mut MaybeUninit<T> {
1453+
type Initialized = &'static mut T;
1454+
fn write_init<E>(self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
1455+
let slot = self.as_mut_ptr();
1456+
// SAFETY: `self` is a valid pointer to uninitialized memory.
1457+
unsafe { init.__init(slot)? };
1458+
// SAFETY: The above call initialized the memory.
1459+
Ok(unsafe { &mut *slot })
1460+
}
1461+
1462+
fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
1463+
let slot = self.as_mut_ptr();
1464+
// SAFETY: `self` is a valid pointer to uninitialized memory.
1465+
unsafe { init.__pinned_init(slot)? };
1466+
// SAFETY: The above call initialized the memory.
1467+
Ok(unsafe { Pin::new_unchecked(&mut *slot) })
1468+
}
1469+
}
1470+
14521471
/// Trait facilitating pinned destruction.
14531472
///
14541473
/// Use [`pinned_drop`] to implement this trait safely:

0 commit comments

Comments
 (0)