@@ -77,6 +77,7 @@ use crate::{
77
77
} ;
78
78
use std:: {
79
79
future:: Future ,
80
+ marker:: PhantomData ,
80
81
pin:: Pin ,
81
82
task:: { Context as TaskContext , Poll } ,
82
83
} ;
@@ -87,28 +88,31 @@ pub type Request = http::Request<Body>;
87
88
/// Functions serving as ALB and API Gateway REST and HTTP API handlers must conform to this type.
88
89
///
89
90
/// This can be viewed as a `lambda_runtime::Handler` constrained to `http` crate `Request` and `Response` types
90
- pub trait Handler : Sized {
91
+ pub trait Handler < ' a > : Sized {
91
92
/// The type of Error that this Handler will return
92
93
type Error ;
93
94
/// The type of Response this Handler will return
94
95
type Response : IntoResponse ;
95
96
/// The type of Future this Handler will return
96
- type Fut : Future < Output = Result < Self :: Response , Self :: Error > > + Send + ' static ;
97
+ type Fut : Future < Output = Result < Self :: Response , Self :: Error > > + Send + ' a ;
97
98
/// Function used to execute handler behavior
98
99
fn call ( & self , event : Request , context : Context ) -> Self :: Fut ;
99
100
}
100
101
101
102
/// Adapts a [`Handler`](trait.Handler.html) to the `lambda_runtime::run` interface
102
- pub fn handler < H : Handler > ( handler : H ) -> Adapter < H > {
103
- Adapter { handler }
103
+ pub fn handler < ' a , H : Handler < ' a > > ( handler : H ) -> Adapter < ' a , H > {
104
+ Adapter {
105
+ handler,
106
+ _pd : PhantomData ,
107
+ }
104
108
}
105
109
106
110
/// An implementation of `Handler` for a given closure return a `Future` representing the computed response
107
- impl < F , R , Fut > Handler for F
111
+ impl < ' a , F , R , Fut > Handler < ' a > for F
108
112
where
109
113
F : Fn ( Request , Context ) -> Fut ,
110
114
R : IntoResponse ,
111
- Fut : Future < Output = Result < R , Error > > + Send + ' static ,
115
+ Fut : Future < Output = Result < R , Error > > + Send + ' a ,
112
116
{
113
117
type Response = R ;
114
118
type Error = Error ;
@@ -119,12 +123,12 @@ where
119
123
}
120
124
121
125
#[ doc( hidden) ]
122
- pub struct TransformResponse < R , E > {
126
+ pub struct TransformResponse < ' a , R , E > {
123
127
request_origin : RequestOrigin ,
124
- fut : Pin < Box < dyn Future < Output = Result < R , E > > + Send > > ,
128
+ fut : Pin < Box < dyn Future < Output = Result < R , E > > + Send + ' a > > ,
125
129
}
126
130
127
- impl < R , E > Future for TransformResponse < R , E >
131
+ impl < ' a , R , E > Future for TransformResponse < ' a , R , E >
128
132
where
129
133
R : IntoResponse ,
130
134
{
@@ -146,11 +150,12 @@ where
146
150
///
147
151
/// See [this article](http://smallcultfollowing.com/babysteps/blog/2015/01/14/little-orphan-impls/)
148
152
/// for a larger explaination of why this is nessessary
149
- pub struct Adapter < H : Handler > {
153
+ pub struct Adapter < ' a , H : Handler < ' a > > {
150
154
handler : H ,
155
+ _pd : PhantomData < & ' a H > ,
151
156
}
152
157
153
- impl < H : Handler > Handler for Adapter < H > {
158
+ impl < ' a , H : Handler < ' a > > Handler < ' a > for Adapter < ' a , H > {
154
159
type Response = H :: Response ;
155
160
type Error = H :: Error ;
156
161
type Fut = H :: Fut ;
@@ -159,9 +164,9 @@ impl<H: Handler> Handler for Adapter<H> {
159
164
}
160
165
}
161
166
162
- impl < H : Handler > LambdaHandler < LambdaRequest < ' _ > , LambdaResponse > for Adapter < H > {
167
+ impl < ' a , H : Handler < ' a > > LambdaHandler < LambdaRequest < ' a > , LambdaResponse > for Adapter < ' a , H > {
163
168
type Error = H :: Error ;
164
- type Fut = TransformResponse < H :: Response , Self :: Error > ;
169
+ type Fut = TransformResponse < ' a , H :: Response , Self :: Error > ;
165
170
166
171
fn call ( & self , event : LambdaRequest < ' _ > , context : Context ) -> Self :: Fut {
167
172
let request_origin = event. request_origin ( ) ;
0 commit comments