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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Possible [`core::mem`] additions and replacements.

use core::mem;

use crate::marker::Forget;

pub fn forget<T: Forget>(x: T) {
    mem::forget(x)
}

pub unsafe fn forget_unchecked<T>(x: T) {
    mem::forget(x)
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ManuallyDrop<T: ?Sized> {
    inner: mem::ManuallyDrop<T>,
}

unsafe impl<T: ?Sized> Forget for ManuallyDrop<T> {}

impl<T> ManuallyDrop<T> {
    pub const fn new(value: T) -> Self
    where
        T: Forget,
    {
        Self {
            inner: mem::ManuallyDrop::new(value),
        }
    }

    pub const unsafe fn new_unchecked(value: T) -> Self {
        Self {
            inner: mem::ManuallyDrop::new(value),
        }
    }

    pub unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
        mem::ManuallyDrop::take(&mut slot.inner)
    }

    pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
        mem::ManuallyDrop::into_inner(slot.inner)
    }
}

impl<T: ?Sized> std::ops::DerefMut for ManuallyDrop<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<T: ?Sized> std::ops::Deref for ManuallyDrop<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

#[repr(transparent)]
pub struct MaybeUninit<T> {
    inner: mem::MaybeUninit<T>,
}

unsafe impl<T> Forget for MaybeUninit<T> {}

impl<T> MaybeUninit<T> {
    pub const fn new(val: T) -> Self
    where
        T: Forget,
    {
        MaybeUninit {
            inner: mem::MaybeUninit::new(val),
        }
    }

    pub const unsafe fn new_unchecked(val: T) -> Self {
        MaybeUninit {
            inner: mem::MaybeUninit::new(val),
        }
    }

    pub const fn uninit() -> Self {
        MaybeUninit {
            inner: mem::MaybeUninit::uninit(),
        }
    }

    pub const fn zeroed() -> Self {
        MaybeUninit {
            inner: mem::MaybeUninit::uninit(),
        }
    }

    pub fn write(&mut self, val: T) -> &mut T
    where
        T: Forget,
    {
        self.inner.write(val)
    }

    pub unsafe fn write_unchecked(&mut self, val: T) -> &mut T {
        self.inner.write(val)
    }

    pub const fn as_ptr(&self) -> *const T {
        self.inner.as_ptr()
    }

    pub fn as_mut_ptr(&mut self) -> *mut T {
        self.inner.as_mut_ptr()
    }

    pub const unsafe fn assume_init(self) -> T {
        self.inner.assume_init()
    }

    pub const unsafe fn assume_init_read(&self) -> T {
        self.inner.assume_init_read()
    }

    pub unsafe fn assume_init_drop(&mut self) {
        self.inner.assume_init_drop()
    }

    pub const unsafe fn assume_init_ref(&self) -> &T {
        self.inner.assume_init_ref()
    }

    pub unsafe fn assume_init_mut(&mut self) -> &mut T {
        self.inner.assume_init_mut()
    }
}

impl<T: Copy> Copy for MaybeUninit<T> {}

impl<T: Copy> Clone for MaybeUninit<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> std::fmt::Debug for MaybeUninit<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner.fmt(f)
    }
}