replace unsound MaybeUninit deref with ptr::copy

Signed-off-by: isaacthefallenapple <isaacthefallenapple@gmail.com>
This commit is contained in:
isaacthefallenapple 2022-08-29 13:13:23 +02:00
parent 1db7c3664d
commit 4c5e89f46c
No known key found for this signature in database
GPG key ID: F35A6A891A638683

View file

@ -151,15 +151,16 @@ impl<T, E> RocResult<T, E> {
matches!(self.tag, RocResultTag::RocErr)
}
fn into_payload(mut self) -> RocResultPayload<T, E> {
fn into_payload(self) -> RocResultPayload<T, E> {
let mut value = MaybeUninit::uninit();
let ref_mut_value = unsafe { &mut *value.as_mut_ptr() };
// move the value into our MaybeUninit memory
core::mem::swap(&mut self.payload, ref_mut_value);
// copy the value into our MaybeUninit memory
unsafe {
core::ptr::copy_nonoverlapping(&self.payload, value.as_mut_ptr(), 1);
}
// don't run the destructor on self; the `payload` has been moved out
// and replaced by uninitialized memory
// don't run the destructor on self; the `payload` briefly has two owners
// but only `value` is allowed to drop it (after initialization)
core::mem::forget(self);
unsafe { value.assume_init() }