Skip to content

Commit 5e313a7

Browse files
committed
Move generiс code out-of-function, create more specialized and simple code
1 parent 2a36d11 commit 5e313a7

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

serde_derive/src/de.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,20 @@ fn prepare_enum_variant_enum(
12471247
}
12481248
};
12491249

1250+
let (ignore_variant, fallthrough) = if let Some(other_idx) = other_idx {
1251+
let ignore_variant = variant_names_idents[other_idx].1.clone();
1252+
let fallthrough = quote!(_serde::__private::Ok(__Field::#ignore_variant));
1253+
(None, Some(fallthrough))
1254+
} else {
1255+
(None, None)
1256+
};
1257+
12501258
let variant_visitor = Stmts(deserialize_generated_identifier(
12511259
&variant_names_idents,
12521260
cattrs,
12531261
true,
1254-
other_idx,
1262+
ignore_variant,
1263+
fallthrough,
12551264
));
12561265

12571266
(variants_stmt, variant_visitor)
@@ -1976,27 +1985,12 @@ fn deserialize_generated_identifier(
19761985
fields: &[(&str, Ident, &BTreeSet<String>)],
19771986
cattrs: &attr::Container,
19781987
is_variant: bool,
1979-
other_idx: Option<usize>,
1988+
ignore_variant: Option<TokenStream>,
1989+
fallthrough: Option<TokenStream>,
19801990
) -> Fragment {
19811991
let this_value = quote!(__Field);
19821992
let field_idents: &Vec<_> = &fields.iter().map(|(_, ident, _)| ident).collect();
19831993

1984-
let (ignore_variant, fallthrough) = if !is_variant && cattrs.has_flatten() {
1985-
let ignore_variant = quote!(__other(_serde::__private::de::Content<'de>),);
1986-
let fallthrough = quote!(_serde::__private::Ok(__Field::__other(__value)));
1987-
(Some(ignore_variant), Some(fallthrough))
1988-
} else if let Some(other_idx) = other_idx {
1989-
let ignore_variant = fields[other_idx].1.clone();
1990-
let fallthrough = quote!(_serde::__private::Ok(__Field::#ignore_variant));
1991-
(None, Some(fallthrough))
1992-
} else if is_variant || cattrs.deny_unknown_fields() {
1993-
(None, None)
1994-
} else {
1995-
let ignore_variant = quote!(__ignore,);
1996-
let fallthrough = quote!(_serde::__private::Ok(__Field::__ignore));
1997-
(Some(ignore_variant), Some(fallthrough))
1998-
};
1999-
20001994
let visitor_impl = Stmts(deserialize_identifier(
20011995
&this_value,
20021996
fields,
@@ -2048,11 +2042,24 @@ fn deserialize_field_identifier(
20482042
fields: &[(&str, Ident, &BTreeSet<String>)],
20492043
cattrs: &attr::Container,
20502044
) -> Stmts {
2045+
let (ignore_variant, fallthrough) = if cattrs.has_flatten() {
2046+
let ignore_variant = quote!(__other(_serde::__private::de::Content<'de>),);
2047+
let fallthrough = quote!(_serde::__private::Ok(__Field::__other(__value)));
2048+
(Some(ignore_variant), Some(fallthrough))
2049+
} else if cattrs.deny_unknown_fields() {
2050+
(None, None)
2051+
} else {
2052+
let ignore_variant = quote!(__ignore,);
2053+
let fallthrough = quote!(_serde::__private::Ok(__Field::__ignore));
2054+
(Some(ignore_variant), Some(fallthrough))
2055+
};
2056+
20512057
Stmts(deserialize_generated_identifier(
20522058
fields,
20532059
cattrs,
20542060
false,
2055-
None,
2061+
ignore_variant,
2062+
fallthrough,
20562063
))
20572064
}
20582065

0 commit comments

Comments
 (0)