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
use futures::Future;
use crate::{layer::Layer, service::Service};
#[derive(Clone, Debug)]
pub enum Either<A, B> {
A(A),
B(B),
}
impl<S, A, B> Layer<S> for Either<A, B>
where
A: Layer<S>,
B: Layer<S>,
{
type Service = Either<A::Service, B::Service>;
fn layer(self, inner: S) -> Self::Service {
match self {
Either::A(layer) => Either::A(layer.layer(inner)),
Either::B(layer) => Either::B(layer.layer(inner)),
}
}
}
impl<A, B, Cx, Req> Service<Cx, Req> for Either<A, B>
where
Req: 'static + Send,
Cx: Send + 'static,
A: Service<Cx, Req> + Send + 'static + Sync,
B: Service<Cx, Req, Response = A::Response, Error = A::Error> + Send + 'static + Sync,
{
type Response = A::Response;
type Error = A::Error;
type Future<'cx> = impl Future<Output = Result<Self::Response, Self::Error>> + 'cx
where
Cx: 'cx,
Self: 'cx;
fn call<'cx, 's>(&'s self, cx: &'cx mut Cx, req: Req) -> Self::Future<'cx>
where
's: 'cx,
{
async move {
match self {
Either::A(s) => s.call(cx, req).await,
Either::B(s) => s.call(cx, req).await,
}
}
}
}