diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index b7edf0b8e..421ca938d 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -771,19 +771,26 @@ fn read_fields_in_order( let expecting = cattrs.expecting().unwrap_or(&expecting); let mut index_in_seq = 0_usize; - let let_values = vars.clone().zip(fields).map(|(var, field)| { - if field.attrs.skip_deserializing() { - let default = Expr(expr_is_missing(field, cattrs)); - quote! { - let #var = #default; - } - } else { + let let_values = vars.clone().zip(fields).filter_map(|(var, field)| { + if !field.attrs.skip_deserializing() { let read = read_field(params, index_in_seq, field, expecting); index_in_seq += 1; - quote! { + + return Some(quote! { let #var = #read; - } + }); + } + None + }); + let let_skipped = vars.clone().zip(fields).filter_map(|(var, field)| { + if field.attrs.skip_deserializing() { + let default = Expr(expr_is_missing(field, cattrs)); + + return Some(quote! { + let #var = #default; + }); } + None }); let mut result = if is_struct { @@ -822,6 +829,7 @@ fn read_fields_in_order( quote_block! { #let_default #(#let_values)* + #(#let_skipped)* _serde::__private::Ok(#result) } } @@ -890,15 +898,9 @@ fn read_fields_in_order_in_place( let expecting = cattrs.expecting().unwrap_or(&expecting); let mut index_in_seq = 0usize; - let write_values = fields.iter().map(|field| { - let member = &field.member; - - if field.attrs.skip_deserializing() { - let default = Expr(expr_is_missing(field, cattrs)); - quote! { - self.place.#member = #default; - } - } else { + let write_values = fields.iter().filter_map(|field| { + if !field.attrs.skip_deserializing() { + let member = &field.member; let value_if_none = match field.attrs.default() { attr::Default::Default => quote!( self.place.#member = _serde::__private::Default::default(); @@ -936,8 +938,20 @@ fn read_fields_in_order_in_place( } }; index_in_seq += 1; - write + return Some(write); } + None + }); + let write_skipped = fields.iter().filter_map(|field| { + if field.attrs.skip_deserializing() { + let member = &field.member; + let default = Expr(expr_is_missing(field, cattrs)); + + return Some(quote! { + self.place.#member = #default; + }); + } + None }); let this_type = ¶ms.this_type; @@ -959,6 +973,7 @@ fn read_fields_in_order_in_place( quote_block! { #let_default #(#write_values)* + #(#write_skipped)* _serde::__private::Ok(()) } }