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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use futures::{Async, Poll};

use actor::Actor;
use fut::ActorFuture;

/// Future for the `map_err` combinator, changing the error type of a future.
///
/// This is created by the `Future::map_err` method.
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct MapErr<A, F>
where
    A: ActorFuture,
{
    future: A,
    f: Option<F>,
}

pub fn new<A, F>(future: A, f: F) -> MapErr<A, F>
where
    A: ActorFuture,
{
    MapErr { future, f: Some(f) }
}

impl<U, A, F> ActorFuture for MapErr<A, F>
where
    A: ActorFuture,
    F: FnOnce(A::Error, &mut A::Actor, &mut <A::Actor as Actor>::Context) -> U,
{
    type Item = A::Item;
    type Error = U;
    type Actor = A::Actor;

    fn poll(
        &mut self, act: &mut A::Actor, ctx: &mut <A::Actor as Actor>::Context,
    ) -> Poll<A::Item, U> {
        let e = match self.future.poll(act, ctx) {
            Ok(Async::NotReady) => return Ok(Async::NotReady),
            other => other,
        };
        match e {
            Err(e) => Err(self.f.take().expect("cannot poll MapErr twice")(
                e, act, ctx,
            )),
            Ok(err) => Ok(err),
        }
    }
}

pub struct DropErr<A>
where
    A: ActorFuture,
{
    future: A,
}

impl<A> DropErr<A>
where
    A: ActorFuture,
{
    pub(crate) fn new(future: A) -> DropErr<A> {
        DropErr { future }
    }
}

impl<A> ActorFuture for DropErr<A>
where
    A: ActorFuture,
{
    type Item = A::Item;
    type Error = ();
    type Actor = A::Actor;

    fn poll(
        &mut self, act: &mut A::Actor, ctx: &mut <A::Actor as Actor>::Context,
    ) -> Poll<A::Item, ()> {
        match self.future.poll(act, ctx) {
            Ok(Async::Ready(item)) => Ok(Async::Ready(item)),
            Ok(Async::NotReady) => Ok(Async::NotReady),
            Err(_) => Err(()),
        }
    }
}