diff --git a/src/lib.rs b/src/lib.rs index c4ee9919..6ecb4678 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,16 +160,20 @@ pub enum Error { // TODO(david): make this trait sealed #[async_trait] pub trait Handler { - async fn call(self, req: Request) -> Result, Error>; + type ResponseBody; + + async fn call(self, req: Request) -> Result, Error>; } #[async_trait] -impl Handler<()> for F +impl Handler<()> for F where F: Fn(Request) -> Fut + Send + Sync, - Fut: Future, Error>> + Send, + Fut: Future, Error>> + Send, { - async fn call(self, req: Request) -> Result, Error> { + type ResponseBody = B; + + async fn call(self, req: Request) -> Result, Error> { self(req).await } } @@ -178,13 +182,15 @@ macro_rules! impl_handler { ( $head:ident $(,)? ) => { #[async_trait] #[allow(non_snake_case)] - impl Handler<($head,)> for F + impl Handler<($head,)> for F where F: Fn(Request, $head) -> Fut + Send + Sync, - Fut: Future, Error>> + Send, + Fut: Future, Error>> + Send, $head: FromRequest + Send, { - async fn call(self, mut req: Request) -> Result, Error> { + type ResponseBody = B; + + async fn call(self, mut req: Request) -> Result, Error> { let $head = $head::from_request(&mut req).await?; let res = self(req, $head).await?; Ok(res) @@ -195,14 +201,16 @@ macro_rules! impl_handler { ( $head:ident, $($tail:ident),* $(,)? ) => { #[async_trait] #[allow(non_snake_case)] - impl Handler<($head, $($tail,)*)> for F + impl Handler<($head, $($tail,)*)> for F where F: Fn(Request, $head, $($tail,)*) -> Fut + Send + Sync, - Fut: Future, Error>> + Send, + Fut: Future, Error>> + Send, $head: FromRequest + Send, $( $tail: FromRequest + Send, )* { - async fn call(self, mut req: Request) -> Result, Error> { + type ResponseBody = B; + + async fn call(self, mut req: Request) -> Result, Error> { let $head = $head::from_request(&mut req).await?; $( let $tail = $tail::from_request(&mut req).await?; @@ -238,8 +246,9 @@ where impl Service> for HandlerSvc where H: Handler + Clone + 'static, + H::ResponseBody: 'static, { - type Response = Response; + type Response = Response; type Error = Error; type Future = future::BoxFuture<'static, Result>;