diff options
| author | Ralf Jung <post@ralfj.de> | 2018-10-12 08:59:15 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2018-11-23 22:50:20 +0100 |
| commit | 0bb2e2d6d49c7c148ec19d72a2e844081de9f623 (patch) | |
| tree | 85d92198abbd2d7fdda0614e1d0814d5e2a91318 | |
| parent | 525e8f4368dadd1fe66468ff4c14ee889cf504f7 (diff) | |
| download | rust-0bb2e2d6d49c7c148ec19d72a2e844081de9f623.tar.gz rust-0bb2e2d6d49c7c148ec19d72a2e844081de9f623.zip | |
use MaybeUninit in core::ptr::{read,read_unaligned}
Code by @japaric, I just split it into individual commits
| -rw-r--r-- | src/libcore/ptr.rs | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index e9cf11424ca..405f95acf19 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -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 |
