sift_trait/
lib.rs

1#![no_std]
2
3use either::Either;
4
5pub trait Sift<T> {
6    type Residue;
7    fn sift(self) -> Result<T, Self::Residue>;
8    fn of(t: T) -> Self;
9    fn lift(r: Self::Residue) -> Self;
10}
11impl<T, U, A: Sift<T, Residue: Sift<U>>> Sift<Either<T, U>> for A {
12    type Residue = <<A as Sift<T>>::Residue as Sift<U>>::Residue;
13
14    fn sift(self) -> Result<Either<T, U>, Self::Residue> {
15        match self.sift() {
16            Ok(a) => Ok(Either::Left(a)),
17            Err(b) => b.sift().map(Either::Right),
18        }
19    }
20
21    fn of(t: Either<T, U>) -> Self {
22        match t {
23            Either::Left(a) => <A as Sift<T>>::of(a),
24            Either::Right(b) => <A as Sift<T>>::lift(<<A as Sift<T>>::Residue as Sift<U>>::of(b)),
25        }
26    }
27
28    fn lift(r: Self::Residue) -> Self {
29        <A as Sift<T>>::lift(<<A as Sift<T>>::Residue as Sift<U>>::lift(r))
30    }
31}