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 super::Middleware;
use futures::{Future, Poll};
use std::sync::Arc;
use tower_service::NewService;
#[derive(Debug)]
pub struct MiddlewareChain<S, M> {
new_service: S,
middleware: std::sync::Arc<M>,
}
impl<S, M> MiddlewareChain<S, M>
where
S: NewService,
M: Middleware<S::Service>,
{
pub(crate) fn new(new_service: S, middleware: M) -> Self {
MiddlewareChain {
new_service,
middleware: Arc::new(middleware),
}
}
}
impl<S, M> NewService for MiddlewareChain<S, M>
where
S: NewService,
M: Middleware<S::Service>,
{
type Request = M::Request;
type Response = M::Response;
type Error = M::Error;
type Service = M::Service;
type InitError = S::InitError;
type Future = MiddlewareChainFuture<S::Future, M>;
fn new_service(&self) -> Self::Future {
MiddlewareChainFuture {
future: self.new_service.new_service(),
middleware: self.middleware.clone(),
}
}
}
#[allow(missing_debug_implementations)]
pub struct MiddlewareChainFuture<F, M> {
future: F,
middleware: std::sync::Arc<M>,
}
impl<F, M> Future for MiddlewareChainFuture<F, M>
where
F: Future,
M: Middleware<F::Item>,
{
type Item = M::Service;
type Error = F::Error;
#[inline]
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.future
.poll()
.map(|x| x.map(|service| self.middleware.wrap(service)))
}
}