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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::mem;

/// Ffi-safe equivalent of the `Option<_>` type.
///
/// `Option<_>` is also ffi-safe for NonNull/NonZero types,and references.
///
/// Use ROption<_> when `Option<_>` would not be viable.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[repr(C)]
#[derive(StableAbi)]
#[sabi(inside_abi_stable_crate)]
pub enum ROption<T> {
    RSome(T),
    RNone,
}

pub use self::ROption::*;

impl<T> ROption<T> {
    #[inline]
    pub fn as_ref(&self) -> ROption<&T> {
        match self {
            RSome(v) => RSome(v),
            RNone => RNone,
        }
    }

    #[inline]
    pub fn as_mut(&mut self) -> ROption<&mut T> {
        match self {
            RSome(v) => RSome(v),
            RNone => RNone,
        }
    }

    #[inline]
    pub fn as_option(&self) -> Option<&T> {
        match self {
            RSome(v) => Some(v),
            RNone => None,
        }
    }

    #[inline]
    pub fn as_option_mut(&mut self) -> Option<&mut T> {
        match self {
            RSome(v) => Some(v),
            RNone => None,
        }
    }

    #[inline]
    pub fn into_option(self) -> Option<T> {
        self.into()
    }

    #[inline]
    pub fn expect(self, msg: &str) -> T {
        self.into_option().expect(msg)
    }

    #[inline]
    pub fn unwrap(self) -> T {
        self.into_option().unwrap()
    }

    #[inline]
    pub fn unwrap_or(self, def: T) -> T {
        match self {
            RSome(x) => x,
            RNone => def,
        }
    }

    #[inline]
    pub fn unwrap_or_else<F>(self, f: F) -> T
    where
        F: FnOnce() -> T,
    {
        match self {
            RSome(x) => x,
            RNone => f(),
        }
    }

    #[inline]
    pub fn map<U, F>(self, f: F) -> ROption<U>
    where
        F: FnOnce(T) -> U,
    {
        match self {
            RSome(x) => RSome(f(x)),
            RNone => RNone,
        }
    }

    #[inline]
    pub fn map_or<U, F>(self, default: U, f: F) -> U
    where
        F: FnOnce(T) -> U,
    {
        match self {
            RSome(t) => f(t),
            RNone => default,
        }
    }

    #[inline]
    pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
    where
        D: FnOnce() -> U,
        F: FnOnce(T) -> U,
    {
        match self {
            RSome(t) => f(t),
            RNone => default(),
        }
    }

    pub fn filter<P>(self, predicate: P) -> Self
    where
        P: FnOnce(&T) -> bool,
    {
        if let RSome(x) = self {
            if predicate(&x) {
                return RSome(x);
            }
        }
        RNone
    }

    #[inline]
    pub fn or(self, optb: ROption<T>) -> ROption<T> {
        match self {
            RSome(_) => self,
            RNone => optb,
        }
    }

    #[inline]
    pub fn or_else<F>(self, f: F) -> ROption<T>
    where
        F: FnOnce() -> ROption<T>,
    {
        match self {
            RSome(_) => self,
            RNone => f(),
        }
    }

    #[inline]
    pub fn xor(self, optb: ROption<T>) -> ROption<T> {
        match (self, optb) {
            (RSome(a), RNone) => RSome(a),
            (RNone, RSome(b)) => RSome(b),
            _ => RNone,
        }
    }

    #[inline]
    pub fn get_or_insert(&mut self, v: T) -> &mut T {
        match *self {
            RNone => *self = RSome(v),
            _ => (),
        }

        match *self {
            RSome(ref mut v) => v,
            RNone => unreachable!(),
        }
    }

    #[inline]
    pub fn get_or_insert_with<F>(&mut self, f: F) -> &mut T
    where
        F: FnOnce() -> T,
    {
        match *self {
            RNone => *self = RSome(f()),
            _ => (),
        }

        match *self {
            RSome(ref mut v) => v,
            RNone => unreachable!(),
        }
    }

    #[inline]
    pub fn take(&mut self) -> ROption<T> {
        mem::replace(self, RNone)
    }

    #[inline]
    pub fn replace(&mut self, value: T) -> ROption<T> {
        mem::replace(self, RSome(value))
    }
}

impl_from_rust_repr! {
    impl[T] From<Option<T>> for ROption<T> {
        fn(this){
            match this {
                Some(v) => RSome(v),
                None => RNone,
            }
        }
    }
}

impl_into_rust_repr! {
    impl[T] Into<Option<T>> for ROption<T> {
        fn(this){
            match this {
                RSome(v) => Some(v),
                RNone => None,
            }
        }
    }
}

impl<'de, T> Deserialize<'de> for ROption<T>
where
    T: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Option::deserialize(deserializer).map(Self::from)
    }
}

impl<T> Serialize for ROption<T>
where
    T: Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.as_option().serialize(serializer)
    }
}