From f72dda0c77b9092852ae475f3c2b160792027b46 Mon Sep 17 00:00:00 2001 From: Thiago Trannin <51510921+thinety@users.noreply.github.com> Date: Tue, 23 Jul 2024 02:42:09 -0300 Subject: [PATCH] Add manual `Default` implementation for `#[handler]` (#848) --- poem-derive/src/lib.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/poem-derive/src/lib.rs b/poem-derive/src/lib.rs index ed12849991..fb2a16644e 100644 --- a/poem-derive/src/lib.rs +++ b/poem-derive/src/lib.rs @@ -59,7 +59,7 @@ fn generate_handler(internal: bool, input: TokenStream) -> Result { }; let def_struct = if !item_fn.sig.generics.params.is_empty() { - let members = item_fn + let iter = item_fn .sig .generics .params @@ -70,13 +70,26 @@ fn generate_handler(internal: bool, input: TokenStream) -> Result { }) .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; }