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
use crate::AsyncOperator;
use futures::TryFutureExt;

/// Convert Error.
#[derive(Debug, Clone, Copy)]
pub struct MapErr<F, P> {
    pub(super) inner: P,
    pub(super) f: F,
}

impl<I, E, F, P> AsyncOperator<I> for MapErr<F, P>
where
    P: AsyncOperator<I>,
    F: FnMut(P::Error) -> E,
{
    type Output = P::Output;
    type Error = E;
    type Future<'a> = futures::future::MapErr<P::Future<'a>, &'a mut F> where P: 'a, F: 'a;

    fn poll_ready(
        &mut self,
        cx: &mut core::task::Context<'_>,
    ) -> core::task::Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx).map_err(&mut self.f)
    }

    fn next(&mut self, input: I) -> Self::Future<'_> {
        self.inner.next(input).map_err(&mut self.f)
    }
}