@@ -9,10 +9,7 @@ use futures_util::{Stream, TryFutureExt};
99pub use http:: { self , Response } ;
1010use http_body:: Body ;
1111use lambda_runtime:: {
12- tower:: {
13- util:: { MapRequest , MapResponse } ,
14- ServiceBuilder , ServiceExt ,
15- } ,
12+ tower:: { util:: BoxCloneService , ServiceBuilder , ServiceExt } ,
1613 Diagnostic ,
1714} ;
1815pub use lambda_runtime:: { Error , LambdaEvent , MetadataPrelude , Service , StreamResponse } ;
@@ -27,10 +24,22 @@ pub struct StreamAdapter<'a, S, B> {
2724 _phantom_data : PhantomData < & ' a B > ,
2825}
2926
27+ impl < ' a , S , B > Clone for StreamAdapter < ' a , S , B >
28+ where
29+ S : Clone ,
30+ {
31+ fn clone ( & self ) -> Self {
32+ Self {
33+ service : self . service . clone ( ) ,
34+ _phantom_data : PhantomData ,
35+ }
36+ }
37+ }
38+
3039impl < ' a , S , B , E > From < S > for StreamAdapter < ' a , S , B >
3140where
32- S : Service < Request , Response = Response < B > , Error = E > ,
33- S :: Future : Send + ' a ,
41+ S : Service < Request , Response = Response < B > , Error = E > + Clone + Send + ' static ,
42+ S :: Future : Send + ' static ,
3443 B : Body + Unpin + Send + ' static ,
3544 B :: Data : Into < Bytes > + Send ,
3645 B :: Error : Into < Error > + Send + Debug ,
@@ -45,15 +54,15 @@ where
4554
4655impl < ' a , S , B , E > Service < LambdaEvent < LambdaRequest > > for StreamAdapter < ' a , S , B >
4756where
48- S : Service < Request , Response = Response < B > , Error = E > ,
49- S :: Future : Send + ' a ,
57+ S : Service < Request , Response = Response < B > , Error = E > + Clone + Send + ' static ,
58+ S :: Future : Send + ' static ,
5059 B : Body + Unpin + Send + ' static ,
5160 B :: Data : Into < Bytes > + Send ,
5261 B :: Error : Into < Error > + Send + Debug ,
5362{
5463 type Response = StreamResponse < BodyStream < B > > ;
5564 type Error = E ;
56- type Future = Pin < Box < dyn Future < Output = Result < Self :: Response , Self :: Error > > + Send + ' a > > ;
65+ type Future = Pin < Box < dyn Future < Output = Result < Self :: Response , Self :: Error > > + Send + ' static > > ;
5766
5867 fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
5968 self . service . poll_ready ( cx)
@@ -78,27 +87,23 @@ where
7887/// Used internally by [`run_with_streaming_response`]; not part of the public
7988/// API.
8089#[ allow( clippy:: type_complexity) ]
81- fn into_stream_service < ' a , S , B , E > (
90+ fn into_stream_service < S , B , E > (
8291 handler : S ,
83- ) -> MapResponse <
84- MapRequest < S , impl FnMut ( LambdaEvent < LambdaRequest > ) -> Request > ,
85- impl FnOnce ( Response < B > ) -> StreamResponse < BodyStream < B > > + Clone ,
86- >
92+ ) -> BoxCloneService < LambdaEvent < LambdaRequest > , StreamResponse < BodyStream < B > > , E >
8793where
88- S : Service < Request , Response = Response < B > , Error = E > ,
89- S :: Future : Send + ' a ,
90- E : Debug + Into < Diagnostic > ,
94+ S : Service < Request , Response = Response < B > , Error = E > + Clone + Send + ' static ,
95+ S :: Future : Send + ' static ,
96+ E : Debug + Into < Diagnostic > + Send + ' static ,
9197 B : Body + Unpin + Send + ' static ,
9298 B :: Data : Into < Bytes > + Send ,
9399 B :: Error : Into < Error > + Send + Debug ,
94100{
95- ServiceBuilder :: new ( )
96- . map_request ( |req : LambdaEvent < LambdaRequest > | {
97- let event: Request = req. payload . into ( ) ;
98- event. with_lambda_context ( req. context )
99- } )
101+ let svc = ServiceBuilder :: new ( )
102+ . map_request ( event_to_request as fn ( LambdaEvent < LambdaRequest > ) -> Request )
100103 . service ( handler)
101- . map_response ( into_stream_response)
104+ . map_response ( into_stream_response) ;
105+
106+ BoxCloneService :: new ( svc)
102107}
103108
104109/// Converts an `http::Response<B>` into a streaming Lambda response.
@@ -128,18 +133,23 @@ where
128133 }
129134}
130135
136+ fn event_to_request ( req : LambdaEvent < LambdaRequest > ) -> Request {
137+ let event: Request = req. payload . into ( ) ;
138+ event. with_lambda_context ( req. context )
139+ }
140+
131141/// Runs the Lambda runtime with a handler that returns **streaming** HTTP
132142/// responses.
133143///
134144/// See the [AWS docs for response streaming].
135145///
136146/// [AWS docs for response streaming]:
137147/// https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html
138- pub async fn run_with_streaming_response < ' a , S , B , E > ( handler : S ) -> Result < ( ) , Error >
148+ pub async fn run_with_streaming_response < S , B , E > ( handler : S ) -> Result < ( ) , Error >
139149where
140- S : Service < Request , Response = Response < B > , Error = E > ,
141- S :: Future : Send + ' a ,
142- E : Debug + Into < Diagnostic > ,
150+ S : Service < Request , Response = Response < B > , Error = E > + Clone + Send + ' static ,
151+ S :: Future : Send + ' static ,
152+ E : Debug + Into < Diagnostic > + Send + ' static ,
143153 B : Body + Unpin + Send + ' static ,
144154 B :: Data : Into < Bytes > + Send ,
145155 B :: Error : Into < Error > + Send + Debug ,
0 commit comments