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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! A generic ```Either``` type implementation, accompanied by an ecosystem of helper traits, types, and others

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(all(feature = "alloc", feature = "std"))]
compile_error!("`alloc` and `std` features cannot be enabled simultaneously");

mod lr;
pub use lr::*;

mod refr;
use pin_project::pin_project;
pub use refr::*;

/// Iterator extensions
pub mod iter;

cfg_if::cfg_if! {
    if #[cfg(feature = "serialize")] {
        mod serde;
        pub use self::serde::*;
        use ::serde::{Serialize, Deserialize};
    }
}

cfg_if::cfg_if! {
    if #[cfg(feature = "random")] {
        mod random;
        pub use random::*;
    }
}

cfg_if::cfg_if! {
    if #[cfg(feature = "macro")] {
        mod macros;
        pub use macros::*;
    }
}

cfg_if::cfg_if! {
    if #[cfg(feature = "async")] {
        /// Future/Stream extensions 
        mod future;
        pub use future::*;
    }
}

pub mod prelude {
    pub use crate::Either;
    pub use crate::Either::{Left, Right};
}

use core::{ops::{Deref, DerefMut}, fmt::Display};
use core::hint::unreachable_unchecked;
use self::Either::*;

/// Generic data type that represents either a value
/// that's of one type or another.
#[pin_project(project = EitherProj)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", serde(untagged))]
pub enum Either<A,B> {
    Left (#[pin] A),
    Right (#[pin] B)
}

impl<A,B> Either<A,B> {
    /// Returns `true` if the value is a `Left`
    #[inline(always)]
    pub const fn is_left (&self) -> bool {
        matches!(self, Left(_))
    }

    /// Returns `true` if the value is a `Right`
    #[inline(always)]
    pub const fn is_right (&self) -> bool {
        matches!(self, Right(_))
    }

    /// Returns a new `Either` with a reference to the value inside
    #[inline(always)]
    pub const fn as_ref (&self) -> Either<&A, &B> {
        match self {
            Left(x) => Either::Left(x),
            Right(x) => Either::Right(x)
        }
    }

    /// Returns a new `Either` with a mutable reference to the value inside
    #[inline(always)]
    pub fn as_mut (&mut self) -> Either<&mut A, &mut B> {
        match self {
            Left(x) => Left(x),
            Right(x) => Right(x)
        }
    }

    /// ## Example ##
    /// ```rust
    /// use elor::Either::{self, *};
    /// 
    /// fn main () {
    ///     let alpha : Either<String, Vec<u8>> = Left("hello world".to_string());
    ///     let beta : Either<&str, &[u8]> = alpha.as_deref();
    ///     assert_eq!(beta, Left("hello world"))
    /// }
    /// ```
    #[inline(always)]
    pub fn as_deref (&self) -> Either<&<A as Deref>::Target, &<B as Deref>::Target> where A: Deref, B: Deref {
        match self {
            Left(x) => Left(x.deref()),
            Right(x) => Right(x.deref())
        }
    }

    /// ## Example ##
    /// ```rust
    /// use elor::Either::{self, *};
    /// 
    /// fn main () {
    ///     let mut alpha : Either<String, Vec<u8>> = Left("hello world".to_string());
    ///     let beta : Either<&mut str, &mut [u8]> = alpha.as_deref_mut();
    ///     assert_eq!(beta, Left("hello world"))
    /// }
    /// ```
    #[inline(always)]
    pub fn as_deref_mut (&mut self) -> Either<&mut <A as Deref>::Target, &mut <B as Deref>::Target> where A: DerefMut, B: DerefMut {
        match self {
            Left(x) => Left(x.deref_mut()),
            Right(x) => Right(x.deref_mut())
        }
    }

    /// Returns a new ```Either``` with a clone of the value inside
    #[inline(always)]
    pub fn cloned (&self) -> Either<A,B> where A: Clone, B: Clone {
        match self {
            Left(x) => Left(x.clone()),
            Right(x) => Right(x.clone())
        }
    }

    /// Returns a new ```Either``` with a Copy of the value inside
    #[inline(always)]
    pub fn copied (&self) -> Either<A,B> where A: Copy, B: Copy {
        match self {
            Left(x) => Left(*x),
            Right(x) => Right(*x)
        }
    }

    /// Converts a single ```Either``` into two ```Option```
    #[inline(always)]
    pub fn unzip (self) -> (Option<A>, Option<B>) {
        match self {
            Left(x) => (Some(x), None),
            Right(x) => (None, Some(x))
        }
    }

    /// Returns the left value, panicking with a custom message if the value is on the right
    #[inline(always)]
    pub fn expect_left (self, msg: &str) -> A {
        match self {
            Left(x) => x,
            _ => panic!("{msg}")
        }
    }

    /// Returns the right value, panicking with a custom message if the value is on the left
    #[inline(always)]
    pub fn expect_right (self, msg: &str) -> B {
        match self {
            Right(x) => x,
            _ => panic!("{msg}")
        }
    }

    /// Returns the left value, ```None``` otherwise
    #[inline(always)]
    pub fn left (self) -> Option<A> {
        match self {
            Left(x) => Some(x),
            _ => None
        }
    }

    /// Returns the right value, ```None``` otherwise
    #[inline(always)]
    pub fn right (self) -> Option<B> {
        match self {
            Right(x) => Some(x),
            _ => None
        }
    }

    /// Returns `Ok` if the value is on the left, otherwise returning an `Err` with the right-side value.
    #[inline(always)]
    pub fn ok_left (self) -> Result<A, B> {
        match self {
            Self::Left(x) => Ok(x),
            Self::Right(e) => Err(e)
        }
    }

    /// Returns `Ok` if the value is on the right, otherwise returning an `Err` with the left-side value.
    #[inline(always)]
    pub fn ok_right (self) -> Result<B, A> {
        match self {
            Self::Right(x) => Ok(x),
            Self::Left(e) => Err(e)
        }
    }

    #[deprecated(since = "1.1.0", note = "use ```left``` instead")]
    #[inline(always)]
    pub fn some_left (self) -> Option<A> {
        self.left()
    }

    #[deprecated(since = "1.1.0", note = "use ```right``` instead")]
    #[inline(always)]
    pub fn some_right (self) -> Option<B> {
        self.right()
    }

    /// Returns the left value, panicking if the value is on the right
    #[inline(always)]
    pub fn unwrap_left (self) -> A {
        self.expect_left("called `Either::unwrap_left()` on a `Right` value")
    }

    /// Returns the right value, panicking if the value is on the left
    #[inline(always)]
    pub fn unwrap_right (self) -> A {
        self.expect_left("called `Either::unwrap_right()` on a `Left` value")
    }

    /// Returns the left value without checking if the value is on the right
    #[inline(always)]
    pub unsafe fn unwrap_left_unchecked (self) -> A {
        match self {
            Left(x) => x,
            _ => unreachable_unchecked()
        }
    }

    /// Returns the right value without checking if the value is on the left
    #[inline(always)]
    pub unsafe fn unwrap_right_unchecked (self) -> B {
        match self {
            Right(x) => x,
            _ => unreachable_unchecked()
        }
    }

    /// Returns the left value, or the specified value if the value is on the right
    #[inline(always)]
    pub fn unwrap_left_or (self, default: A) -> A {
        match self {
            Left(x) => x,
            _ => default
        }
    }

    /// Returns the right value, or the specified value if the value is on the left
    #[inline(always)]
    pub fn unwrap_right_or (self, default: B) -> B {
        match self {
            Right(x) => x,
            _ => default
        }
    }

    /// Returns the left value, or calls the specified function if the value is on the right
    #[inline(always)]
    pub fn unwrap_left_or_else<F: FnOnce() -> A> (self, f: F) -> A {
        match self {
            Left(x) => x,
            _ => f()
        }
    }

    /// Returns the right value, or calls the specified function if the value is on the left
    #[inline(always)]
    pub fn unwrap_right_or_else<F: FnOnce() -> B> (self, f: F) -> B {
        match self {
            Right(x) => x,
            _ => f()
        }
    }

    #[inline(always)]
    pub fn map <X, Y, F: FnOnce(A) -> X, G: FnOnce(B) -> Y> (self, f: F, g: G) -> Either<X,Y> {
        match self {
            Left(x) => Left(f(x)),
            Right(x) => Right(g(x))
        }
    }

    #[inline(always)]
    pub fn map_left<T, F: FnOnce(A) -> T> (self, f: F) -> Either<T,B> {
        match self {
            Left(x) => Left(f(x)),
            Right(x) => Right(x)
        }
    }

    #[inline(always)]
    pub fn map_right<T, F: FnOnce(B) -> T> (self, f: F) -> Either<A,T> {
        match self {
            Left(x) => Left(x),
            Right(x) => Right(f(x))
        }
    }

    #[inline(always)]
    pub fn fold<T, F: FnOnce(A) -> T, G: FnOnce(B) -> T> (self, f: F, g: G) -> T {
        match self {
            Left(x) => f(x),
            Right(x) => g(x)
        }
    }

    #[inline(always)]
    pub fn fold_left<F: FnOnce(B) -> A> (self, f: F) -> A {
        match self {
            Left(x) => x,
            Right(x) => f(x)
        }
    }

    #[inline(always)]
    pub fn fold_right<F: FnOnce(A) -> B> (self, f: F) -> B {
        match self {
            Right(x) => x,
            Left(x) => f(x)
        }
    }

    #[inline(always)]
    pub fn inverse (self) -> Either<B, A> {
        match self {
            Left(x) => Right(x),
            Right(x) => Left(x)
        }
    }
}

impl<A,B> Either<Either<A,B>,B> {
    #[inline(always)]
    pub fn flatten_left_right (self) -> Either<A,B> {
        match self {
            Left(Left(x)) => Left(x),
            Left(Right(x)) => Right(x),
            Right(x) => Right(x) 
        }
    }
}

impl<A,B> Either<A,Either<A,B>> {
    #[inline(always)]
    pub fn flatten_right_left (self) -> Either<A,B> {
        match self {
            Right(Left(x)) => Left(x),
            Right(Right(x)) => Right(x),
            Left(x) => Left(x)
        }
    }
}

impl<A,B> Either<Either<A,B>,A> {
    #[inline(always)]
    pub fn flatten_left_left (self) -> Either<A,B> {
        match self {
            Left(Left(x)) => Left(x),
            Left(Right(x)) => Right(x),
            Right(x) => Left(x) 
        }
    }
}

impl<A,B> Either<A,Either<B,A>> {
    #[inline(always)]
    pub fn flatten_right_right (self) -> Either<A,B> {
        match self {
            Right(Left(x)) => Right(x),
            Right(Right(x)) => Left(x),
            Left(x) => Left(x)
        }
    }
}

impl<A,B,EA,EB> Either<Result<A,EA>, Result<B,EB>> {
    #[inline(always)]
    pub fn flatten_result (self) -> Result<Either<A,B>, Either<EA,EB>> {
        match self {
            Left(Ok(x)) => Ok(Left(x)),
            Left(Err(e)) => Err(Left(e)),
            Right(Ok(x)) => Ok(Right(x)),
            Right(Err(e)) => Err(Right(e)) 
        }
    }

    #[inline(always)]
    pub fn expand_result (result: Result<Either<A,B>, Either<EA,EB>>) -> Self {
        match result {
            Ok(Left(x)) => Left(Ok(x)),
            Ok(Right(x)) => Right(Ok(x)),
            Err(Left(e)) => Left(Err(e)),
            Err(Right(e)) => Right(Err(e)) 
        }
    }
}

impl<A,B> Either<Option<A>, Option<B>> {
    #[inline(always)]
    pub fn flatten_option (self) -> Option<Either<A,B>> {
        match self {
            Left(Some(x)) => Some(Left(x)),
            Right(Some(x)) => Some(Right(x)),
            _ => None
        }
    }
}

impl<A: Deref, B: Deref<Target = A::Target>> Deref for Either<A,B> {
    type Target = A::Target;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        match self {
            Left(x) => x.deref(),
            Right(x) => x.deref()
        }
    }
}

impl<A: DerefMut, B: DerefMut<Target = A::Target>> DerefMut for Either<A,B> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            Left(x) => x.deref_mut(),
            Right(x) => x.deref_mut()
        }
    }
}

impl<T> From<Option<T>> for Either<T,()> {
    #[inline(always)]
    fn from(x: Option<T>) -> Self {
        match x {
            Some(x) => Left(x),
            None => Right(())
        }
    }
}

impl<T> Into<Option<T>> for Either<T,()> {
    #[inline(always)]
    fn into(self) -> Option<T> {
        match self {
            Left(x) => Some(x),
            _ => None
        }
    }
}

impl<T,E> From<Result<T,E>> for Either<T,E> {
    #[inline(always)]
    fn from(x: Result<T,E>) -> Self {
        match x {
            Ok(x) => Left(x),
            Err(e) => Right(e)
        }
    }
}

impl<T,E> Into<Result<T,E>> for Either<T,E> {
    #[inline(always)]
    fn into(self) -> Result<T,E> {
        match self {
            Left(x) => Ok(x),
            Right(e) => Err(e)
        }
    }
}

impl<A,B> Display for Either<A,B> where A: Display, B: Display {
    #[inline(always)]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Left(x) => x.fmt(f),
            Right(x) => x.fmt(f)
        }
    }
}