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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::error::Error as StdError;
use std::fmt;
use futures::{Async, Future, IntoFuture, Poll};
use body::Payload;
use super::Service;
pub trait MakeService<Ctx> {
type ReqBody: Payload;
type ResBody: Payload;
type Error: Into<Box<dyn StdError + Send + Sync>>;
type Service: Service<
ReqBody=Self::ReqBody,
ResBody=Self::ResBody,
Error=Self::Error,
>;
type Future: Future<Item=Self::Service, Error=Self::MakeError>;
type MakeError: Into<Box<dyn StdError + Send + Sync>>;
fn poll_ready(&mut self) -> Poll<(), Self::MakeError> {
Ok(Async::Ready(()))
}
fn make_service(&mut self, ctx: Ctx) -> Self::Future;
}
#[doc(hidden)]
pub trait MakeServiceRef<Ctx>: self::sealed::Sealed<Ctx> {
type ReqBody: Payload;
type ResBody: Payload;
type Error: Into<Box<dyn StdError + Send + Sync>>;
type Service: Service<
ReqBody=Self::ReqBody,
ResBody=Self::ResBody,
Error=Self::Error,
>;
type MakeError: Into<Box<dyn StdError + Send + Sync>>;
type Future: Future<Item=Self::Service, Error=Self::MakeError>;
type __DontNameMe: self::sealed::CantImpl;
fn poll_ready_ref(&mut self) -> Poll<(), Self::MakeError>;
fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future;
}
impl<T, Ctx, E, ME, S, F, IB, OB> MakeServiceRef<Ctx> for T
where
T: for<'a> MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
E: Into<Box<dyn StdError + Send + Sync>>,
ME: Into<Box<dyn StdError + Send + Sync>>,
S: Service<ReqBody=IB, ResBody=OB, Error=E>,
F: Future<Item=S, Error=ME>,
IB: Payload,
OB: Payload,
{
type Error = E;
type Service = S;
type ReqBody = IB;
type ResBody = OB;
type MakeError = ME;
type Future = F;
type __DontNameMe = self::sealed::CantName;
fn poll_ready_ref(&mut self) -> Poll<(), Self::MakeError> {
self.poll_ready()
}
fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future {
self.make_service(ctx)
}
}
impl<T, Ctx, E, ME, S, F, IB, OB> self::sealed::Sealed<Ctx> for T
where
T: for<'a> MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
E: Into<Box<dyn StdError + Send + Sync>>,
ME: Into<Box<dyn StdError + Send + Sync>>,
S: Service<ReqBody=IB, ResBody=OB, Error=E>,
F: Future<Item=S, Error=ME>,
IB: Payload,
OB: Payload,
{}
pub fn make_service_fn<F, Ctx, Ret>(f: F) -> MakeServiceFn<F>
where
F: FnMut(&Ctx) -> Ret,
Ret: IntoFuture,
{
MakeServiceFn {
f,
}
}
pub struct MakeServiceFn<F> {
f: F,
}
impl<'c, F, Ctx, Ret, ReqBody, ResBody> MakeService<&'c Ctx> for MakeServiceFn<F>
where
F: FnMut(&Ctx) -> Ret,
Ret: IntoFuture,
Ret::Item: Service<ReqBody=ReqBody, ResBody=ResBody>,
Ret::Error: Into<Box<dyn StdError + Send + Sync>>,
ReqBody: Payload,
ResBody: Payload,
{
type ReqBody = ReqBody;
type ResBody = ResBody;
type Error = <Ret::Item as Service>::Error;
type Service = Ret::Item;
type Future = Ret::Future;
type MakeError = Ret::Error;
fn make_service(&mut self, ctx: &'c Ctx) -> Self::Future {
(self.f)(ctx).into_future()
}
}
impl<F> fmt::Debug for MakeServiceFn<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MakeServiceFn")
.finish()
}
}
mod sealed {
pub trait Sealed<T> {}
pub trait CantImpl {}
#[allow(missing_debug_implementations)]
pub enum CantName {}
impl CantImpl for CantName {}
}