From 9ea8899aef9114eb81ad568a2113a70639670a1c Mon Sep 17 00:00:00 2001 From: Mingun Date: Tue, 30 May 2023 00:18:15 +0500 Subject: [PATCH] Change Tuple2as1(Skipped, x) representation: tuple(1) -> newtype This matches the Tuple1(x) behavior which should be used Fixed (1): tuple_struct::tuple2as1 Changes in generated code (see the file attached to PR): Tuple2as1: Tuple2as1Default: Tuple2as1With: *_tuple_struct(1) -> *_newtype_struct --- serde_derive/src/de.rs | 10 +++------- serde_derive/src/ser.rs | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 5993a60af0..88fe62525b 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -505,8 +505,6 @@ fn deserialize_tuple( }; let expecting = cattrs.expecting().unwrap_or(&expecting); - let nfields = fields.len(); - let visit_newtype_struct = match form { TupleForm::Tuple if field_count == 1 => { let visit_newtype_struct = Stmts(read_fields_in_order( @@ -564,7 +562,7 @@ fn deserialize_tuple( } }; let dispatch = match form { - TupleForm::Tuple if field_count != 0 && nfields == 1 => { + TupleForm::Tuple if field_count == 1 => { let type_name = cattrs.name().deserialize_name(); quote! { _serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr) @@ -646,8 +644,6 @@ fn deserialize_tuple_in_place( }; let expecting = cattrs.expecting().unwrap_or(&expecting); - let nfields = fields.len(); - let visit_newtype_struct = if !is_enum && field_count == 1 { // We deserialize newtype, so only one field is not skipped let index = fields @@ -660,7 +656,7 @@ fn deserialize_tuple_in_place( }; // Deserialize and write defaults if at least one field is skipped, // otherwise only deserialize - if nfields > 1 { + if fields.len() > 1 { let write_defaults = fields.iter().enumerate().filter_map(|(index, field)| { if field.attrs.skip_deserializing() { let index = Index::from(index); @@ -708,7 +704,7 @@ fn deserialize_tuple_in_place( quote!(_serde::Deserializer::deserialize_tuple(#deserializer, #field_count, #visitor_expr)) } else if is_enum { quote!(_serde::de::VariantAccess::tuple_variant(__variant, #field_count, #visitor_expr)) - } else if field_count != 0 && nfields == 1 { + } else if field_count == 1 { let type_name = cattrs.name().deserialize_name(); quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr)) } else { diff --git a/serde_derive/src/ser.rs b/serde_derive/src/ser.rs index 937ff11691..3f0470e643 100644 --- a/serde_derive/src/ser.rs +++ b/serde_derive/src/ser.rs @@ -176,7 +176,7 @@ fn serialize_body(cont: &Container, params: &Parameters) -> Fragment { serialize_tuple_struct(params, fields, &cont.attrs) } Data::Struct(Style::Newtype, fields) => { - serialize_newtype_struct(params, &fields[0], &cont.attrs) + serialize_newtype_struct(params, 0, &fields[0], &cont.attrs) } Data::Struct(Style::Unit, _) => serialize_unit_struct(&cont.attrs), } @@ -225,6 +225,7 @@ fn serialize_unit_struct(cattrs: &attr::Container) -> Fragment { fn serialize_newtype_struct( params: &Parameters, + index: usize, field: &Field, cattrs: &attr::Container, ) -> Fragment { @@ -235,14 +236,7 @@ fn serialize_newtype_struct( let type_name = cattrs.name().serialize_name(); - let mut field_expr = get_member( - params, - field, - &Member::Unnamed(Index { - index: 0, - span: Span::call_site(), - }), - ); + let mut field_expr = get_member(params, field, &Member::Unnamed(Index::from(index))); if let Some(path) = field.attrs.serialize_with() { field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr); } @@ -270,6 +264,14 @@ fn serialize_tuple_struct( .filter(|(_, field)| !field.attrs.skip_serializing()) .peekable(); + match serialized_fields.clone().count() { + 1 => { + let (index, field) = serialized_fields.next().unwrap(); + return serialize_newtype_struct(params, index, field, cattrs); + } + _ => {} + } + let let_mut = mut_if(serialized_fields.peek().is_some()); let len = serialized_fields