Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `&'static mut MaybeUninit<T>` now implements `InPlaceWrite`. This enables users to use external
allocation mechanisms such as `static_cell`.

### Changed

- `#[pin_data]` now generates a `*Projection` struct similar to the `pin-project` crate.
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,30 @@ 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.
unsafe { init.__pinned_init(slot)? };

// SAFETY: The above call initialized the memory.
unsafe { Ok(Pin::static_mut(self.assume_init_mut())) }
}
}

/// Trait facilitating pinned destruction.
///
/// Use [`pinned_drop`] to implement this trait safely:
Expand Down
Loading