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
pub trait TupleMapperFirst<Input, Output, Result>
where
    Self: Sized,
{
    fn map_first(self, mapper: impl Fn(Input) -> Output) -> Result;
}

pub trait TupleMapperSecond<Input, Output, Result>
where
    Self: Sized,
{
    fn map_second(self, mapper: impl FnOnce(Input) -> Output) -> Result;
}

impl<S, E, I, O> TupleMapperFirst<I, O, Result<(O, S), E>> for Result<(I, S), E> {
    fn map_first(self, mapper: impl Fn(I) -> O) -> Result<(O, S), E> {
        self.map(|(first, second)| (mapper(first), second))
    }
}

impl<S, E, I, O> TupleMapperSecond<I, O, Result<(S, O), E>> for Result<(S, I), E> {
    fn map_second(self, mapper: impl FnOnce(I) -> O) -> Result<(S, O), E> {
        self.map(|(first, second)| (first, mapper(second)))
    }
}