Skip to content

Commit

Permalink
Change Tuple2as1(Skipped, x) representation: tuple(1) -> newtype
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mingun committed Jul 31, 2023
1 parent 654ea6d commit 1eac41c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
10 changes: 3 additions & 7 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions serde_derive/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1eac41c

Please sign in to comment.