From a020cceed8f6ef970d906de193b657f92ae2b069 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 24 Dec 2016 12:18:02 -0500 Subject: [PATCH] Simplify variant serialize patterns --- serde_codegen/src/ser.rs | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/serde_codegen/src/ser.rs b/serde_codegen/src/ser.rs index 0fc928af..c4dbc395 100644 --- a/serde_codegen/src/ser.rs +++ b/serde_codegen/src/ser.rs @@ -266,40 +266,17 @@ fn serialize_variant( match variant.style { Style::Unit => { quote! { - #type_ident::#variant_ident => #skipped_err, + #type_ident::#variant_ident => #skipped_err, } }, - Style::Newtype => { + Style::Newtype | Style::Tuple => { quote! { - #type_ident::#variant_ident(ref __simple_value) => #skipped_err, + #type_ident::#variant_ident(..) => #skipped_err, } }, - Style::Tuple => { - let field_names: Vec = (0 .. variant.fields.len()) - .map(|i| { - let id = aster::id(format!("__field{}", i)); - quote!(ref #id) - }) - .collect(); - - let pat = quote!(#type_ident::#variant_ident(#(#field_names),*)); - - quote! { - #pat => #skipped_err, - } - } Style::Struct => { - let fields = variant.fields.iter().map(|field| { - let id = match field.ident { - Some(ref name) => name.clone(), - None => panic!("struct variant has unnamed fields"), - }; - quote!(ref #id) - }); - let pat = quote!(#type_ident::#variant_ident { #(#fields),* }); - quote! { - #pat => #skipped_err, + #type_ident::#variant_ident{..} => #skipped_err, } } } @@ -338,8 +315,6 @@ fn serialize_variant( }) .collect(); - let pat = quote!(#type_ident::#variant_ident(#(#field_names),*)); - let block = serialize_tuple_variant( type_name, variant_index, @@ -350,7 +325,7 @@ fn serialize_variant( ); quote! { - #pat => { #block } + #type_ident::#variant_ident(#(#field_names),*) => { #block } } } Style::Struct => { @@ -361,7 +336,6 @@ fn serialize_variant( }; quote!(ref #id) }); - let pat = quote!(#type_ident::#variant_ident { #(#fields),* }); let block = serialize_struct_variant( variant_index, @@ -373,7 +347,7 @@ fn serialize_variant( ); quote! { - #pat => { #block } + #type_ident::#variant_ident { #(#fields),* } => { #block } } } }