Skip to content

Commit

Permalink
Inline wrap_deserialize_variant_with because it is very short and use…
Browse files Browse the repository at this point in the history
…d only once
  • Loading branch information
Mingun committed Jul 31, 2023
1 parent 4647c19 commit 0c8fa03
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,7 @@ fn deserialize_adjacently_tagged_enum(
Style::Unit => quote! {
_serde::__private::Ok(#this_value::#variant_ident)
},
// Feature https://github.com/serde-rs/serde/issues/1013
Style::Newtype if variant.attrs.deserialize_with().is_none() => {
let span = variant.original.span();
let func = quote_spanned!(span=> _serde::__private::de::missing_field);
Expand Down Expand Up @@ -1856,8 +1857,13 @@ fn deserialize_externally_tagged_variant(
variant: &Variant,
cattrs: &attr::Container,
) -> Fragment {
// Feature https://github.com/serde-rs/serde/issues/1013
if let Some(path) = variant.attrs.deserialize_with() {
let (wrapper, wrapper_ty, unwrap_fn) = wrap_deserialize_variant_with(params, variant, path);
let field_tys = variant.fields.iter().map(|field| field.ty);
let (wrapper, wrapper_ty) = wrap_deserialize_with(params, &quote!((#(#field_tys),*)), path);

let unwrap_fn = unwrap_to_variant_closure(params, variant, true);

return quote_block! {
#wrapper
_serde::__private::Result::map(
Expand Down Expand Up @@ -1904,6 +1910,7 @@ fn deserialize_internally_tagged_variant(
cattrs: &attr::Container,
deserializer: TokenStream,
) -> Fragment {
// Feature https://github.com/serde-rs/serde/issues/1013
if variant.attrs.deserialize_with().is_some() {
return deserialize_untagged_variant(params, variant, cattrs, deserializer);
}
Expand Down Expand Up @@ -1940,6 +1947,7 @@ fn deserialize_untagged_variant(
cattrs: &attr::Container,
deserializer: TokenStream,
) -> Fragment {
// Feature https://github.com/serde-rs/serde/issues/1013
if let Some(path) = variant.attrs.deserialize_with() {
let unwrap_fn = unwrap_to_variant_closure(params, variant, false);
return quote_block! {
Expand Down Expand Up @@ -3001,20 +3009,6 @@ fn wrap_deserialize_field_with(
wrap_deserialize_with(params, &quote!(#field_ty), deserialize_with)
}

fn wrap_deserialize_variant_with(
params: &Parameters,
variant: &Variant,
deserialize_with: &syn::ExprPath,
) -> (TokenStream, TokenStream, TokenStream) {
let field_tys = variant.fields.iter().map(|field| field.ty);
let (wrapper, wrapper_ty) =
wrap_deserialize_with(params, &quote!((#(#field_tys),*)), deserialize_with);

let unwrap_fn = unwrap_to_variant_closure(params, variant, true);

(wrapper, wrapper_ty, unwrap_fn)
}

// Generates closure that converts single input parameter to the final value.
fn unwrap_to_variant_closure(
params: &Parameters,
Expand Down

0 comments on commit 0c8fa03

Please sign in to comment.