Reputation: 743
How do I safely get Pin<&mut T>
from Pin<&mut MaybeUninit<T>>
, assuming MaybeUninit<T>
is initialized?
Upvotes: 2
Views: 97
Reputation: 60482
It should be safe via Pin::map_unchecked_mut
:
let pin_uninit_t: Pin<&mut MaybeUninit<T>> = ...;
let pin_t: Pin<&mut T> = unsafe { pin_uninit_t.map_unchecked_mut(|m| m.assume_init_mut()) };
The function is unsafe
because you are given unprotected mutable access to a value you must not move, but we are not moving the value (assume_init_mut
is a simple reference cast) so it is safe assuming the MaybeUninit<T>
is initialized.
Upvotes: 3