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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/// Temporary fork of some stuff in `core` that's doesn't have a `const fn` API

pub mod mem {
    #[cfg(not(feature = "const-fn"))]
    pub use core::mem;
    pub use core::mem::{replace, zeroed, ManuallyDrop};

    // See RFC 1892
    #[cfg(feature = "const-fn")]
    pub union MaybeUninit<T> {
        uninit: (),
        value: ManuallyDrop<T>,
    }

    // workaround to get this to compile on stable ("unions with non-`Copy` fields are unstable")
    #[cfg(not(feature = "const-fn"))]
    pub struct MaybeUninit<T> {
        value: ManuallyDrop<T>,
    }

    impl<T> MaybeUninit<T> {
        #[cfg(feature = "const-fn")]
        pub const unsafe fn uninitialized() -> Self {
            MaybeUninit { uninit: () }
        }

        #[cfg(not(feature = "const-fn"))]
        pub unsafe fn uninitialized() -> Self {
            mem::uninitialized()
        }

        /// Get a reference to the contained value.
        ///
        /// # Unsafety
        ///
        /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an
        /// initialized state, otherwise this will immediately cause undefined behavior.
        pub unsafe fn get_ref(&self) -> &T {
            &*self.value
        }

        /// Get a mutable reference to the contained value.
        ///
        /// # Unsafety
        ///
        /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an
        /// initialized state, otherwise this will immediately cause undefined behavior.
        pub unsafe fn get_mut(&mut self) -> &mut T {
            &mut *self.value
        }
    }
}