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 21, 2023
1 parent f57e5fc commit bbd264b
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 @@ -510,8 +510,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 @@ -569,7 +567,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 @@ -651,8 +649,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 @@ -665,7 +661,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 @@ -713,7 +709,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 @@ -179,7 +179,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 @@ -228,6 +228,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 @@ -238,14 +239,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 @@ -273,6 +267,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 bbd264b

Please sign in to comment.