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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use core::{mem::MaybeUninit, ops::{Deref, DerefMut}};

use crate::Maybe;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MaybeCell<T, const IS_SOME: bool>([T; IS_SOME as usize])
where
    [(); IS_SOME as usize]:;

impl<T> MaybeCell<T, false>
{
    pub const fn new() -> Self
    {
        Self([])
    }
}
impl<T> MaybeCell<T, true>
{
    pub const fn new(value: T) -> Self
    {
        Self([value])
    }

    pub const fn into_value(self) -> T
    {
        let x = unsafe {core::ptr::read(self.0.as_ptr())};
        core::mem::forget(self);
        x
    }
    pub const fn as_value(&self) -> &T
    {
        &self.0[0]
    }
    pub const fn as_value_mut(&mut self) -> &mut T
    {
        &mut self.0[0]
    }
}
impl<T, const IS_SOME: bool> MaybeCell<T, IS_SOME>
where
    [(); IS_SOME as usize]:
{
    pub fn from_fn<F>(func: F) -> Self
    where
        F: FnOnce() -> T
    {
        let mut x = MaybeUninit::uninit_array();
        if IS_SOME
        {
            unsafe {x.as_mut_ptr().write(MaybeUninit::new(func()))};
        }
        Self(unsafe {MaybeUninit::array_assume_init(x)})
    }

    pub fn map<F, O>(self, map: F) -> MaybeCell<O, IS_SOME>
    where
        F: FnOnce(T) -> O
    {
        let mut x = MaybeUninit::uninit_array();
        if IS_SOME
        {
            unsafe {x.as_mut_ptr().write(MaybeUninit::new(map(self.0.as_ptr().read())))};
        }
        core::mem::forget(self);
        MaybeCell(unsafe {MaybeUninit::array_assume_init(x)})
    }

    pub const fn into_option(self) -> Option<T>
    {
        if IS_SOME
        {
            let x = unsafe {core::ptr::read(self.0.as_ptr())};
            core::mem::forget(self);
            Some(x)
        }
        else
        {
            core::mem::forget(self);
            None
        }
    }

    pub const fn get(&self) -> Option<&T>
    {
        if IS_SOME
        {
            Some(&self.0[0])
        }
        else
        {
            None
        }
    }

    pub const fn get_mut(&mut self) -> Option<&mut T>
    {
        if IS_SOME
        {
            Some(&mut self.0[0])
        }
        else
        {
            None
        }
    }
}

impl<T> From<T> for MaybeCell<T, true>
{
    fn from(value: T) -> Self
    {
        Self::new(value)
    }
}
impl<T> Deref for MaybeCell<T, true>
{
    type Target = T;

    fn deref(&self) -> &Self::Target
    {
        self.as_value()
    }
}
impl<T> DerefMut for MaybeCell<T, true>
{
    fn deref_mut(&mut self) -> &mut Self::Target
    {
        self.as_value_mut()
    }
}

impl<T> Default for MaybeCell<T, false>
{
    fn default() -> Self
    {
        Self::new()
    }
}
impl<T> Default for MaybeCell<T, true>
where
    T: Default
{
    fn default() -> Self
    {
        Self::new(T::default())
    }
}

impl<T, const IS_SOME: bool> Maybe<T> for MaybeCell<T, IS_SOME>
where
    [(); IS_SOME as usize]:
{
    fn into_option(self) -> Option<T>
    {
        self.into_option()
    }

    fn as_option(&self) -> Option<&T>
    {
        self.get()
    }

    fn as_option_mut(&mut self) -> Option<&mut T>
    {
        self.get_mut()
    }
}