ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional type classes
Documentation
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! `EitherT` Monad Transformer
//!
//! `EitherT` adds error handling with a specific error type to any base monad.
//! It wraps a computation that returns `M<Result<A, E>>` and provides a monadic
//! interface that handles the `Result` layer automatically.
//!
//! # Example
//!
//! ```
//! # #[cfg(feature = "alloc")]
//! # fn main() {
//! use ordofp_core::transformers::EitherT;
//!
//! // EitherT over Option - combines error handling with optionality
//! let computation: EitherT<Option<Result<i32, &str>>> = EitherT::right(42);
//! let result = computation
//!     .map(|x| x * 2)
//!     .flat_map(|x| if x > 50 { EitherT::right(x) } else { EitherT::left("too small") });
//! assert_eq!(result.run(), Some(Ok(84)));
//! # }
//! # #[cfg(not(feature = "alloc"))]
//! # fn main() {}
//! ```

use super::MonadTransformer;

/// The `EitherT` monad transformer adds error handling to any base monad `M`.
///
/// `EitherT<M>` wraps `M<Result<A, E>>` and provides a unified interface for working
/// with fallible computations inside another monadic context.
///
/// # Type Parameters
///
/// - `M`: The base monad wrapping `Result<A, E>` (e.g., `Option<Result<A, E>>`)
///
/// # Terminology
///
/// - `right`: The success case (like `Ok`)
/// - `left`: The error case (like `Err`)
///
/// # Examples
///
/// ## Basic Usage
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # fn main() {
/// use ordofp_core::transformers::EitherT;
///
/// // Create a successful EitherT
/// let ok: EitherT<Option<Result<i32, &str>>> = EitherT::right(42);
/// assert_eq!(ok.run(), Some(Ok(42)));
///
/// // Create a failed EitherT
/// let err: EitherT<Option<Result<i32, &str>>> = EitherT::left("error");
/// assert_eq!(err.run(), Some(Err("error")));
/// # }
/// # #[cfg(not(feature = "alloc"))]
/// # fn main() {}
/// ```
///
/// ## Chaining Computations
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # fn main() {
/// use ordofp_core::transformers::EitherT;
///
/// fn safe_div(a: i32, b: i32) -> EitherT<Option<Result<i32, &'static str>>> {
///     if b == 0 {
///         EitherT::left("division by zero")
///     } else {
///         EitherT::right(a / b)
///     }
/// }
///
/// let result = EitherT::<Option<Result<i32, &str>>>::right(100)
///     .flat_map(|x| safe_div(x, 2))
///     .flat_map(|x| safe_div(x, 5));
/// assert_eq!(result.run(), Some(Ok(10)));
///
/// // Division by zero returns an error
/// let result2 = EitherT::<Option<Result<i32, &str>>>::right(100)
///     .flat_map(|x| safe_div(x, 0));
/// assert_eq!(result2.run(), Some(Err("division by zero")));
/// # }
/// # #[cfg(not(feature = "alloc"))]
/// # fn main() {}
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EitherT<M> {
    /// The wrapped computation
    inner: M,
}

impl<M> EitherT<M> {
    /// Creates a new `EitherT` from a wrapped computation.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let inner: Option<Result<i32, &str>> = Some(Ok(42));
    /// let either_t = EitherT::new(inner);
    /// assert_eq!(either_t.run(), Some(Ok(42)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn new(inner: M) -> Self {
        EitherT { inner }
    }

    /// Runs the transformer, extracting the inner computation.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let either_t: EitherT<Option<Result<i32, &str>>> = EitherT::right(42);
    /// let result: Option<Result<i32, &str>> = either_t.run();
    /// assert_eq!(result, Some(Ok(42)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn run(self) -> M {
        self.inner
    }

    /// Returns a reference to the inner computation.
    #[inline]
    pub fn inner_ref(&self) -> &M {
        &self.inner
    }
}

// ============================================================================
// EitherT over Option
// ============================================================================

impl<A, E> EitherT<Option<Result<A, E>>> {
    /// Creates an `EitherT` containing a success value over `Option`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let ok: EitherT<Option<Result<i32, &str>>> = EitherT::right(42);
    /// assert_eq!(ok.run(), Some(Ok(42)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn right(value: A) -> Self {
        EitherT::new(Some(Ok(value)))
    }

    /// Creates an `EitherT` containing an error value over `Option`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let err: EitherT<Option<Result<i32, &str>>> = EitherT::left("error");
    /// assert_eq!(err.run(), Some(Err("error")));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn left(error: E) -> Self {
        EitherT::new(Some(Err(error)))
    }

    /// Creates an `EitherT` representing an absent computation (None).
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let absent: EitherT<Option<Result<i32, &str>>> = EitherT::absent();
    /// assert_eq!(absent.run(), None);
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn absent() -> Self {
        EitherT::new(None)
    }

    /// Lifts an `Option` value into `EitherT`, treating `Some` as success.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let some: Option<i32> = Some(42);
    /// let lifted: EitherT<Option<Result<i32, &str>>> = EitherT::lift_m(some);
    /// assert_eq!(lifted.run(), Some(Ok(42)));
    ///
    /// let none: Option<i32> = None;
    /// let lifted_none: EitherT<Option<Result<i32, &str>>> = EitherT::lift_m(none);
    /// assert_eq!(lifted_none.run(), None);
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn lift_m(option: Option<A>) -> Self {
        EitherT::new(option.map(Ok))
    }

    /// Maps a function over the success value.
    ///
    /// If the computation is `Some(Ok(a))`, applies `f` to `a`.
    /// If the computation is `Some(Err(e))` or `None`, propagates unchanged.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let ok: EitherT<Option<Result<i32, &str>>> = EitherT::right(21);
    /// let doubled = ok.map(|x| x * 2);
    /// assert_eq!(doubled.run(), Some(Ok(42)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn map<B, F>(self, f: F) -> EitherT<Option<Result<B, E>>>
    where
        F: FnOnce(A) -> B,
    {
        EitherT::new(self.inner.map(|res| res.map(f)))
    }

    /// Maps a function over the error value.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let err: EitherT<Option<Result<i32, i32>>> = EitherT::left(1);
    /// let mapped = err.map_left(|e| e * 2);
    /// assert_eq!(mapped.run(), Some(Err(2)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn map_left<E2, F>(self, f: F) -> EitherT<Option<Result<A, E2>>>
    where
        F: FnOnce(E) -> E2,
    {
        EitherT::new(self.inner.map(|res| res.map_err(f)))
    }

    /// Chains a computation that returns an `EitherT`.
    ///
    /// Also known as `bind` or `>>=`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let ok: EitherT<Option<Result<i32, &str>>> = EitherT::right(10);
    /// let result = ok.flat_map(|x| {
    ///     if x > 5 { EitherT::right(x * 2) }
    ///     else { EitherT::left("too small") }
    /// });
    /// assert_eq!(result.run(), Some(Ok(20)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn flat_map<B, F>(self, f: F) -> EitherT<Option<Result<B, E>>>
    where
        F: FnOnce(A) -> EitherT<Option<Result<B, E>>>,
    {
        EitherT::new(match self.inner {
            Some(Ok(a)) => f(a).inner,
            Some(Err(e)) => Some(Err(e)),
            None => None,
        })
    }

    /// Applies a wrapped function to this value.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let val: EitherT<Option<Result<i32, &str>>> = EitherT::right(21);
    /// let func: EitherT<Option<Result<fn(i32) -> i32, &str>>> =
    ///     EitherT::right(|x: i32| x * 2);
    /// let result = val.apply(func);
    /// assert_eq!(result.run(), Some(Ok(42)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn apply<B, F>(self, f: EitherT<Option<Result<F, E>>>) -> EitherT<Option<Result<B, E>>>
    where
        F: FnOnce(A) -> B,
    {
        EitherT::new(match (self.inner, f.inner) {
            (Some(Ok(a)), Some(Ok(func))) => Some(Ok(func(a))),
            (Some(Err(e)), _) | (_, Some(Err(e))) => Some(Err(e)),
            (None, _) | (_, None) => None,
        })
    }

    /// Combines two `EitherT` values using a function.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let a: EitherT<Option<Result<i32, &str>>> = EitherT::right(10);
    /// let b: EitherT<Option<Result<i32, &str>>> = EitherT::right(20);
    /// let combined = a.map2(b, |x, y| x + y);
    /// assert_eq!(combined.run(), Some(Ok(30)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn map2<B, C, F>(
        self,
        other: EitherT<Option<Result<B, E>>>,
        f: F,
    ) -> EitherT<Option<Result<C, E>>>
    where
        F: FnOnce(A, B) -> C,
    {
        EitherT::new(match (self.inner, other.inner) {
            (Some(Ok(a)), Some(Ok(b))) => Some(Ok(f(a, b))),
            (Some(Err(e)), _) | (_, Some(Err(e))) => Some(Err(e)),
            (None, _) | (_, None) => None,
        })
    }

    /// Handles the error case, potentially recovering.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "alloc")]
    /// # fn main() {
    /// use ordofp_core::transformers::EitherT;
    ///
    /// let err: EitherT<Option<Result<i32, &str>>> = EitherT::left("error");
    /// let recovered = err.handle_error(|_| EitherT::right(0));
    /// assert_eq!(recovered.run(), Some(Ok(0)));
    /// # }
    /// # #[cfg(not(feature = "alloc"))]
    /// # fn main() {}
    /// ```
    #[inline]
    pub fn handle_error<F>(self, f: F) -> Self
    where
        F: FnOnce(E) -> Self,
    {
        EitherT::new(match self.inner {
            Some(Ok(a)) => Some(Ok(a)),
            Some(Err(e)) => f(e).inner,
            None => None,
        })
    }

    /// Converts the error type using a function.
    ///
    /// Alias for `map_left`.
    #[inline]
    pub fn with_error<E2, F>(self, f: F) -> EitherT<Option<Result<A, E2>>>
    where
        F: FnOnce(E) -> E2,
    {
        self.map_left(f)
    }

    /// Checks if the computation is a success.
    #[inline]
    pub fn is_right(&self) -> bool {
        matches!(&self.inner, Some(Ok(_)))
    }

    /// Checks if the computation is an error.
    #[inline]
    pub fn is_left(&self) -> bool {
        matches!(&self.inner, Some(Err(_)))
    }

    /// Checks if the computation is absent.
    #[inline]
    pub fn is_absent(&self) -> bool {
        self.inner.is_none()
    }
}

impl<A, E> MonadTransformer for EitherT<Option<Result<A, E>>> {
    type BaseMonad = Option<A>;

    #[inline]
    fn lift(base: Option<A>) -> Self {
        EitherT::lift_m(base)
    }
}

// ============================================================================
// EitherT over Result (Result<Result<A, E1>, E2>)
// ============================================================================

impl<A, E1, E2> EitherT<Result<Result<A, E1>, E2>> {
    /// Creates an `EitherT` containing a success value over `Result`.
    #[inline]
    pub fn right_result(value: A) -> Self {
        EitherT::new(Ok(Ok(value)))
    }

    /// Creates an `EitherT` containing an inner error over `Result`.
    #[inline]
    pub fn left_result(error: E1) -> Self {
        EitherT::new(Ok(Err(error)))
    }

    /// Creates an `EitherT` containing an outer error over `Result`.
    #[inline]
    pub fn outer_err(error: E2) -> Self {
        EitherT::new(Err(error))
    }

    /// Maps a function over the success value.
    #[inline]
    pub fn map_result<B, F>(self, f: F) -> EitherT<Result<Result<B, E1>, E2>>
    where
        F: FnOnce(A) -> B,
    {
        EitherT::new(self.inner.map(|res| res.map(f)))
    }

    /// Chains a computation that returns an `EitherT`.
    #[inline]
    pub fn flat_map_result<B, F>(self, f: F) -> EitherT<Result<Result<B, E1>, E2>>
    where
        F: FnOnce(A) -> EitherT<Result<Result<B, E1>, E2>>,
    {
        EitherT::new(match self.inner {
            Ok(Ok(a)) => f(a).inner,
            Ok(Err(e)) => Ok(Err(e)),
            Err(e) => Err(e),
        })
    }
}

// ============================================================================
// EitherT over Vec
// ============================================================================

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "alloc")]
impl<A, E> EitherT<Vec<Result<A, E>>> {
    /// Creates an `EitherT` containing a success value over `Vec`.
    #[inline]
    pub fn right_vec(value: A) -> Self {
        EitherT::new(alloc::vec![Ok(value)])
    }

    /// Creates an `EitherT` containing an error value over `Vec`.
    #[inline]
    pub fn left_vec(error: E) -> Self {
        EitherT::new(alloc::vec![Err(error)])
    }

    /// Creates an `EitherT` from multiple results.
    #[inline]
    pub fn from_vec(values: Vec<Result<A, E>>) -> Self {
        EitherT::new(values)
    }

    /// Maps a function over all success values.
    #[inline]
    pub fn map_vec<B, F>(self, mut f: F) -> EitherT<Vec<Result<B, E>>>
    where
        F: FnMut(A) -> B,
    {
        EitherT::new(self.inner.into_iter().map(|res| res.map(&mut f)).collect())
    }

    /// Chains a computation that returns an `EitherT`.
    #[inline]
    pub fn flat_map_vec<B, F>(self, mut f: F) -> EitherT<Vec<Result<B, E>>>
    where
        F: FnMut(A) -> EitherT<Vec<Result<B, E>>>,
    {
        let results: Vec<Result<B, E>> = self
            .inner
            .into_iter()
            .flat_map(|res| match res {
                Ok(a) => f(a).inner,
                Err(e) => alloc::vec![Err(e)],
            })
            .collect();
        EitherT::new(results)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_either_t_right() {
        let ok: EitherT<Option<Result<i32, &str>>> = EitherT::right(42);
        assert_eq!(ok.run(), Some(Ok(42)));
    }

    #[test]
    fn test_either_t_left() {
        let err: EitherT<Option<Result<i32, &str>>> = EitherT::left("error");
        assert_eq!(err.run(), Some(Err("error")));
    }

    #[test]
    fn test_either_t_absent() {
        let absent: EitherT<Option<Result<i32, &str>>> = EitherT::absent();
        assert_eq!(absent.run(), None);
    }

    #[test]
    fn test_either_t_map() {
        let ok: EitherT<Option<Result<i32, &str>>> = EitherT::right(21);
        let doubled = ok.map(|x| x * 2);
        assert_eq!(doubled.run(), Some(Ok(42)));

        let err: EitherT<Option<Result<i32, &str>>> = EitherT::left("error");
        let err_mapped = err.map(|x| x * 2);
        assert_eq!(err_mapped.run(), Some(Err("error")));
    }

    #[test]
    fn test_either_t_flat_map() {
        let ok: EitherT<Option<Result<i32, &str>>> = EitherT::right(10);
        let result = ok.flat_map(|x| {
            if x > 5 {
                EitherT::right(x * 2)
            } else {
                EitherT::left("too small")
            }
        });
        assert_eq!(result.run(), Some(Ok(20)));

        let ok2: EitherT<Option<Result<i32, &str>>> = EitherT::right(3);
        let result2 = ok2.flat_map(|x| {
            if x > 5 {
                EitherT::right(x * 2)
            } else {
                EitherT::left("too small")
            }
        });
        assert_eq!(result2.run(), Some(Err("too small")));
    }

    #[test]
    #[allow(clippy::type_complexity)] // spelled-out nested transformer type is the point
    fn test_either_t_apply() {
        let val: EitherT<Option<Result<i32, &str>>> = EitherT::right(21);
        let func: EitherT<Option<Result<fn(i32) -> i32, &str>>> = EitherT::right(|x: i32| x * 2);
        let result = val.apply(func);
        assert_eq!(result.run(), Some(Ok(42)));
    }

    #[test]
    fn test_either_t_lift_m() {
        let some: Option<i32> = Some(42);
        let lifted: EitherT<Option<Result<i32, &str>>> = EitherT::lift_m(some);
        assert_eq!(lifted.run(), Some(Ok(42)));

        let none: Option<i32> = None;
        let lifted_none: EitherT<Option<Result<i32, &str>>> = EitherT::lift_m(none);
        assert_eq!(lifted_none.run(), None);
    }

    #[test]
    fn test_either_t_handle_error() {
        let err: EitherT<Option<Result<i32, &str>>> = EitherT::left("error");
        let recovered = err.handle_error(|_| EitherT::right(0));
        assert_eq!(recovered.run(), Some(Ok(0)));
    }

    // Monad law tests
    #[test]
    fn test_either_t_left_identity() {
        // pure(a).flat_map(f) == f(a)
        let a = 5;
        let f = |x: i32| EitherT::<Option<Result<i32, &str>>>::right(x * 2);

        let left = EitherT::<Option<Result<i32, &str>>>::right(a).flat_map(f);
        let right = f(a);
        assert_eq!(left.run(), right.run());
    }

    #[test]
    fn test_either_t_right_identity() {
        // m.flat_map(pure) == m
        let m: EitherT<Option<Result<i32, &str>>> = EitherT::right(42);
        let result = m.flat_map(EitherT::right);
        assert_eq!(result.run(), Some(Ok(42)));
    }

    #[test]
    fn test_either_t_associativity() {
        // m.flat_map(f).flat_map(g) == m.flat_map(|x| f(x).flat_map(g))
        let m: EitherT<Option<Result<i32, &str>>> = EitherT::right(5);
        let f = |x: i32| EitherT::<Option<Result<i32, &str>>>::right(x + 1);
        let g = |x: i32| EitherT::<Option<Result<i32, &str>>>::right(x * 2);

        let left = m.flat_map(f).flat_map(g);
        let right = EitherT::<Option<Result<i32, &str>>>::right(5).flat_map(|x| f(x).flat_map(g));
        assert_eq!(left.run(), right.run());
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_either_t_vec() {
        let ok: EitherT<Vec<Result<i32, &str>>> = EitherT::right_vec(42);
        assert_eq!(ok.run(), alloc::vec![Ok(42)]);

        let mapped: EitherT<Vec<Result<i32, &str>>> = EitherT::right_vec(21);
        let result = mapped.map_vec(|x| x * 2);
        assert_eq!(result.run(), alloc::vec![Ok(42)]);
    }
}