Skip to content

Commit 0bb2e2d

Browse files
committed
use MaybeUninit in core::ptr::{read,read_unaligned}
Code by @japaric, I just split it into individual commits
1 parent 525e8f4 commit 0bb2e2d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/libcore/ptr.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use ops::{CoerceUnsized, DispatchFromDyn};
7979
use fmt;
8080
use hash;
8181
use marker::{PhantomData, Unsize};
82-
use mem;
82+
use mem::{self, MaybeUninit};
8383
use nonzero::NonZero;
8484

8585
use cmp::Ordering::{self, Less, Equal, Greater};
@@ -575,9 +575,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
575575
#[inline]
576576
#[stable(feature = "rust1", since = "1.0.0")]
577577
pub unsafe fn read<T>(src: *const T) -> T {
578-
let mut tmp: T = mem::uninitialized();
579-
copy_nonoverlapping(src, &mut tmp, 1);
580-
tmp
578+
let mut tmp = MaybeUninit::<T>::uninitialized();
579+
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
580+
tmp.into_inner()
581581
}
582582

583583
/// Reads the value from `src` without moving it. This leaves the
@@ -642,11 +642,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
642642
#[inline]
643643
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
644644
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
645-
let mut tmp: T = mem::uninitialized();
645+
let mut tmp = MaybeUninit::<T>::uninitialized();
646646
copy_nonoverlapping(src as *const u8,
647-
&mut tmp as *mut T as *mut u8,
647+
tmp.as_mut_ptr() as *mut u8,
648648
mem::size_of::<T>());
649-
tmp
649+
tmp.into_inner()
650650
}
651651

652652
/// Overwrites a memory location with the given value without reading or

0 commit comments

Comments
 (0)