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
85
86
87
88
89
90
91
92
93
94
95
use core::{
    future::Future,
    marker::PhantomData,
    pin::Pin,
    task::{Context, Poll},
};

use pin_project_lite::pin_project;

use super::Transform;

/// Transform for the [`TransformExt::map_init_err`] combinator, changing the type of a new
/// [`Transform`]'s initialization error.
pub struct TransformMapInitErr<T, S, Req, F, E> {
    transform: T,
    mapper: F,
    _phantom: PhantomData<fn(Req) -> (S, E)>,
}

impl<T, S, F, E, Req> TransformMapInitErr<T, S, Req, F, E> {
    pub(crate) fn new(t: T, f: F) -> Self
    where
        T: Transform<S, Req>,
        F: Fn(T::InitError) -> E,
    {
        Self {
            transform: t,
            mapper: f,
            _phantom: PhantomData,
        }
    }
}

impl<T, S, Req, F, E> Clone for TransformMapInitErr<T, S, Req, F, E>
where
    T: Clone,
    F: Clone,
{
    fn clone(&self) -> Self {
        Self {
            transform: self.transform.clone(),
            mapper: self.mapper.clone(),
            _phantom: PhantomData,
        }
    }
}

impl<T, S, F, E, Req> Transform<S, Req> for TransformMapInitErr<T, S, Req, F, E>
where
    T: Transform<S, Req>,
    F: Fn(T::InitError) -> E + Clone,
{
    type Response = T::Response;
    type Error = T::Error;
    type Transform = T::Transform;

    type InitError = E;
    type Future = TransformMapInitErrFuture<T, S, F, E, Req>;

    fn new_transform(&self, service: S) -> Self::Future {
        TransformMapInitErrFuture {
            fut: self.transform.new_transform(service),
            f: self.mapper.clone(),
        }
    }
}

pin_project! {
    pub struct TransformMapInitErrFuture<T, S, F, E, Req>
    where
    T: Transform<S, Req>,
    F: Fn(T::InitError) -> E,
    {
        #[pin]
        fut: T::Future,
        f: F,
    }
}

impl<T, S, F, E, Req> Future for TransformMapInitErrFuture<T, S, F, E, Req>
where
    T: Transform<S, Req>,
    F: Fn(T::InitError) -> E + Clone,
{
    type Output = Result<T::Transform, E>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        if let Poll::Ready(res) = this.fut.poll(cx) {
            Poll::Ready(res.map_err(this.f))
        } else {
            Poll::Pending
        }
    }
}