powerset_enum_traits/
lib.rs

1pub trait WithVariant<T> {
2    type With;
3    fn add_possibility(self) -> Self::With;
4}
5
6pub trait WithoutVariant<V> {
7    type Without;
8    fn remove_possibility(self) -> Result<Self::Without, V>;
9}
10
11impl<T, E, V> WithoutVariant<V> for Result<T, E>
12where E: WithoutVariant<V>,
13{
14    type Without = Result<T, <E as WithoutVariant<V>>::Without>;
15    fn remove_possibility(self) -> Result<Self::Without, V> {
16        match self {
17            Ok(ok) => Ok(Ok(ok)),
18            Err(err) => match err.remove_possibility() {
19                Ok(remaining_err) => Ok(Err(remaining_err)),
20                Err(err) => Err(err),
21            },
22        }
23    }
24}
25
26pub trait Extract {
27    fn extract<V>(self) -> Result<<Self as WithoutVariant<V>>::Without, V>
28    where Self: WithoutVariant<V>;
29}
30
31impl<T> Extract for T {
32    fn extract<V>(self) -> Result<<Self as WithoutVariant<V>>::Without, V>
33    where T: WithoutVariant<V>
34    {
35        self.remove_possibility()
36    }
37}