From 7c8324edcf8c2a7ac0bb274d1785dfc0c83dd175 Mon Sep 17 00:00:00 2001 From: Vadim Belman Date: Sat, 21 Dec 2024 22:56:29 -0500 Subject: [PATCH] fix: Generics for serde shadow struct --- fieldx/tests/builder_generic_default.rs | 18 ++++++++++++++++++ fieldx_derive/src/codegen/serde.rs | 16 ++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 fieldx/tests/builder_generic_default.rs diff --git a/fieldx/tests/builder_generic_default.rs b/fieldx/tests/builder_generic_default.rs new file mode 100644 index 0000000..950024c --- /dev/null +++ b/fieldx/tests/builder_generic_default.rs @@ -0,0 +1,18 @@ +use fieldx::fxstruct; +use serde::{Deserialize, Serialize}; + +#[fxstruct(no_new, builder, get, serde)] +#[derive(Clone)] +struct Foo +where + S: AsRef + Default + Clone, +{ + foo: S, +} + +#[test] +fn generic_default() { + let foo = Foo::builder().foo("foo".to_string()).build().unwrap(); + let foo_s: &str = foo.foo(); + assert_eq!(foo_s, "foo", "default generic value is empty string"); +} diff --git a/fieldx_derive/src/codegen/serde.rs b/fieldx_derive/src/codegen/serde.rs index a95f6b5..1e806c3 100644 --- a/fieldx_derive/src/codegen/serde.rs +++ b/fieldx_derive/src/codegen/serde.rs @@ -258,7 +258,7 @@ impl FXRewriteSerde for super::FXRewriter { let mut serde_args: Vec = vec![]; let serde_helper = args.serde().as_ref().unwrap(); - let generics = ctx.input().generics(); + let (_, generics, _) = ctx.input().generics().split_for_impl(); let shadow_ident = format!("{}{}", ctx.shadow_ident(), generics.to_token_stream()); let serde_span = args.serde_helper_span().unwrap_or_else(|| Span::call_site()); @@ -344,8 +344,7 @@ impl FXRewriteSerde for super::FXRewriter { let struct_ident = ctx.input_ident(); let shadow_var = ctx.shadow_var_ident(); let mut fields = vec![]; - let generics = ctx.input().generics(); - let where_clause = &generics.where_clause; + let (_, generics, where_clause) = ctx.input().generics().split_for_impl(); let span = args.serde_helper_span().unwrap_or_else(|| Span::call_site()); for field in ctx.all_fields() { @@ -397,8 +396,8 @@ impl FXRewriteSerde for super::FXRewriter { let mut fields = vec![]; let mut lazy_inits = vec![]; let me_var = ctx.me_var_ident(); - let generics = ctx.input().generics(); - let where_clause = &generics.where_clause; + let (_, generics, where_clause) = ctx.input().generics().split_for_impl(); + let span = args.serde_helper_span().unwrap_or_else(|| Span::call_site()); for field in ctx.all_fields() { let fctx = ctx.field_ctx(field); @@ -410,13 +409,14 @@ impl FXRewriteSerde for super::FXRewriter { ctx.exec_or_record(|| { let cgen = self.field_codegen(&fctx)?; let fetch_struct_field = cgen.field_from_struct(&fctx)?; + let span = *fctx.span(); if is_lazy { let init_call = cgen.field_lazy_initializer(&fctx, Some(quote![(&#me_var)]))?; - lazy_inits.push(quote![ let _ = #init_call; ]); + lazy_inits.push(quote_spanned![span=> let _ = #init_call; ]); } - fields.push(quote_spanned![*fctx.span()=> #field_ident: #fetch_struct_field ]); + fields.push(quote_spanned![span=> #field_ident: #fetch_struct_field ]); Ok(()) }); @@ -427,7 +427,7 @@ impl FXRewriteSerde for super::FXRewriter { } } - ctx.tokens_extend(quote![ + ctx.tokens_extend(quote_spanned![span=> impl #generics ::std::convert::From<#struct_ident #generics> for #shadow_ident #generics #where_clause { fn from(mut #me_var: #struct_ident #generics) -> Self { #( #lazy_inits )*