@@ -67,6 +67,8 @@ extern crate maplit;
6767pub use http:: { self , Response } ;
6868use lambda_runtime:: LambdaEvent ;
6969pub use lambda_runtime:: { self , service_fn, tower, Context , Error , Service } ;
70+ use request:: RequestFuture ;
71+ use response:: ResponseFuture ;
7072
7173pub mod ext;
7274pub mod request;
@@ -91,9 +93,9 @@ pub type Request = http::Request<Body>;
9193///
9294/// This is used by the `Adapter` wrapper and is completely internal to the `lambda_http::run` function.
9395#[ doc( hidden) ]
94- pub struct TransformResponse < ' a , R , E > {
95- request_origin : RequestOrigin ,
96- fut : Pin < Box < dyn Future < Output = Result < R , E > > + ' a > > ,
96+ pub enum TransformResponse < ' a , R , E > {
97+ Request ( RequestOrigin , RequestFuture < ' a , R , E > ) ,
98+ Response ( RequestOrigin , ResponseFuture ) ,
9799}
98100
99101impl < ' a , R , E > Future for TransformResponse < ' a , R , E >
@@ -103,11 +105,19 @@ where
103105 type Output = Result < LambdaResponse , E > ;
104106
105107 fn poll ( mut self : Pin < & mut Self > , cx : & mut TaskContext ) -> Poll < Self :: Output > {
106- match self . fut . as_mut ( ) . poll ( cx) {
107- Poll :: Ready ( result) => Poll :: Ready (
108- result. map ( |resp| LambdaResponse :: from_response ( & self . request_origin , resp. into_response ( ) ) ) ,
109- ) ,
110- Poll :: Pending => Poll :: Pending ,
108+ match * self {
109+ TransformResponse :: Request ( ref mut origin, ref mut request) => match request. as_mut ( ) . poll ( cx) {
110+ Poll :: Ready ( Ok ( resp) ) => {
111+ * self = TransformResponse :: Response ( origin. clone ( ) , resp. into_response ( ) ) ;
112+ self . poll ( cx)
113+ }
114+ Poll :: Ready ( Err ( err) ) => Poll :: Ready ( Err ( err) ) ,
115+ Poll :: Pending => Poll :: Pending ,
116+ } ,
117+ TransformResponse :: Response ( ref mut origin, ref mut response) => match response. as_mut ( ) . poll ( cx) {
118+ Poll :: Ready ( resp) => Poll :: Ready ( Ok ( LambdaResponse :: from_response ( origin, resp) ) ) ,
119+ Poll :: Pending => Poll :: Pending ,
120+ } ,
111121 }
112122 }
113123}
@@ -153,7 +163,8 @@ where
153163 let request_origin = req. payload . request_origin ( ) ;
154164 let event: Request = req. payload . into ( ) ;
155165 let fut = Box :: pin ( self . service . call ( event. with_lambda_context ( req. context ) ) ) ;
156- TransformResponse { request_origin, fut }
166+
167+ TransformResponse :: Request ( request_origin, fut)
157168 }
158169}
159170
0 commit comments