Correctly calculate `has_flatten` attribute in all cases for deserialization
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Consequence: `FlattenSkipDeserializing[DenyUnknown]` - does not collect data in Field, because do not read them anyway - gets `deserialize_in_place` method - gets ability to deserialize from sequence (visit_seq method) - uses `deserialize_struct` instead of `deserialize_map`
authored by Mingun · committed by Mingun · parent 0647a7c · Browse files · 3 files·+13 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+13 added−39 removed
- other:+13 added−39 removed
Provenance
- Commit
fd5b5e9- Tree
6fc9ea4- Author
Mingun <Alexander_Sergey@mail.ru>- Committer
Mingun <Alexander_Sergey@mail.ru>- Parent
0647a7c
3 files changed+13 added−39 removed
modified:serde_derive/src/internals/ast.rs-1 removed
83 unmodified lines84 variant.attrs.rename_by_rules(attrs.rename_all_rules());85 for field in &mut variant.fields {86 if field.attrs.flatten() {87 has_flatten = true;88- variant.attrs.mark_has_flatten();88 }89 field.attrs.rename_by_rules(90 variant91 .attrs125 unmodified linesmodified:serde_derive/src/internals/attr.rs-21 removed
808 unmodified lines809 name: Name,810 rename_all_rules: RenameAllRules,811 ser_bound: Option<Vec<syn::WherePredicate>>,812 de_bound: Option<Vec<syn::WherePredicate>>,813- /// True if variant is a struct variant which contains a field with814- /// `#[serde(flatten)]`.815- ///816- /// ```ignore817- /// enum Enum {818- /// Variant {819- /// #[serde(flatten)]820- /// some_field: (),821- /// },822- /// }823- /// ```824- has_flatten: bool,813 skip_deserializing: bool,814 skip_serializing: bool,815 other: bool,816 serialize_with: Option<syn::ExprPath>,161 unmodified lines978 deserialize: rename_all_de_rule.get().unwrap_or(RenameRule::None),979 },980 ser_bound: ser_bound.get(),981 de_bound: de_bound.get(),994- has_flatten: false,982 skip_deserializing: skip_deserializing.get(),983 skip_serializing: skip_serializing.get(),984 other: other.get(),985 serialize_with: serialize_with.get(),34 unmodified lines1020 pub fn de_bound(&self) -> Option<&[syn::WherePredicate]> {1021 self.de_bound.as_ref().map(|vec| &vec[..])1022 }1023 1037- pub fn has_flatten(&self) -> bool {1038- self.has_flatten1039- }1040-1041- pub fn mark_has_flatten(&mut self) {1042- self.has_flatten = true;1043- }1044-1024 pub fn skip_deserializing(&self) -> bool {1025 self.skip_deserializing1026 }1027 870 unmodified linesmodified:serde_derive/src/de.rs+13 added-17 removed
283 unmodified lines284 Data::Struct(Style::Struct, fields) => {285 deserialize_struct(params, fields, &cont.attrs, StructForm::Struct)286 }287 Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => {288- deserialize_tuple(289- params,290- fields,291- &cont.attrs,292- cont.attrs.has_flatten(),293- TupleForm::Tuple,294- )288+ deserialize_tuple(params, fields, &cont.attrs, TupleForm::Tuple)289 }290 Data::Struct(Style::Unit, _) => deserialize_unit_struct(params, &cont.attrs),291 }292 } else {165 unmodified lines458 fn deserialize_tuple(459 params: &Parameters,460 fields: &[Field],461 cattrs: &attr::Container,468- has_flatten: bool,462 form: TupleForm,463 ) -> Fragment {464 assert!(472- !has_flatten,465+ !has_flatten(fields),466 "tuples and tuple variants cannot have flatten fields"467 );468 469 let field_count = fields112 unmodified lines582 fields: &[Field],583 cattrs: &attr::Container,584 ) -> Fragment {585 assert!(593- !cattrs.has_flatten(),586+ !has_flatten(fields),587 "tuples and tuple variants cannot have flatten fields"588 );589 590 let field_count = fields373 unmodified lines964 )965 })966 .collect();967 975- let has_flatten = fields976- .iter()977- .any(|field| field.attrs.flatten() && !field.attrs.skip_deserializing());968+ let has_flatten = has_flatten(fields);969 let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs, has_flatten);970 971 // untagged struct variants do not get a visit_seq method. The same applies to972 // structs that only have a map representation.131 unmodified lines1104 cattrs: &attr::Container,1105 ) -> Option<Fragment> {1106 // for now we do not support in_place deserialization for structs that1107 // are represented as map.1117- if cattrs.has_flatten() {1108+ if has_flatten(fields) {1109 return None;1110 }1111 1112 let this_type = ¶ms.this_type;707 unmodified lines1820 Style::Tuple => deserialize_tuple(1821 params,1822 &variant.fields,1823 cattrs,1833- variant.attrs.has_flatten(),1824 TupleForm::ExternallyTagged(variant_ident),1825 ),1826 Style::Struct => deserialize_struct(1827 params,91 unmodified lines1919 Style::Tuple => deserialize_tuple(1920 params,1921 &variant.fields,1922 cattrs,1933- variant.attrs.has_flatten(),1923 TupleForm::Untagged(variant_ident, deserializer),1924 ),1925 Style::Struct => deserialize_struct(1926 params,764 unmodified lines2691 fields: &[Field],2692 cattrs: &attr::Container,2693 ) -> Fragment {2694 assert!(2706- !cattrs.has_flatten(),2695+ !has_flatten(fields),2696 "inplace deserialization of maps does not support flatten fields"2697 );2698 2699 // Create the field names for the fields.326 unmodified lines3026 other => other,3027 }3028 }3029 3030+/// True if there are fields that is not skipped and has a `#[serde(flatten)]` attribute.3031+fn has_flatten(fields: &[Field]) -> bool {3032+ fields3033+ .iter()3034+ .any(|field| field.attrs.flatten() && !field.attrs.skip_deserializing())3035+}3036+3037 struct DeImplGenerics<'a>(&'a Parameters);3038 #[cfg(feature = "deserialize_in_place")]3039 struct InPlaceImplGenerics<'a>(&'a Parameters);3040 131 unmodified lines