FEAT: Switch to using MaybeUninit for everything
Use std::mem::MaybeUninit and stop using nodrop as a fallback. This means we require Rust 1.36
This commit is contained in:
+14
-16
@@ -1,27 +1,28 @@
|
||||
|
||||
|
||||
use array::Array;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::mem::MaybeUninit as StdMaybeUninit;
|
||||
|
||||
/// A combination of ManuallyDrop and “maybe uninitialized”;
|
||||
/// this wraps a value that can be wholly or partially uninitialized;
|
||||
/// it also has no drop regardless of the type of T.
|
||||
#[repr(C)] // for cast from self ptr to value
|
||||
pub union MaybeUninit<T> {
|
||||
empty: (),
|
||||
value: ManuallyDrop<T>,
|
||||
#[derive(Copy)]
|
||||
pub struct MaybeUninit<T> {
|
||||
inner: StdMaybeUninit<T>,
|
||||
}
|
||||
|
||||
impl<T> Clone for MaybeUninit<T>
|
||||
where T: Copy
|
||||
{
|
||||
fn clone(&self) -> Self { *self }
|
||||
}
|
||||
// Why we don't use std's MaybeUninit on nightly? See the ptr method
|
||||
|
||||
impl<T> MaybeUninit<T> {
|
||||
/// Create a new MaybeUninit with uninitialized interior
|
||||
pub unsafe fn uninitialized() -> Self {
|
||||
MaybeUninit { empty: () }
|
||||
MaybeUninit { inner: StdMaybeUninit::uninit() }
|
||||
}
|
||||
|
||||
/// Create a new MaybeUninit from the value `v`.
|
||||
pub fn from(v: T) -> Self {
|
||||
MaybeUninit { value: ManuallyDrop::new(v) }
|
||||
MaybeUninit { inner: StdMaybeUninit::new(v) }
|
||||
}
|
||||
|
||||
// Raw pointer casts written so that we don't reference or access the
|
||||
@@ -31,16 +32,13 @@ impl<T> MaybeUninit<T> {
|
||||
pub fn ptr(&self) -> *const T::Item
|
||||
where T: Array
|
||||
{
|
||||
// std MaybeUninit creates a &self.value reference here which is
|
||||
// not guaranteed to be sound in our case - we will partially
|
||||
// initialize the value, not always wholly.
|
||||
self as *const _ as *const T::Item
|
||||
self.inner.as_ptr() as *const T::Item
|
||||
}
|
||||
|
||||
/// Return a mut raw pointer to the start of the interior array
|
||||
pub fn ptr_mut(&mut self) -> *mut T::Item
|
||||
where T: Array
|
||||
{
|
||||
self as *mut _ as *mut T::Item
|
||||
self.inner.as_mut_ptr() as *mut T::Item
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user