requiem_service/
map_init_err.rs

1use std::future::Future;
2use std::marker::PhantomData;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6use super::ServiceFactory;
7
8/// `MapInitErr` service combinator
9pub struct MapInitErr<A, F, E> {
10    a: A,
11    f: F,
12    e: PhantomData<E>,
13}
14
15impl<A, F, E> MapInitErr<A, F, E>
16where
17    A: ServiceFactory,
18    F: Fn(A::InitError) -> E,
19{
20    /// Create new `MapInitErr` combinator
21    pub(crate) fn new(a: A, f: F) -> Self {
22        Self {
23            a,
24            f,
25            e: PhantomData,
26        }
27    }
28}
29
30impl<A, F, E> Clone for MapInitErr<A, F, E>
31where
32    A: Clone,
33    F: Clone,
34{
35    fn clone(&self) -> Self {
36        Self {
37            a: self.a.clone(),
38            f: self.f.clone(),
39            e: PhantomData,
40        }
41    }
42}
43
44impl<A, F, E> ServiceFactory for MapInitErr<A, F, E>
45where
46    A: ServiceFactory,
47    F: Fn(A::InitError) -> E + Clone,
48{
49    type Request = A::Request;
50    type Response = A::Response;
51    type Error = A::Error;
52
53    type Config = A::Config;
54    type Service = A::Service;
55    type InitError = E;
56    type Future = MapInitErrFuture<A, F, E>;
57
58    fn new_service(&self, cfg: A::Config) -> Self::Future {
59        MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
60    }
61}
62
63#[pin_project::pin_project]
64pub struct MapInitErrFuture<A, F, E>
65where
66    A: ServiceFactory,
67    F: Fn(A::InitError) -> E,
68{
69    f: F,
70    #[pin]
71    fut: A::Future,
72}
73
74impl<A, F, E> MapInitErrFuture<A, F, E>
75where
76    A: ServiceFactory,
77    F: Fn(A::InitError) -> E,
78{
79    fn new(fut: A::Future, f: F) -> Self {
80        MapInitErrFuture { f, fut }
81    }
82}
83
84impl<A, F, E> Future for MapInitErrFuture<A, F, E>
85where
86    A: ServiceFactory,
87    F: Fn(A::InitError) -> E,
88{
89    type Output = Result<A::Service, E>;
90
91    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
92        let this = self.project();
93        this.fut.poll(cx).map_err(this.f)
94    }
95}