diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index ef129821..c80845ed 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -941,9 +941,50 @@ fn deserialize_struct( let expecting = cattrs.expecting().unwrap_or(&expecting); let (field_visitor, fields_stmt) = if cattrs.has_flatten() { - deserialize_struct_as_map_visitor(fields, cattrs) + let field_names_idents: Vec<_> = fields + .iter() + .enumerate() + .filter(|&(_, field)| !field.attrs.skip_deserializing() && !field.attrs.flatten()) + .map(|(i, field)| { + ( + field.attrs.name().deserialize_name(), + field_i(i), + field.attrs.aliases(), + ) + }) + .collect(); + + let field_visitor = deserialize_generated_identifier(&field_names_idents, cattrs, false, None); + + (field_visitor, None) } else { - deserialize_struct_as_struct_visitor(fields, cattrs) + let field_names_idents: Vec<_> = fields + .iter() + .enumerate() + .filter(|&(_, field)| !field.attrs.skip_deserializing()) + .map(|(i, field)| { + ( + field.attrs.name().deserialize_name(), + field_i(i), + field.attrs.aliases(), + ) + }) + .collect(); + + let fields_stmt = { + let field_names = field_names_idents + .iter() + .flat_map(|(_, _, aliases)| aliases); + + quote_block! { + #[doc(hidden)] + const FIELDS: &'static [&'static str] = &[ #(#field_names),* ]; + } + }; + + let field_visitor = deserialize_generated_identifier(&field_names_idents, cattrs, false, None); + + (field_visitor, Some(fields_stmt)) }; let field_visitor = Stmts(field_visitor); let fields_stmt = fields_stmt.map(Stmts); @@ -2424,63 +2465,6 @@ fn deserialize_identifier( } } -fn deserialize_struct_as_struct_visitor( - fields: &[Field], - cattrs: &attr::Container, -) -> (Fragment, Option) { - assert!(!cattrs.has_flatten()); - - let field_names_idents: Vec<_> = fields - .iter() - .enumerate() - .filter(|&(_, field)| !field.attrs.skip_deserializing()) - .map(|(i, field)| { - ( - field.attrs.name().deserialize_name(), - field_i(i), - field.attrs.aliases(), - ) - }) - .collect(); - - let fields_stmt = { - let field_names = field_names_idents - .iter() - .flat_map(|(_, _, aliases)| aliases); - - quote_block! { - #[doc(hidden)] - const FIELDS: &'static [&'static str] = &[ #(#field_names),* ]; - } - }; - - let field_visitor = deserialize_generated_identifier(&field_names_idents, cattrs, false, None); - - (field_visitor, Some(fields_stmt)) -} - -fn deserialize_struct_as_map_visitor( - fields: &[Field], - cattrs: &attr::Container, -) -> (Fragment, Option) { - let field_names_idents: Vec<_> = fields - .iter() - .enumerate() - .filter(|&(_, field)| !field.attrs.skip_deserializing() && !field.attrs.flatten()) - .map(|(i, field)| { - ( - field.attrs.name().deserialize_name(), - field_i(i), - field.attrs.aliases(), - ) - }) - .collect(); - - let field_visitor = deserialize_generated_identifier(&field_names_idents, cattrs, false, None); - - (field_visitor, None) -} - fn deserialize_map( struct_path: &TokenStream, params: &Parameters,