1
- use crate :: Status ;
2
- use http:: Response ;
3
- use http_body:: Frame ;
4
- use pin_project:: pin_project;
5
1
use std:: {
6
2
future:: Future ,
7
3
pin:: Pin ,
8
4
task:: { ready, Context , Poll } ,
9
5
} ;
10
- use tower:: Service ;
6
+
7
+ use http:: Response ;
8
+ use pin_project:: pin_project;
9
+ use tower_service:: Service ;
10
+
11
+ use crate :: Status ;
11
12
12
13
/// Middleware that attempts to recover from service errors by turning them into a response built
13
14
/// from the `Status`.
@@ -22,20 +23,20 @@ impl<S> RecoverError<S> {
22
23
}
23
24
}
24
25
25
- impl < S , R , ResBody > Service < R > for RecoverError < S >
26
+ impl < S , Req , ResBody > Service < Req > for RecoverError < S >
26
27
where
27
- S : Service < R , Response = Response < ResBody > > ,
28
+ S : Service < Req , Response = Response < ResBody > > ,
28
29
S :: Error : Into < crate :: BoxError > ,
29
30
{
30
- type Response = Response < MaybeEmptyBody < ResBody > > ;
31
+ type Response = Response < ResponseBody < ResBody > > ;
31
32
type Error = crate :: BoxError ;
32
33
type Future = ResponseFuture < S :: Future > ;
33
34
34
35
fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
35
36
self . inner . poll_ready ( cx) . map_err ( Into :: into)
36
37
}
37
38
38
- fn call ( & mut self , req : R ) -> Self :: Future {
39
+ fn call ( & mut self , req : Req ) -> Self :: Future {
39
40
ResponseFuture {
40
41
inner : self . inner . call ( req) ,
41
42
}
@@ -53,21 +54,18 @@ where
53
54
F : Future < Output = Result < Response < ResBody > , E > > ,
54
55
E : Into < crate :: BoxError > ,
55
56
{
56
- type Output = Result < Response < MaybeEmptyBody < ResBody > > , crate :: BoxError > ;
57
+ type Output = Result < Response < ResponseBody < ResBody > > , crate :: BoxError > ;
57
58
58
59
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
59
- let result: Result < Response < _ > , crate :: BoxError > =
60
- ready ! ( self . project( ) . inner. poll( cx) ) . map_err ( Into :: into) ;
61
-
62
- match result {
60
+ match ready ! ( self . project( ) . inner. poll( cx) ) {
63
61
Ok ( response) => {
64
- let response = response. map ( MaybeEmptyBody :: full) ;
62
+ let response = response. map ( ResponseBody :: full) ;
65
63
Poll :: Ready ( Ok ( response) )
66
64
}
67
- Err ( err) => match Status :: try_from_error ( err) {
65
+ Err ( err) => match Status :: try_from_error ( err. into ( ) ) {
68
66
Ok ( status) => {
69
67
let ( parts, ( ) ) = status. into_http :: < ( ) > ( ) . into_parts ( ) ;
70
- let res = Response :: from_parts ( parts, MaybeEmptyBody :: empty ( ) ) ;
68
+ let res = Response :: from_parts ( parts, ResponseBody :: empty ( ) ) ;
71
69
Poll :: Ready ( Ok ( res) )
72
70
}
73
71
Err ( err) => Poll :: Ready ( Err ( err) ) ,
@@ -77,22 +75,22 @@ where
77
75
}
78
76
79
77
#[ pin_project]
80
- pub ( crate ) struct MaybeEmptyBody < B > {
78
+ pub ( crate ) struct ResponseBody < B > {
81
79
#[ pin]
82
80
inner : Option < B > ,
83
81
}
84
82
85
- impl < B > MaybeEmptyBody < B > {
83
+ impl < B > ResponseBody < B > {
86
84
fn full ( inner : B ) -> Self {
87
85
Self { inner : Some ( inner) }
88
86
}
89
87
90
- fn empty ( ) -> Self {
88
+ const fn empty ( ) -> Self {
91
89
Self { inner : None }
92
90
}
93
91
}
94
92
95
- impl < B > http_body:: Body for MaybeEmptyBody < B >
93
+ impl < B > http_body:: Body for ResponseBody < B >
96
94
where
97
95
B : http_body:: Body ,
98
96
{
@@ -102,7 +100,7 @@ where
102
100
fn poll_frame (
103
101
self : Pin < & mut Self > ,
104
102
cx : & mut Context < ' _ > ,
105
- ) -> Poll < Option < Result < Frame < Self :: Data > , Self :: Error > > > {
103
+ ) -> Poll < Option < Result < http_body :: Frame < Self :: Data > , Self :: Error > > > {
106
104
match self . project ( ) . inner . as_pin_mut ( ) {
107
105
Some ( b) => b. poll_frame ( cx) ,
108
106
None => Poll :: Ready ( None ) ,
0 commit comments