Skip to content

Commit

Permalink
Change Tuple1as0(Skipped) representation: newtype -> tuple(0), simila…
Browse files Browse the repository at this point in the history
…r to Tuple0() struct

A side-effect: Tuple2as1(Skipped, x) now also can be deserialized using visit_newtype_struct

Changes in generated code (see the file attached to PR):
  Tuple1as0:
  Tuple1as0Default:
  Tuple1as0With:
    removed visit_newtype_struct, *_newtype_struct -> *_tuple_struct(0)

  Tuple2as1:
  Tuple2as1Default:
  Tuple2as1With:
    added visit_newtype_struct

This commit fixes compilation error and actually fixes the issue as it was reported in serde-rs#2105
  • Loading branch information
Mingun committed Aug 2, 2023
1 parent b3b9d93 commit e383366
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
37 changes: 15 additions & 22 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ fn deserialize_tuple(
let nfields = fields.len();

let visit_newtype_struct = match form {
TupleForm::Tuple if nfields == 1 => {
TupleForm::Tuple if field_count == 1 => {
let visit_newtype_struct = Stmts(read_fields_in_order(
&type_path,
params,
Expand Down Expand Up @@ -564,7 +564,7 @@ fn deserialize_tuple(
}
};
let dispatch = match form {
TupleForm::Tuple if nfields == 1 => {
TupleForm::Tuple if field_count != 0 && nfields == 1 => {
let type_name = cattrs.name().deserialize_name();
quote! {
_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr)
Expand Down Expand Up @@ -642,34 +642,27 @@ fn deserialize_tuple_in_place(

let nfields = fields.len();

let visit_newtype_struct = if nfields == 1 {
let visit_newtype_struct = if field_count == 1 {
// We deserialize newtype, so only one field is not skipped
let index = fields
.iter()
.position(|field| !field.attrs.skip_deserializing())
.unwrap_or(0);
let index = Index::from(index);
.map(Index::from)
.unwrap();
let mut deserialize = quote! {
_serde::Deserialize::deserialize_in_place(__e, &mut self.place.#index)
};
let write_defaults = fields.iter().enumerate().filter_map(|(index, field)| {
if field.attrs.skip_deserializing() {
let index = Index::from(index);
let default = Expr(expr_is_missing(field, cattrs));
return Some(quote!(self.place.#index = #default;));
}
None
});
// If there are no deserialized fields, write only defaults
if field_count == 0 {
deserialize = quote! {
#(#write_defaults)*
_serde::__private::Ok(())
}
} else
// Deserialize and write defaults if at least one field is skipped,
// otherwise only deserialize
if nfields > field_count {
if nfields > 1 {
let write_defaults = fields.iter().enumerate().filter_map(|(index, field)| {
if field.attrs.skip_deserializing() {
let index = Index::from(index);
let default = Expr(expr_is_missing(field, cattrs));
return Some(quote!(self.place.#index = #default;));
}
None
});
deserialize = quote! {
match #deserialize {
_serde::__private::Ok(_) => {
Expand Down Expand Up @@ -706,7 +699,7 @@ fn deserialize_tuple_in_place(
};

let type_name = cattrs.name().deserialize_name();
let dispatch = if nfields == 1 {
let dispatch = if field_count != 0 && nfields == 1 {
quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr))
} else {
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #field_count, #visitor_expr))
Expand Down
5 changes: 5 additions & 0 deletions serde_derive/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ fn serialize_newtype_struct(
field: &Field,
cattrs: &attr::Container,
) -> Fragment {
// For `struct Tuple1as0(#[serde(skip)] u8);` cases
if field.attrs.skip_serializing() {
return serialize_tuple_struct(params, &[], cattrs);
}

let type_name = cattrs.name().serialize_name();

let mut field_expr = get_member(
Expand Down
3 changes: 1 addition & 2 deletions test_suite/tests/test_skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ mod tuple_struct {
);
}

/* FIXME: compilation error: https://github.com/serde-rs/serde/issues/2105
#[test]
fn tuple1as0() {
/// This newtype struct in the serialized form the same as `struct Tuple0();`
Expand All @@ -107,7 +106,7 @@ mod tuple_struct {
Token::TupleStructEnd,
],
);
}*/
}

#[test]
fn tuple2as0() {
Expand Down

0 comments on commit e383366

Please sign in to comment.