Skip to content

Commit

Permalink
fix: Generics for serde shadow struct
Browse files Browse the repository at this point in the history
  • Loading branch information
vrurg committed Dec 22, 2024
1 parent 7c8ad96 commit 7c8324e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 18 additions & 0 deletions fieldx/tests/builder_generic_default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use fieldx::fxstruct;
use serde::{Deserialize, Serialize};

#[fxstruct(no_new, builder, get, serde)]
#[derive(Clone)]
struct Foo<S = String>
where
S: AsRef<str> + 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");
}
16 changes: 8 additions & 8 deletions fieldx_derive/src/codegen/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl FXRewriteSerde for super::FXRewriter {
let mut serde_args: Vec<TokenStream> = 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());

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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(())
});
Expand All @@ -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 )*
Expand Down

0 comments on commit 7c8324e

Please sign in to comment.