use MaybeUninit in core::ptr::{read,read_unaligned}

Code by @japaric, I just split it into individual commits
This commit is contained in:
Ralf Jung 2018-10-12 08:59:15 +02:00
parent 525e8f4368
commit 0bb2e2d6d4

View file

@ -79,7 +79,7 @@ use ops::{CoerceUnsized, DispatchFromDyn};
use fmt;
use hash;
use marker::{PhantomData, Unsize};
use mem;
use mem::{self, MaybeUninit};
use nonzero::NonZero;
use cmp::Ordering::{self, Less, Equal, Greater};
@ -575,9 +575,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn read<T>(src: *const T) -> T {
let mut tmp: T = mem::uninitialized();
copy_nonoverlapping(src, &mut tmp, 1);
tmp
let mut tmp = MaybeUninit::<T>::uninitialized();
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
tmp.into_inner()
}
/// Reads the value from `src` without moving it. This leaves the
@ -642,11 +642,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
#[inline]
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
let mut tmp: T = mem::uninitialized();
let mut tmp = MaybeUninit::<T>::uninitialized();
copy_nonoverlapping(src as *const u8,
&mut tmp as *mut T as *mut u8,
tmp.as_mut_ptr() as *mut u8,
mem::size_of::<T>());
tmp
tmp.into_inner()
}
/// Overwrites a memory location with the given value without reading or