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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/// ResultLike macro

pub trait ResultLike<T, E> {}

#[macro_export]
macro_rules! result_like {
    (pub $(($pub:ident))? $Result:ident, $Ok:ident, $Err:ident) => {
        #[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, is_macro::Is)]
        pub$(($pub))? enum $Result<T, E> {
            $Ok(T),
            $Err(E),
        }
        result_like::impl_result_like!($Result, $Ok, $Err);
    };
    ($Result:ident, $Ok:ident, $Err:ident) => {
        #[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, is_macro::Is)]
        enum $Result<T, E> {
            $Ok(T),
            $Err(E),
        }
        result_like::impl_result_like!($Result, $Ok, $Err);
    };
    (pub $(($pub:ident))? $Result:ident) => {
        result_like::result_like!(pub $(($pub))? $Result, Ok, Err);
    };
    ($Result:ident) => {
        result_like::result_like!($Result, Ok, Err);
    };
}

#[macro_export]
macro_rules! impl_result_like {
    ($Result:ident, $Ok:ident, $Err:ident) => {
        impl<T, E> result_like::ResultLike<T, E> for $Result<T, E> {}

        impl<T, E> $Result<T, E> {
            #[inline]
            pub fn from_result(result: Result<T, E>) -> Self {
                match result {
                    Ok(v) => $Result::$Ok(v),
                    Err(e) => $Result::$Err(e),
                }
            }

            #[inline]
            pub fn into_result(self) -> Result<T, E> {
                match self {
                    $Result::$Ok(v) => Ok(v),
                    $Result::$Err(e) => Err(e),
                }
            }

            #[inline]
            pub fn as_result(&self) -> Result<&T, &E> {
                match self {
                    $Result::$Ok(ref x) => Ok(x),
                    $Result::$Err(ref x) => Err(x),
                }
            }

            #[inline]
            pub fn as_result_mut(&mut self) -> Result<&mut T, &mut E> {
                match self {
                    $Result::$Ok(ref mut x) => Ok(x),
                    $Result::$Err(ref mut x) => Err(x),
                }
            }

            // contains
            // contains_err

            #[inline]
            pub fn as_ref(&self) -> $Result<&T, &E> {
                match self {
                    $Result::$Ok(ref x) => $Result::$Ok(x),
                    $Result::$Err(ref x) => $Result::$Err(x),
                }
            }

            #[inline]
            pub fn as_mut(&mut self) -> $Result<&mut T, &mut E> {
                match self {
                    $Result::$Ok(ref mut x) => $Result::$Ok(x),
                    $Result::$Err(ref mut x) => $Result::$Err(x),
                }
            }

            #[inline]
            pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> $Result<U, E> {
                match self {
                    $Result::$Ok(t) => $Result::$Ok(op(t)),
                    $Result::$Err(e) => $Result::$Err(e),
                }
            }

            #[inline]
            pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
                match self {
                    $Result::$Ok(t) => f(t),
                    $Result::$Err(_) => default,
                }
            }

            #[inline]
            pub fn map_or_else<U, M: FnOnce(T) -> U, F: FnOnce(E) -> U>(
                self,
                fallback: F,
                map: M,
            ) -> U {
                self.map(map).unwrap_or_else(fallback)
            }

            #[inline]
            pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> $Result<T, F> {
                match self {
                    $Result::$Ok(t) => $Result::$Ok(t),
                    $Result::$Err(e) => $Result::$Err(op(e)),
                }
            }

            // iter
            // iter_mut

            #[inline]
            pub fn and<U>(self, res: $Result<U, E>) -> $Result<U, E> {
                match self {
                    $Result::$Ok(_) => res,
                    $Result::$Err(e) => $Result::$Err(e),
                }
            }

            #[inline]
            pub fn and_then<U, F: FnOnce(T) -> $Result<U, E>>(self, op: F) -> $Result<U, E> {
                match self {
                    $Result::$Ok(t) => op(t),
                    $Result::$Err(e) => $Result::$Err(e),
                }
            }

            #[inline]
            pub fn or<F>(self, res: $Result<T, F>) -> $Result<T, F> {
                match self {
                    $Result::$Ok(v) => $Result::$Ok(v),
                    $Result::$Err(_) => res,
                }
            }

            #[inline]
            pub fn or_else<F, O: FnOnce(E) -> $Result<T, F>>(self, op: O) -> $Result<T, F> {
                match self {
                    $Result::$Ok(t) => $Result::$Ok(t),
                    $Result::$Err(e) => op(e),
                }
            }

            #[inline]
            pub fn unwrap_or(self, optb: T) -> T {
                self.into_result().unwrap_or(optb)
            }

            #[inline]
            pub fn unwrap_or_else<F: FnOnce(E) -> T>(self, op: F) -> T {
                self.into_result().unwrap_or_else(op)
            }
        }

        impl<T: Copy, E> $Result<&T, E> {
            pub fn copied(self) -> $Result<T, E> {
                self.map(|&t| t)
            }
        }

        impl<T: Copy, E> $Result<&mut T, E> {
            pub fn copied(self) -> $Result<T, E> {
                self.map(|&mut t| t)
            }
        }

        impl<T: Clone, E> $Result<&T, E> {
            pub fn cloned(self) -> $Result<T, E> {
                self.map(|t| t.clone())
            }
        }

        impl<T: Clone, E> $Result<&mut T, E> {
            pub fn cloned(self) -> $Result<T, E> {
                self.map(|t| t.clone())
            }
        }

        impl<T, E: std::fmt::Debug> $Result<T, E> {
            #[inline]
            pub fn expect(self, msg: &str) -> T {
                self.into_result().expect(msg)
            }

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

        impl<T: std::fmt::Debug, E> $Result<T, E> {
            // #[inline]
            // pub fn expect_err(self, msg: &str) -> E {
            //     self.into_result().expect_err(msg)
            // }

            #[inline]
            pub fn unwrap_err(self) -> E {
                self.into_result().unwrap_err()
            }
        }

        impl<T: Default, E> $Result<T, E> {
            #[inline]
            pub fn unwrap_or_default(self) -> T {
                self.into_result().unwrap_or_default()
            }
        }

        // into_ok

        impl<T: std::ops::Deref, E> $Result<T, E> {
            pub fn as_deref_ok(&self) -> $Result<&T::Target, &E> {
                self.as_ref().map(|t| t.deref())
            }
        }

        impl<T, E: std::ops::Deref> $Result<T, E> {
            pub fn as_deref_err(&self) -> $Result<&T, &E::Target> {
                self.as_ref().map_err(|e| e.deref())
            }
        }

        impl<T: std::ops::Deref, E: std::ops::Deref> $Result<T, E> {
            pub fn as_deref(&self) -> $Result<&T::Target, &E::Target> {
                self.as_ref().map(|t| t.deref()).map_err(|e| e.deref())
            }
        }

        impl<T: std::ops::DerefMut, E> $Result<T, E> {
            pub fn as_deref_mut_ok(&mut self) -> $Result<&mut T::Target, &mut E> {
                self.as_mut().map(|t| t.deref_mut())
            }
        }

        impl<T, E: std::ops::DerefMut> $Result<T, E> {
            pub fn as_deref_mut_err(&mut self) -> $Result<&mut T, &mut E::Target> {
                self.as_mut().map_err(|e| e.deref_mut())
            }
        }

        impl<T: std::ops::DerefMut, E: std::ops::DerefMut> $Result<T, E> {
            pub fn as_deref_mut(&mut self) -> $Result<&mut T::Target, &mut E::Target> {
                self.as_mut()
                    .map(|t| t.deref_mut())
                    .map_err(|e| e.deref_mut())
            }
        }

        impl<T, E> $Result<Option<T>, E> {
            #[inline]
            pub fn transpose(self) -> Option<$Result<T, E>> {
                self.into_result()
                    .transpose()
                    .map(|r| $Result::from_result(r))
            }
        }

        // flatten

        impl<T: Clone, E: Clone> Clone for $Result<T, E> {
            #[inline]
            fn clone(&self) -> Self {
                match self {
                    $Result::$Ok(x) => $Result::$Ok(x.clone()),
                    $Result::$Err(x) => $Result::$Err(x.clone()),
                }
            }

            #[inline]
            fn clone_from(&mut self, source: &Self) {
                match (self, source) {
                    ($Result::$Ok(to), $Result::$Ok(from)) => to.clone_from(from),
                    ($Result::$Err(to), $Result::$Err(from)) => to.clone_from(from),
                    (to, from) => *to = from.clone(),
                }
            }
        }

        impl<T, E> IntoIterator for $Result<T, E> {
            type Item = T;
            type IntoIter = std::result::IntoIter<T>;

            #[inline]
            fn into_iter(self) -> std::result::IntoIter<T> {
                self.into_result().into_iter()
            }
        }

        // impl<'a, T, E> IntoIterator for &'a $Result<T, E> {
        //     type Item = &'a T;
        //     type IntoIter = std::result::Iter<'a, T>;

        //     fn into_iter(self) -> std::result::Iter<'a, T> {
        //         self.into_result().iter()
        //     }
        // }

        // impl<'a, T, E> IntoIterator for &'a mut Result<T, E> {
        //     type Item = &'a mut T;
        //     type IntoIter = IterMut<'a, T>;

        //     fn into_iter(self) -> IterMut<'a, T> {
        //         self.iter_mut()
        //     }
        // }
    };
    ($Result:ident) => {
        result_like::impl_result_like!($Result, Ok, Err);
    };
}