1
0
Fork 0
mirror of https://github.com/tokio-rs/axum.git synced 2025-04-02 04:27:23 +02:00

Simplify macros for implementing Handler and FromRequest ()

Makes the generated docs nicer and avoids having a recursive macro.
This commit is contained in:
David Pedersen 2021-09-19 17:51:25 +02:00 committed by GitHub
parent 0b9e0c7508
commit 48401f2c8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 31 deletions
src
extract
handler

View file

@ -20,29 +20,39 @@ where
}
macro_rules! impl_from_request {
() => {
};
() => {};
( $head:ident, $($tail:ident),* $(,)? ) => {
( $($ty:ident),* $(,)? ) => {
#[async_trait]
#[allow(non_snake_case)]
impl<B, $head, $($tail,)*> FromRequest<B> for ($head, $($tail,)*)
impl<B, $($ty,)*> FromRequest<B> for ($($ty,)*)
where
$head: FromRequest<B> + Send,
$( $tail: FromRequest<B> + Send, )*
$( $ty: FromRequest<B> + Send, )*
B: Send,
{
type Rejection = Response<BoxBody>;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let $head = $head::from_request(req).await.map_err(|err| err.into_response().map(box_body))?;
$( let $tail = $tail::from_request(req).await.map_err(|err| err.into_response().map(box_body))?; )*
Ok(($head, $($tail,)*))
$( let $ty = $ty::from_request(req).await.map_err(|err| err.into_response().map(box_body))?; )*
Ok(($($ty,)*))
}
}
impl_from_request!($($tail,)*);
};
}
impl_from_request!(T1);
impl_from_request!(T1, T2);
impl_from_request!(T1, T2, T3);
impl_from_request!(T1, T2, T3, T4);
impl_from_request!(T1, T2, T3, T4, T5);
impl_from_request!(T1, T2, T3, T4, T5, T6);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
impl_from_request!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);

View file

@ -2,7 +2,7 @@
use crate::{
body::{box_body, BoxBody},
extract::FromRequest,
extract::{FromRequest, RequestParts},
response::IntoResponse,
routing::{EmptyRouter, MethodFilter},
service::HandleError,
@ -319,48 +319,52 @@ where
}
macro_rules! impl_handler {
() => {
};
( $head:ident, $($tail:ident),* $(,)? ) => {
( $($ty:ident),* $(,)? ) => {
#[async_trait]
#[allow(non_snake_case)]
impl<F, Fut, B, Res, $head, $($tail,)*> Handler<B, ($head, $($tail,)*)> for F
impl<F, Fut, B, Res, $($ty,)*> Handler<B, ($($ty,)*)> for F
where
F: FnOnce($head, $($tail,)*) -> Fut + Clone + Send + Sync + 'static,
F: FnOnce($($ty,)*) -> Fut + Clone + Send + Sync + 'static,
Fut: Future<Output = Res> + Send,
B: Send + 'static,
Res: IntoResponse,
$head: FromRequest<B> + Send,
$( $tail: FromRequest<B> + Send,)*
$( $ty: FromRequest<B> + Send,)*
{
type Sealed = sealed::Hidden;
async fn call(self, req: Request<B>) -> Response<BoxBody> {
let mut req = crate::extract::RequestParts::new(req);
let $head = match $head::from_request(&mut req).await {
Ok(value) => value,
Err(rejection) => return rejection.into_response().map(box_body),
};
let mut req = RequestParts::new(req);
$(
let $tail = match $tail::from_request(&mut req).await {
let $ty = match $ty::from_request(&mut req).await {
Ok(value) => value,
Err(rejection) => return rejection.into_response().map(box_body),
};
)*
let res = self($head, $($tail,)*).await;
let res = self($($ty,)*).await;
res.into_response().map(crate::body::box_body)
res.into_response().map(box_body)
}
}
impl_handler!($($tail,)*);
};
}
impl_handler!(T1);
impl_handler!(T1, T2);
impl_handler!(T1, T2, T3);
impl_handler!(T1, T2, T3, T4);
impl_handler!(T1, T2, T3, T4, T5);
impl_handler!(T1, T2, T3, T4, T5, T6);
impl_handler!(T1, T2, T3, T4, T5, T6, T7);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
/// A [`Service`] created from a [`Handler`] by applying a Tower middleware.