Remove B type param (#1751)

Co-authored-by: Jonas Platte <[email protected]>
Co-authored-by: Michael Scofield <[email protected]>
This commit is contained in:
David Pedersen
2023-04-21 17:45:31 +02:00
co-authored by Jonas Platte Michael Scofield
parent 9be0ea934c
commit 4e4c29175f
100 changed files with 966 additions and 1160 deletions
+8 -21
View File
@@ -6,14 +6,10 @@ use crate::{
};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use syn::{parse::Parse, parse_quote, spanned::Spanned, FnArg, ItemFn, Token, Type};
use syn::{parse::Parse, spanned::Spanned, FnArg, ItemFn, Token, Type};
pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream {
let Attrs { body_ty, state_ty } = attr;
let body_ty = body_ty
.map(second)
.unwrap_or_else(|| parse_quote!(axum::body::Body));
let Attrs { state_ty } = attr;
let mut state_ty = state_ty.map(second);
@@ -57,7 +53,7 @@ pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream {
}
} else {
let check_inputs_impls_from_request =
check_inputs_impls_from_request(&item_fn, &body_ty, state_ty);
check_inputs_impls_from_request(&item_fn, state_ty);
quote! {
#check_inputs_impls_from_request
@@ -88,20 +84,16 @@ mod kw {
}
pub(crate) struct Attrs {
body_ty: Option<(kw::body, Type)>,
state_ty: Option<(kw::state, Type)>,
}
impl Parse for Attrs {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut body_ty = None;
let mut state_ty = None;
while !input.is_empty() {
let lh = input.lookahead1();
if lh.peek(kw::body) {
parse_assignment_attribute(input, &mut body_ty)?;
} else if lh.peek(kw::state) {
if lh.peek(kw::state) {
parse_assignment_attribute(input, &mut state_ty)?;
} else {
return Err(lh.error());
@@ -110,7 +102,7 @@ impl Parse for Attrs {
let _ = input.parse::<Token![,]>();
}
Ok(Self { body_ty, state_ty })
Ok(Self { state_ty })
}
}
@@ -183,11 +175,7 @@ fn is_self_pat_type(typed: &syn::PatType) -> bool {
ident == "self"
}
fn check_inputs_impls_from_request(
item_fn: &ItemFn,
body_ty: &Type,
state_ty: Type,
) -> TokenStream {
fn check_inputs_impls_from_request(item_fn: &ItemFn, state_ty: Type) -> TokenStream {
let takes_self = item_fn.sig.inputs.first().map_or(false, |arg| match arg {
FnArg::Receiver(_) => true,
FnArg::Typed(typed) => is_self_pat_type(typed),
@@ -266,11 +254,11 @@ fn check_inputs_impls_from_request(
}
} else if consumes_request {
quote_spanned! {span=>
#ty: ::axum::extract::FromRequest<#state_ty, #body_ty> + Send
#ty: ::axum::extract::FromRequest<#state_ty> + Send
}
} else {
quote_spanned! {span=>
#ty: ::axum::extract::FromRequest<#state_ty, #body_ty, M> + Send
#ty: ::axum::extract::FromRequest<#state_ty, M> + Send
}
};
@@ -379,7 +367,6 @@ fn request_consuming_type_name(ty: &Type) -> Option<&'static str> {
let type_name = match &*ident.to_string() {
"Json" => "Json<_>",
"BodyStream" => "BodyStream",
"RawBody" => "RawBody<_>",
"RawForm" => "RawForm",
"Multipart" => "Multipart",
+11 -30
View File
@@ -19,13 +19,6 @@ pub(crate) enum Trait {
}
impl Trait {
fn body_type(&self) -> impl Iterator<Item = Type> {
match self {
Trait::FromRequest => Some(parse_quote!(B)).into_iter(),
Trait::FromRequestParts => None.into_iter(),
}
}
fn via_marker_type(&self) -> Option<Type> {
match self {
Trait::FromRequest => Some(parse_quote!(M)),
@@ -370,14 +363,12 @@ fn impl_struct_by_extracting_each_field(
quote!(::axum::response::Response)
};
let impl_generics = tr
.body_type()
.chain(state.impl_generics())
let impl_generics = state
.impl_generics()
.collect::<Punctuated<Type, Token![,]>>();
let trait_generics = state
.trait_generics()
.chain(tr.body_type())
.collect::<Punctuated<Type, Token![,]>>();
let state_bounds = state.bounds();
@@ -388,15 +379,12 @@ fn impl_struct_by_extracting_each_field(
#[automatically_derived]
impl<#impl_generics> ::axum::extract::FromRequest<#trait_generics> for #ident
where
B: ::axum::body::HttpBody + ::std::marker::Send + 'static,
B::Data: ::std::marker::Send,
B::Error: ::std::convert::Into<::axum::BoxError>,
#state_bounds
{
type Rejection = #rejection_ident;
async fn from_request(
mut req: ::axum::http::Request<B>,
mut req: ::axum::http::Request<::axum::body::Body>,
state: &#state,
) -> ::std::result::Result<Self, Self::Rejection> {
#trait_fn_body
@@ -749,7 +737,7 @@ fn impl_struct_by_extracting_all_at_once(
// struct AppState {}
// ```
//
// we need to implement `impl<B, M> FromRequest<AppState, B, M>` but only for
// we need to implement `impl<M> FromRequest<AppState, M>` but only for
// - `#[derive(FromRequest)]`, not `#[derive(FromRequestParts)]`
// - `State`, not other extractors
//
@@ -760,16 +748,15 @@ fn impl_struct_by_extracting_all_at_once(
None
};
let impl_generics = tr
.body_type()
.chain(via_marker_type.clone())
let impl_generics = via_marker_type
.iter()
.cloned()
.chain(state.impl_generics())
.chain(generic_ident.is_some().then(|| parse_quote!(T)))
.collect::<Punctuated<Type, Token![,]>>();
let trait_generics = state
.trait_generics()
.chain(tr.body_type())
.chain(via_marker_type)
.collect::<Punctuated<Type, Token![,]>>();
@@ -828,13 +815,12 @@ fn impl_struct_by_extracting_all_at_once(
where
#via_path<#via_type_generics>: ::axum::extract::FromRequest<#trait_generics>,
#rejection_bound
B: ::std::marker::Send + 'static,
#state_bounds
{
type Rejection = #associated_rejection_type;
async fn from_request(
req: ::axum::http::Request<B>,
req: ::axum::http::Request<::axum::body::Body>,
state: &#state,
) -> ::std::result::Result<Self, Self::Rejection> {
::axum::extract::FromRequest::from_request(req, state)
@@ -923,14 +909,12 @@ fn impl_enum_by_extracting_all_at_once(
let path_span = path.span();
let impl_generics = tr
.body_type()
.chain(state.impl_generics())
let impl_generics = state
.impl_generics()
.collect::<Punctuated<Type, Token![,]>>();
let trait_generics = state
.trait_generics()
.chain(tr.body_type())
.collect::<Punctuated<Type, Token![,]>>();
let state_bounds = state.bounds();
@@ -942,15 +926,12 @@ fn impl_enum_by_extracting_all_at_once(
#[automatically_derived]
impl<#impl_generics> ::axum::extract::FromRequest<#trait_generics> for #ident
where
B: ::axum::body::HttpBody + ::std::marker::Send + 'static,
B::Data: ::std::marker::Send,
B::Error: ::std::convert::Into<::axum::BoxError>,
#state_bounds
{
type Rejection = #associated_rejection_type;
async fn from_request(
req: ::axum::http::Request<B>,
req: ::axum::http::Request<::axum::body::Body>,
state: &#state,
) -> ::std::result::Result<Self, Self::Rejection> {
::axum::extract::FromRequest::from_request(req, state)
+1 -16
View File
@@ -148,7 +148,7 @@ use from_request::Trait::{FromRequest, FromRequestParts};
/// ```
/// pub struct ViaExtractor<T>(pub T);
///
/// // impl<T, S, B> FromRequest<S, B> for ViaExtractor<T> { ... }
/// // impl<T, S> FromRequest<S> for ViaExtractor<T> { ... }
/// ```
///
/// More complex via extractors are not supported and require writing a manual implementation.
@@ -481,21 +481,6 @@ pub fn derive_from_request_parts(item: TokenStream) -> TokenStream {
/// }
/// ```
///
/// # Changing request body type
///
/// By default `#[debug_handler]` assumes your request body type is `axum::body::Body`. This will
/// work for most extractors but, for example, it wont work for `Request<axum::body::BoxBody>`,
/// which only implements `FromRequest<BoxBody>` and _not_ `FromRequest<Body>`.
///
/// To work around that the request body type can be customized like so:
///
/// ```
/// use axum::{body::BoxBody, http::Request, debug_handler};
///
/// #[debug_handler(body = BoxBody)]
/// async fn handler(request: Request<BoxBody>) {}
/// ```
///
/// # Changing state type
///
/// By default `#[debug_handler]` assumes your state type is `()` unless your handler has a