Skip to content

Commit 76d26b9

Browse files
authored
Don't require Lambda Service to use lambda_http::Error (#474)
Currently, the `Service` that you pass to `lambda_http::run` is required to use `lambda_http::Error` as its associated error type, a type-erased boxed error type. However, all the runtime needs is for the error type to implement `Debug` and `Display`. This commit relaxes this constraint, allowing users to run a broader class of `Service`s.
1 parent 4b3e6c3 commit 76d26b9

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

lambda-http/examples/shared-resources-example.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lambda_http::{service_fn, Error, IntoResponse, Request, RequestExt, Response};
1+
use lambda_http::{service_fn, Body, Error, IntoResponse, Request, RequestExt, Response};
22

33
struct SharedClient {
44
name: &'static str,
@@ -20,15 +20,17 @@ async fn main() -> Result<(), Error> {
2020

2121
// Define a closure here that makes use of the shared client.
2222
let handler_func_closure = move |event: Request| async move {
23-
Ok(match event.query_string_parameters().first("first_name") {
24-
Some(first_name) => shared_client_ref
25-
.response(event.lambda_context().request_id, first_name)
26-
.into_response(),
27-
_ => Response::builder()
28-
.status(400)
29-
.body("Empty first name".into())
30-
.expect("failed to render response"),
31-
})
23+
Result::<Response<Body>, std::convert::Infallible>::Ok(
24+
match event.query_string_parameters().first("first_name") {
25+
Some(first_name) => shared_client_ref
26+
.response(event.lambda_context().request_id, first_name)
27+
.into_response(),
28+
_ => Response::builder()
29+
.status(400)
30+
.body("Empty first name".into())
31+
.expect("failed to render response"),
32+
},
33+
)
3234
};
3335

3436
// Pass the closure to the runtime here.

lambda-http/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
//! async fn main() -> Result<(), Error> {
2424
//! // initialize dependencies once here for the lifetime of your
2525
//! // lambda task
26-
//! lambda_http::run(service_fn(|request| async { Ok("👋 world!") })).await?;
26+
//! lambda_http::run(service_fn(|request| async {
27+
//! Result::<&str, std::convert::Infallible>::Ok("👋 world!")
28+
//! })).await?;
2729
//! Ok(())
2830
//! }
2931
//! ```
@@ -44,7 +46,7 @@
4446
//!
4547
//! async fn hello(
4648
//! request: Request
47-
//! ) -> Result<impl IntoResponse, Error> {
49+
//! ) -> Result<impl IntoResponse, std::convert::Infallible> {
4850
//! let _context = request.lambda_context();
4951
//!
5052
//! Ok(format!(
@@ -119,9 +121,9 @@ pub struct Adapter<'a, R, S> {
119121
_phantom_data: PhantomData<&'a R>,
120122
}
121123

122-
impl<'a, R, S> From<S> for Adapter<'a, R, S>
124+
impl<'a, R, S, E> From<S> for Adapter<'a, R, S>
123125
where
124-
S: Service<Request, Response = R, Error = Error>,
126+
S: Service<Request, Response = R, Error = E>,
125127
S::Future: 'a,
126128
R: IntoResponse,
127129
{
@@ -133,14 +135,14 @@ where
133135
}
134136
}
135137

136-
impl<'a, R, S> Service<LambdaEvent<LambdaRequest>> for Adapter<'a, R, S>
138+
impl<'a, R, S, E> Service<LambdaEvent<LambdaRequest>> for Adapter<'a, R, S>
137139
where
138-
S: Service<Request, Response = R, Error = Error>,
140+
S: Service<Request, Response = R, Error = E>,
139141
S::Future: 'a,
140142
R: IntoResponse,
141143
{
142144
type Response = LambdaResponse;
143-
type Error = Error;
145+
type Error = E;
144146
type Future = TransformResponse<'a, R, Self::Error>;
145147

146148
fn poll_ready(&mut self, _cx: &mut core::task::Context<'_>) -> core::task::Poll<Result<(), Self::Error>> {
@@ -160,11 +162,12 @@ where
160162
///
161163
/// This takes care of transforming the LambdaEvent into a [`Request`] and then
162164
/// converting the result into a [`LambdaResponse`].
163-
pub async fn run<'a, R, S>(handler: S) -> Result<(), Error>
165+
pub async fn run<'a, R, S, E>(handler: S) -> Result<(), Error>
164166
where
165-
S: Service<Request, Response = R, Error = Error>,
167+
S: Service<Request, Response = R, Error = E>,
166168
S::Future: 'a,
167169
R: IntoResponse,
170+
E: std::fmt::Debug + std::fmt::Display,
168171
{
169172
lambda_runtime::run(Adapter::from(handler)).await
170173
}

0 commit comments

Comments
 (0)