pub struct PinUninit<'a, T> { /* private fields */ }Expand description
A pinned, uninitialized pointer.
This can be considered as Pin<&mut MaybeUninit<T>>:
- The pointee has a stable location in memory. It cannot be moved elsewhere.
- The pointee is not yet initialized, therefore the drop guarantee is not existent.
However, PinUninit provides the additional guarantee that once a method
that successfully initialze the data is called (e.g. init_ok), the
pointee will be considered as Pin<&mut T>, therefore the drop guarantee
kicks in, and T’s destructor is guaranteed to be called before the storage
is deallocated.
Implementations§
Source§impl<'a, T> PinUninit<'a, T>
impl<'a, T> PinUninit<'a, T>
Sourcepub unsafe fn new(ptr: &'a mut MaybeUninit<T>) -> Self
pub unsafe fn new(ptr: &'a mut MaybeUninit<T>) -> Self
Creates a new PinUninit with a given MaybeUninit<T>.
§Safety
The caller must ensure ptr has a stable location in memory.
The caller must obtain a InitResult that is tied to the lifetime of the returned
PinUninit, and need to respect its value:
- If
InitOkis obtained, the caller must treat theptrasPin<&mut T>. This means that the drop guarantee kick in; the memory cannot be deallocated untilTis dropped. - If
InitErris obtained,ptris uninitialized and the caller must not try to dropT. - If panic happens while trying to get the result, then we are not certain about initialization state. This means that the caller must respect the drop guarantee, but also not drop the value. The only solution is to leak memory. If that’s not possible, then the caller must abort the process.
The lifetime associated with this function should be “closed”. It should be a local,
temporary lifetime, shorter than any of the lifetime the caller have access to (including
'static, and should not escape the calling function.
This is to guarantee that the only way to get a InitResult<'a, T, E>
is to use of the methods of this particular PinUninit returned.
In order to satisfy the requirement, the caller typically takes a constructor with type
Init<T, E>, which can be seen as for<'a> FnOnce(PinUninit<'a, T>) -> InitResult<'a, T, E>.
Sourcepub fn get_mut(&mut self) -> &mut MaybeUninit<T>
pub fn get_mut(&mut self) -> &mut MaybeUninit<T>
Gets a mutable reference to MaybeUninit inside of this PinUninit.
This is safe because the MaybeUninit we point to is not yet initialized,
and MaybeUninit does not have Drop implementation.
Sourcepub unsafe fn init_ok(self) -> InitOk<'a, T>
pub unsafe fn init_ok(self) -> InitOk<'a, T>
Asserts that the initialize is indeed completed. Doing so initiates the
drop guarantee of T.
§Safety
This function is unsafe as this is equivalent to MaybeUninit::assume_init.
Sourcepub fn init_err<E>(self, err: E) -> InitErr<'a, E>
pub fn init_err<E>(self, err: E) -> InitErr<'a, E>
Generates a InitResult signaling that the initialization is failed.
Note that the caller should make sure nothing is partially pinned-initialized.
This isn’t the contract of this function, but is the contract for
creating PinUninit for pinned-initializing sub-fields.
Sourcepub fn init<E, F>(self, value: F) -> InitResult<'a, T, E>where
F: Init<T, E>,
pub fn init<E, F>(self, value: F) -> InitResult<'a, T, E>where
F: Init<T, E>,
Completes the initialization with a callback.
Useful e.g. if the callback is produced by init_pin!.
Sourcepub fn init_with_value(self, value: T) -> InitOk<'a, T>
pub fn init_with_value(self, value: T) -> InitOk<'a, T>
Completes the initialization by moving the given value.
Useful if the the type T can be initialized unpinned.