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
use std::{
    pin::Pin,
    task::{Context, Poll},
};

use futures_core::ready;
use pin_project_lite::pin_project;

use crate::{
    fut::{future::ActorFuture, try_future::ActorTryFuture},
    Actor,
};

pin_project! {
    /// Future for the [`map`](super::ActorTryFutureExt::map_err) method.
    #[project = MapProj]
    #[project_replace = MapProjReplace]
    #[derive(Debug)]
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub enum MapErr<Fut, F> {
        Incomplete {
            #[pin]
            future: Fut,
            f: F,
        },
        Complete,
    }
}

impl<Fut, F> MapErr<Fut, F> {
    pub(crate) fn new(future: Fut, f: F) -> Self {
        Self::Incomplete { future, f }
    }
}

impl<U, Fut, A, F> ActorFuture<A> for MapErr<Fut, F>
where
    Fut: ActorTryFuture<A>,
    A: Actor,
    F: FnOnce(Fut::Error, &mut A, &mut A::Context) -> U,
{
    type Output = Result<Fut::Ok, U>;

    fn poll(
        mut self: Pin<&mut Self>,
        act: &mut A,
        ctx: &mut A::Context,
        task: &mut Context<'_>,
    ) -> Poll<Self::Output> {
        match self.as_mut().project() {
            MapProj::Incomplete { future, .. } => {
                let output = ready!(future.try_poll(act, ctx, task));
                match self.project_replace(MapErr::Complete) {
                    MapProjReplace::Incomplete { f, .. } => {
                        Poll::Ready(output.map_err(|err| f(err, act, ctx)))
                    }
                    MapProjReplace::Complete => unreachable!(),
                }
            }
            MapProj::Complete => {
                panic!("MapErr must not be polled after it returned `Poll::Ready`")
            }
        }
    }
}