Skip to content

Commit

Permalink
Add manual Default implementation for #[handler] (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinety authored Jul 23, 2024
1 parent 0a31ca5 commit f72dda0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions poem-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn generate_handler(internal: bool, input: TokenStream) -> Result<TokenStream> {
};

let def_struct = if !item_fn.sig.generics.params.is_empty() {
let members = item_fn
let iter = item_fn
.sig
.generics
.params
Expand All @@ -70,13 +70,26 @@ fn generate_handler(internal: bool, input: TokenStream) -> Result<TokenStream> {
})
.enumerate()
.map(|(idx, ty)| {
let ty_ident = &ty.ident;
let ident = format_ident!("_mark{}", idx);
quote! { #ident: ::std::marker::PhantomData<#ty_ident> }
let ty_ident = &ty.ident;
(ident, ty_ident)
});

let struct_members = iter.clone().map(|(ident, ty_ident)| {
quote! { #ident: ::std::marker::PhantomData<#ty_ident> }
});

let default_members = iter.clone().map(|(ident, _ty_ident)| {
quote! { #ident: ::std::marker::PhantomData }
});

quote! {
#[derive(Default)]
#vis struct #ident #type_generics { #(#members),*}
#vis struct #ident #type_generics { #(#struct_members),*}
impl #type_generics ::std::default::Default for #ident #type_generics {
fn default() -> Self {
Self { #(#default_members),* }
}
}
}
} else {
quote! { #vis struct #ident; }
Expand Down

0 comments on commit f72dda0

Please sign in to comment.