1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use array::Array;
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};

/// 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 Array.
#[repr(C)] // for cast from self ptr to value
pub union MaybeUninit<A: Array> {
    empty: (),
    value: ManuallyDrop<A>,
}

impl<A: Array> MaybeUninit<A> {
    /// Create a new MaybeUninit with uninitialized interior
    pub unsafe fn uninitialized() -> Self {
        MaybeUninit { empty: () }
    }
}

impl<A: Array> Deref for MaybeUninit<A> {
    type Target = A;

    #[inline(always)]
    fn deref(&self) -> &A {
        unsafe { &self.value }
    }
}

impl<A: Array> DerefMut for MaybeUninit<A> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut A {
        unsafe { &mut self.value }
    }
}