@@ -281,21 +281,11 @@ fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
281281 } else if let attr:: Identifier :: No = cont. attrs . identifier ( ) {
282282 match & cont. data {
283283 Data :: Enum ( variants) => deserialize_enum ( params, variants, & cont. attrs ) ,
284- Data :: Struct ( Style :: Struct , fields) => deserialize_struct (
285- params,
286- fields,
287- & cont. attrs ,
288- cont. attrs . has_flatten ( ) ,
289- StructForm :: Struct ,
290- ) ,
284+ Data :: Struct ( Style :: Struct , fields) => {
285+ deserialize_struct ( params, fields, & cont. attrs , StructForm :: Struct )
286+ }
291287 Data :: Struct ( Style :: Tuple , fields) | Data :: Struct ( Style :: Newtype , fields) => {
292- deserialize_tuple (
293- params,
294- fields,
295- & cont. attrs ,
296- cont. attrs . has_flatten ( ) ,
297- TupleForm :: Tuple ,
298- )
288+ deserialize_tuple ( params, fields, & cont. attrs , TupleForm :: Tuple )
299289 }
300290 Data :: Struct ( Style :: Unit , _) => deserialize_unit_struct ( params, & cont. attrs ) ,
301291 }
@@ -469,11 +459,10 @@ fn deserialize_tuple(
469459 params : & Parameters ,
470460 fields : & [ Field ] ,
471461 cattrs : & attr:: Container ,
472- has_flatten : bool ,
473462 form : TupleForm ,
474463) -> Fragment {
475464 assert ! (
476- !has_flatten,
465+ !has_flatten( fields ) ,
477466 "tuples and tuple variants cannot have flatten fields"
478467 ) ;
479468
@@ -594,7 +583,7 @@ fn deserialize_tuple_in_place(
594583 cattrs : & attr:: Container ,
595584) -> Fragment {
596585 assert ! (
597- !cattrs . has_flatten( ) ,
586+ !has_flatten( fields ) ,
598587 "tuples and tuple variants cannot have flatten fields"
599588 ) ;
600589
@@ -927,7 +916,6 @@ fn deserialize_struct(
927916 params : & Parameters ,
928917 fields : & [ Field ] ,
929918 cattrs : & attr:: Container ,
930- has_flatten : bool ,
931919 form : StructForm ,
932920) -> Fragment {
933921 let this_type = & params. this_type ;
@@ -976,6 +964,8 @@ fn deserialize_struct(
976964 )
977965 } )
978966 . collect ( ) ;
967+
968+ let has_flatten = has_flatten ( fields) ;
979969 let field_visitor = deserialize_field_identifier ( & field_names_idents, cattrs, has_flatten) ;
980970
981971 // untagged struct variants do not get a visit_seq method. The same applies to
@@ -1115,7 +1105,7 @@ fn deserialize_struct_in_place(
11151105) -> Option < Fragment > {
11161106 // for now we do not support in_place deserialization for structs that
11171107 // are represented as map.
1118- if cattrs . has_flatten ( ) {
1108+ if has_flatten ( fields ) {
11191109 return None ;
11201110 }
11211111
@@ -1831,14 +1821,12 @@ fn deserialize_externally_tagged_variant(
18311821 params,
18321822 & variant. fields ,
18331823 cattrs,
1834- variant. attrs . has_flatten ( ) ,
18351824 TupleForm :: ExternallyTagged ( variant_ident) ,
18361825 ) ,
18371826 Style :: Struct => deserialize_struct (
18381827 params,
18391828 & variant. fields ,
18401829 cattrs,
1841- variant. attrs . has_flatten ( ) ,
18421830 StructForm :: ExternallyTagged ( variant_ident) ,
18431831 ) ,
18441832 }
@@ -1882,7 +1870,6 @@ fn deserialize_internally_tagged_variant(
18821870 params,
18831871 & variant. fields ,
18841872 cattrs,
1885- variant. attrs . has_flatten ( ) ,
18861873 StructForm :: InternallyTagged ( variant_ident, deserializer) ,
18871874 ) ,
18881875 Style :: Tuple => unreachable ! ( "checked in serde_derive_internals" ) ,
@@ -1933,14 +1920,12 @@ fn deserialize_untagged_variant(
19331920 params,
19341921 & variant. fields ,
19351922 cattrs,
1936- variant. attrs . has_flatten ( ) ,
19371923 TupleForm :: Untagged ( variant_ident, deserializer) ,
19381924 ) ,
19391925 Style :: Struct => deserialize_struct (
19401926 params,
19411927 & variant. fields ,
19421928 cattrs,
1943- variant. attrs . has_flatten ( ) ,
19441929 StructForm :: Untagged ( variant_ident, deserializer) ,
19451930 ) ,
19461931 }
@@ -2707,7 +2692,7 @@ fn deserialize_map_in_place(
27072692 cattrs : & attr:: Container ,
27082693) -> Fragment {
27092694 assert ! (
2710- !cattrs . has_flatten( ) ,
2695+ !has_flatten( fields ) ,
27112696 "inplace deserialization of maps does not support flatten fields"
27122697 ) ;
27132698
@@ -3042,6 +3027,13 @@ fn effective_style(variant: &Variant) -> Style {
30423027 }
30433028}
30443029
3030+ /// True if there are fields that is not skipped and has a `#[serde(flatten)]` attribute.
3031+ fn has_flatten ( fields : & [ Field ] ) -> bool {
3032+ fields
3033+ . iter ( )
3034+ . any ( |field| field. attrs . flatten ( ) && !field. attrs . skip_deserializing ( ) )
3035+ }
3036+
30453037struct DeImplGenerics < ' a > ( & ' a Parameters ) ;
30463038#[ cfg( feature = "deserialize_in_place" ) ]
30473039struct InPlaceImplGenerics < ' a > ( & ' a Parameters ) ;
0 commit comments