From 795261919f159249a59c92f16bc3c2ed0b5f9c7c Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 1 May 2023 02:08:10 +0500 Subject: [PATCH] Generate `visit_seq` only when needed --- serde_derive/src/de.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 2dfe87ef..c2b01ca1 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -942,10 +942,6 @@ fn deserialize_struct( }; let expecting = cattrs.expecting().unwrap_or(&expecting); - let visit_seq = Stmts(deserialize_seq( - &type_path, params, fields, true, cattrs, expecting, - )); - let (field_visitor, fields_stmt, visit_map) = if cattrs.has_flatten() { deserialize_struct_as_map_visitor(&type_path, params, fields, cattrs) } else { @@ -985,25 +981,31 @@ fn deserialize_struct( } }; - let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing()); - let visitor_var = if all_skipped { - quote!(_) - } else { - quote!(mut __seq) - }; - // untagged struct variants do not get a visit_seq method. The same applies to // structs that only have a map representation. let visit_seq = match *untagged { - Untagged::No if !cattrs.has_flatten() => Some(quote! { - #[inline] - fn visit_seq<__A>(self, #visitor_var: __A) -> _serde::__private::Result - where - __A: _serde::de::SeqAccess<#delife>, - { - #visit_seq - } - }), + Untagged::No if !cattrs.has_flatten() => { + let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing()); + let mut_seq = if all_skipped { + quote!(_) + } else { + quote!(mut __seq) + }; + + let visit_seq = Stmts(deserialize_seq( + &type_path, params, fields, true, cattrs, expecting, + )); + + Some(quote! { + #[inline] + fn visit_seq<__A>(self, #mut_seq: __A) -> _serde::__private::Result + where + __A: _serde::de::SeqAccess<#delife>, + { + #visit_seq + } + }) + } _ => None, };