@@ -27,19 +27,15 @@ where
27
27
/// Transform(middleware) wraps inner service and runs during inbound and/or outbound processing in
28
28
/// the request/response lifecycle. It may modify request and/or response.
29
29
///
30
- /// For example, timeout transform :
30
+ /// For example, a timeout service wrapper :
31
31
///
32
32
/// ```ignore
33
33
/// pub struct Timeout<S> {
34
34
/// service: S,
35
35
/// timeout: Duration,
36
36
/// }
37
37
///
38
- /// impl<S> Service for Timeout<S>
39
- /// where
40
- /// S: Service,
41
- /// {
42
- /// type Request = S::Request;
38
+ /// impl<S: Service<Req>, Req> Service<Req> for Timeout<S> {
43
39
/// type Response = S::Response;
44
40
/// type Error = TimeoutError<S::Error>;
45
41
/// type Future = TimeoutServiceResponse<S>;
@@ -55,34 +51,30 @@ where
55
51
/// }
56
52
/// ```
57
53
///
58
- /// Timeout service in above example is decoupled from underlying service implementation and could
59
- /// be applied to any service.
54
+ /// This wrapper service is decoupled from the underlying service implementation and could be
55
+ /// applied to any service.
60
56
///
61
- /// The `Transform` trait defines the interface of a Service factory . `Transform` is often
57
+ /// The `Transform` trait defines the interface of a service wrapper . `Transform` is often
62
58
/// implemented for middleware, defining how to construct a middleware Service. A Service that is
63
59
/// constructed by the factory takes the Service that follows it during execution as a parameter,
64
60
/// assuming ownership of the next Service.
65
61
///
66
- /// Factory for `Timeout` middleware from the above example could look like this:
62
+ /// A transform for the `Timeout` middleware could look like this:
67
63
///
68
64
/// ```ignore
69
65
/// pub struct TimeoutTransform {
70
66
/// timeout: Duration,
71
67
/// }
72
68
///
73
- /// impl<S> Transform<S> for TimeoutTransform
74
- /// where
75
- /// S: Service,
76
- /// {
77
- /// type Request = S::Request;
69
+ /// impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
78
70
/// type Response = S::Response;
79
71
/// type Error = TimeoutError<S::Error>;
80
72
/// type InitError = S::Error;
81
73
/// type Transform = Timeout<S>;
82
74
/// type Future = Ready<Result<Self::Transform, Self::InitError>>;
83
75
///
84
76
/// fn new_transform(&self, service: S) -> Self::Future {
85
- /// ready(Ok(TimeoutService {
77
+ /// ready(Ok(Timeout {
86
78
/// service,
87
79
/// timeout: self.timeout,
88
80
/// }))
@@ -227,3 +219,54 @@ where
227
219
}
228
220
}
229
221
}
222
+
223
+ #[ cfg( test) ]
224
+ mod tests {
225
+ use core:: {
226
+ future:: { ready, Ready } ,
227
+ time:: Duration ,
228
+ } ;
229
+
230
+ use super :: * ;
231
+ use crate :: Service ;
232
+
233
+ // pseudo-doctest for Transform trait
234
+ pub struct TimeoutTransform {
235
+ timeout : Duration ,
236
+ }
237
+
238
+ // pseudo-doctest for Transform trait
239
+ impl < S : Service < Req > , Req > Transform < S , Req > for TimeoutTransform {
240
+ type Response = S :: Response ;
241
+ type Error = S :: Error ;
242
+ type InitError = S :: Error ;
243
+ type Transform = Timeout < S > ;
244
+ type Future = Ready < Result < Self :: Transform , Self :: InitError > > ;
245
+
246
+ fn new_transform ( & self , service : S ) -> Self :: Future {
247
+ ready ( Ok ( Timeout {
248
+ service,
249
+ _timeout : self . timeout ,
250
+ } ) )
251
+ }
252
+ }
253
+
254
+ // pseudo-doctest for Transform trait
255
+ pub struct Timeout < S > {
256
+ service : S ,
257
+ _timeout : Duration ,
258
+ }
259
+
260
+ // pseudo-doctest for Transform trait
261
+ impl < S : Service < Req > , Req > Service < Req > for Timeout < S > {
262
+ type Response = S :: Response ;
263
+ type Error = S :: Error ;
264
+ type Future = S :: Future ;
265
+
266
+ crate :: forward_ready!( service) ;
267
+
268
+ fn call ( & self , req : Req ) -> Self :: Future {
269
+ self . service . call ( req)
270
+ }
271
+ }
272
+ }
0 commit comments