From 1b0d6b67df791f98a51596ef10ee5daf4a2117e8 Mon Sep 17 00:00:00 2001 From: Wonwoo Choi Date: Sat, 10 Nov 2018 13:14:21 +0900 Subject: [PATCH 1/2] Refactor `end_point_impl!` --- src/endpoint.rs | 58 ++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/src/endpoint.rs b/src/endpoint.rs index 014ec3a6f..f02df67c9 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -46,46 +46,26 @@ impl BoxedEndpoint { #[doc(hidden)] pub struct Ty(T); -macro_rules! end_point_impl_with_head { - ($($X:ident),*) => { - impl Endpoint, Head, $(Ty<$X>),*)> for T - where - T: Send + Sync + Clone + 'static + Fn(Head, $($X),*) -> Fut, - Data: Clone + Send + Sync + 'static, - Fut: Future + Send + 'static, - Fut::Output: IntoResponse, - $( - $X: Extract - ),* - { - type Fut = FutureObj<'static, (Head, Response)>; - - #[allow(unused_mut, non_snake_case)] - fn call(&self, mut data: Data, mut req: Request, params: RouteMatch<'_>) -> Self::Fut { - let f = self.clone(); - $(let $X = $X::extract(&mut data, &mut req, ¶ms);)* - FutureObj::new(Box::new(async move { - let (parts, _) = req.into_parts(); - let head = Head::from(parts); - $(let $X = match await!($X) { - Ok(x) => x, - Err(resp) => return (head, resp), - };)* - let res = await!(f(head.clone(), $($X),*)); - - (head, res.into_response()) - })) - } - } +macro_rules! call_f { + ($head_ty:ty; ($f:ident, $head:ident); $($X:ident),*) => { + $f($head.clone(), $($X),*) + }; + (($f:ident, $head:ident); $($X:ident),*) => { + $f($($X),*) }; } -// TODO: refactor to share code with the above macro -macro_rules! end_point_impl_no_head { - ($($X:ident),*) => { - impl Endpoint, $(Ty<$X>),*)> for T +macro_rules! end_point_impl_raw { + (with_head; $($X:ident),*) => { + end_point_impl_raw!(<$($X),*> Head); + }; + (without_head; $($X:ident),*) => { + end_point_impl_raw!(<$($X),*>); + }; + (<$($X:ident),*> $($head:ty)*) => { + impl Endpoint, $($head,)* $(Ty<$X>),*)> for T where - T: Send + Sync + Clone + 'static + Fn($($X),*) -> Fut, + T: Send + Sync + Clone + 'static + Fn($($head,)* $($X),*) -> Fut, Data: Clone + Send + Sync + 'static, Fut: Future + Send + 'static, Fut::Output: IntoResponse, @@ -106,7 +86,7 @@ macro_rules! end_point_impl_no_head { Ok(x) => x, Err(resp) => return (head, resp), };)* - let res = await!(f($($X),*)); + let res = await!(call_f!($($head;)* (f, head); $($X),*)); (head, res.into_response()) })) @@ -117,8 +97,8 @@ macro_rules! end_point_impl_no_head { macro_rules! end_point_impl { ($($X:ident),*) => { - end_point_impl_with_head!($($X),*); - end_point_impl_no_head!($($X),*); + end_point_impl_raw!(with_head; $($X),*); + end_point_impl_raw!(without_head; $($X),*); } } From aa69f8661999f8bafd3b9bb08e645c2e1cdb7dda Mon Sep 17 00:00:00 2001 From: Wonwoo Choi Date: Sat, 10 Nov 2018 16:17:21 +0900 Subject: [PATCH 2/2] Simplify `end_point_impl_raw!` rules --- src/endpoint.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/endpoint.rs b/src/endpoint.rs index f02df67c9..c1246e8ed 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -56,13 +56,7 @@ macro_rules! call_f { } macro_rules! end_point_impl_raw { - (with_head; $($X:ident),*) => { - end_point_impl_raw!(<$($X),*> Head); - }; - (without_head; $($X:ident),*) => { - end_point_impl_raw!(<$($X),*>); - }; - (<$($X:ident),*> $($head:ty)*) => { + ($([$head:ty])* $($X:ident),*) => { impl Endpoint, $($head,)* $(Ty<$X>),*)> for T where T: Send + Sync + Clone + 'static + Fn($($head,)* $($X),*) -> Fut, @@ -97,8 +91,8 @@ macro_rules! end_point_impl_raw { macro_rules! end_point_impl { ($($X:ident),*) => { - end_point_impl_raw!(with_head; $($X),*); - end_point_impl_raw!(without_head; $($X),*); + end_point_impl_raw!([Head] $($X),*); + end_point_impl_raw!($($X),*); } }