From 397e8716d4b53a67063621920a414b0992bfee28 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 10:46:04 +0500 Subject: [PATCH 01/15] Explain code in ContentDeserializer::deserialize_newtype_struct and add comments about coverage --- serde/src/private/de.rs | 13 +++++++++++++ test_suite/tests/test_enum_adjacently_tagged.rs | 2 ++ test_suite/tests/test_enum_internally_tagged.rs | 2 ++ 3 files changed, 17 insertions(+) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 50ae6ed15..5a129b0a5 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1373,7 +1373,20 @@ mod content { V: Visitor<'de>, { match self.content { + // Covered by tests/test_enum_adjacently_tagged.rs + // newtype_with_newtype Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)), + // Covered by tests/test_enum_internally_tagged.rs + // newtype_newtype + // This case is to support data formats that encode newtype + // structs and their underlying data the same, with no + // indication whether a newtype wrapper was present. For example + // JSON does this, while RON does not. In RON a newtype's name + // is included in the serialized representation and it knows to + // call `Visitor::visit_newtype_struct` from `deserialize_any`. + // JSON's `deserialize_any` never calls `visit_newtype_struct` + // but in this code we still must be able to deserialize the + // resulting Content into newtypes. _ => visitor.visit_newtype_struct(self), } } diff --git a/test_suite/tests/test_enum_adjacently_tagged.rs b/test_suite/tests/test_enum_adjacently_tagged.rs index 0bcdbc39e..beddff45f 100644 --- a/test_suite/tests/test_enum_adjacently_tagged.rs +++ b/test_suite/tests/test_enum_adjacently_tagged.rs @@ -410,6 +410,8 @@ mod newtype { } } +// Reaches crate::private::de::content::ContentDeserializer::deserialize_newtype_struct +// in Content::Newtype case #[test] fn newtype_with_newtype() { #[derive(Debug, PartialEq, Serialize, Deserialize)] diff --git a/test_suite/tests/test_enum_internally_tagged.rs b/test_suite/tests/test_enum_internally_tagged.rs index b4d428c4d..12a0e48b1 100644 --- a/test_suite/tests/test_enum_internally_tagged.rs +++ b/test_suite/tests/test_enum_internally_tagged.rs @@ -225,6 +225,8 @@ fn newtype_unit_struct() { ); } +// Reaches crate::private::de::content::ContentDeserializer::deserialize_newtype_struct +// in _ case #[test] fn newtype_newtype() { assert_tokens( From 90564e60092926a15542cdd552343833280b1f79 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 10:18:24 +0500 Subject: [PATCH 02/15] Cover all cases in ContentDeserializer::deserialize_option --- serde/src/private/de.rs | 9 ++++ test_suite/tests/test_annotations.rs | 77 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 5a129b0a5..2f4687c96 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1306,10 +1306,19 @@ mod content { where V: Visitor<'de>, { + // Covered by tests/test_annotations.rs + // flatten::with_optional_field::* match self.content { Content::None => visitor.visit_none(), Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)), Content::Unit => visitor.visit_unit(), + // This case is to support data formats which do not encode an + // indication whether a value is optional. An example of such a + // format is JSON, and a counterexample is RON. When requesting + // `deserialize_any` in JSON, the data format never performs + // `Visitor::visit_some` but we still must be able to + // deserialize the resulting Content into data structures with + // optional fields. _ => visitor.visit_some(self), } } diff --git a/test_suite/tests/test_annotations.rs b/test_suite/tests/test_annotations.rs index 685bf329d..5a93c9ac8 100644 --- a/test_suite/tests/test_annotations.rs +++ b/test_suite/tests/test_annotations.rs @@ -2974,4 +2974,81 @@ mod flatten { } } } + + // Reaches crate::private::de::content::ContentDeserializer::deserialize_option + mod with_optional_field { + use super::*; + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct Outer { + #[serde(flatten)] + inner: Inner, + } + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct Inner { + optional: Option, + } + + #[test] + fn some() { + assert_tokens( + &Outer { + inner: Inner { optional: Some(42) }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::Some, + Token::U32(42), + Token::MapEnd, + ], + ); + } + + #[test] + fn some_without_marker() { + assert_de_tokens( + &Outer { + inner: Inner { optional: Some(42) }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::U32(42), + Token::MapEnd, + ], + ); + } + + #[test] + fn none() { + assert_tokens( + &Outer { + inner: Inner { optional: None }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::None, + Token::MapEnd, + ], + ); + } + + #[test] + fn unit() { + assert_de_tokens( + &Outer { + inner: Inner { optional: None }, + }, + &[ + Token::Map { len: None }, + Token::Str("optional"), + Token::Unit, + Token::MapEnd, + ], + ); + } + } } From a1e4ce79b34f308014d29f8edffdf61496f63874 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 12:25:10 +0500 Subject: [PATCH 03/15] Move test forgotten in 2cbfd37072a5af4774a77c8d3f7661dd3c1af194 due to resolving conflicts --- test_suite/tests/test_annotations.rs | 30 ------------------- .../tests/test_enum_internally_tagged.rs | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/test_suite/tests/test_annotations.rs b/test_suite/tests/test_annotations.rs index 5a93c9ac8..3b64c8a15 100644 --- a/test_suite/tests/test_annotations.rs +++ b/test_suite/tests/test_annotations.rs @@ -1763,36 +1763,6 @@ fn test_partially_untagged_enum_desugared() { ); } -#[test] -fn test_partially_untagged_internally_tagged_enum() { - #[derive(Serialize, Deserialize, PartialEq, Debug)] - #[serde(tag = "t")] - enum Data { - A, - B, - #[serde(untagged)] - Var(u32), - } - - let data = Data::A; - - assert_de_tokens( - &data, - &[ - Token::Map { len: None }, - Token::Str("t"), - Token::Str("A"), - Token::MapEnd, - ], - ); - - let data = Data::Var(42); - - assert_de_tokens(&data, &[Token::U32(42)]); - - // TODO test error output -} - #[test] fn test_transparent_struct() { #[derive(Serialize, Deserialize, PartialEq, Debug)] diff --git a/test_suite/tests/test_enum_internally_tagged.rs b/test_suite/tests/test_enum_internally_tagged.rs index 12a0e48b1..7214af450 100644 --- a/test_suite/tests/test_enum_internally_tagged.rs +++ b/test_suite/tests/test_enum_internally_tagged.rs @@ -1066,6 +1066,36 @@ fn wrong_tag() { ); } +#[test] +fn partially_untagged() { + #[derive(Debug, PartialEq, Serialize, Deserialize)] + #[serde(tag = "tag")] + enum Data { + A, + B, + #[serde(untagged)] + Var(u32), + } + + let data = Data::A; + + assert_de_tokens( + &data, + &[ + Token::Map { len: None }, + Token::Str("tag"), + Token::Str("A"), + Token::MapEnd, + ], + ); + + let data = Data::Var(42); + + assert_de_tokens(&data, &[Token::U32(42)]); + + // TODO test error output +} + #[test] fn untagged_variant() { #[derive(Debug, PartialEq, Serialize, Deserialize)] From 6929c5dfd10b9a568f83c478d1cae8679d9bd64e Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 11 Aug 2024 02:06:52 +0500 Subject: [PATCH 04/15] Remove confusing call to deserialize_untagged_variant in deserialize_internally_tagged_variant deserialize_untagged_variant in that place is called when deserialzie_with attribute is set. In that case it performs special actions that is better to use explicitly in deserialize_untagged_variant for readability --- serde_derive/src/de.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 518f84320..5f90dff3d 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1846,8 +1846,11 @@ fn deserialize_internally_tagged_variant( cattrs: &attr::Container, deserializer: TokenStream, ) -> Fragment { - if variant.attrs.deserialize_with().is_some() { - return deserialize_untagged_variant(params, variant, cattrs, deserializer); + if let Some(path) = variant.attrs.deserialize_with() { + let unwrap_fn = unwrap_to_variant_closure(params, variant, false); + return quote_block! { + _serde::__private::Result::map(#path(#deserializer), #unwrap_fn) + }; } let variant_ident = &variant.ident; From ec5f5908f9be45de9cf9c1ee155d321668bd0214 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 7 Aug 2023 21:48:28 +0500 Subject: [PATCH 05/15] Add test for sequence in newtype_unit failures(1): newtype_unit --- test_suite/tests/test_enum_internally_tagged.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test_suite/tests/test_enum_internally_tagged.rs b/test_suite/tests/test_enum_internally_tagged.rs index 7214af450..ed666ed98 100644 --- a/test_suite/tests/test_enum_internally_tagged.rs +++ b/test_suite/tests/test_enum_internally_tagged.rs @@ -157,6 +157,23 @@ fn newtype_unit() { Token::StructEnd, ], ); + + assert_de_tokens( + &value, + &[ + Token::Seq { len: Some(1) }, + Token::Str("NewtypeUnit"), // tag + Token::SeqEnd, + ], + ); + assert_de_tokens( + &value, + &[ + Token::Seq { len: Some(1) }, + Token::BorrowedStr("NewtypeUnit"), // tag + Token::SeqEnd, + ], + ); } #[test] From bcffa60daff9971f20f4bf992d9ddea2af8bf9f6 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 7 Aug 2023 23:20:53 +0500 Subject: [PATCH 06/15] Unify handling of unit and unit structs in ContentDeserializer --- serde/src/private/de.rs | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 2f4687c96..563575d5c 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1327,9 +1327,24 @@ mod content { where V: Visitor<'de>, { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_unit match self.content { - Content::Unit => visitor.visit_unit(), - + // As a special case, allow deserializing untagged newtype + // variant containing unit struct. + // + // #[derive(Deserialize)] + // struct Info; + // + // #[derive(Deserialize)] + // #[serde(tag = "topic")] + // enum Message { + // Info(Info), + // } + // + // We want {"topic":"Info"} to deserialize even though + // ordinarily unit structs do not deserialize from empty map/seq. + // // Allow deserializing newtype variant containing unit. // // #[derive(Deserialize)] @@ -1340,7 +1355,8 @@ mod content { // // We want {"result":"Success"} to deserialize into Response<()>. Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), - _ => Err(self.invalid_type(&visitor)), + Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(), + _ => self.deserialize_any(visitor), } } @@ -1352,25 +1368,9 @@ mod content { where V: Visitor<'de>, { - match self.content { - // As a special case, allow deserializing untagged newtype - // variant containing unit struct. - // - // #[derive(Deserialize)] - // struct Info; - // - // #[derive(Deserialize)] - // #[serde(tag = "topic")] - // enum Message { - // Info(Info), - // } - // - // We want {"topic":"Info"} to deserialize even though - // ordinarily unit structs do not deserialize from empty map/seq. - Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), - Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(), - _ => self.deserialize_any(visitor), - } + // Covered by tests/test_enum_internally_tagged.rs + // newtype_unit_struct + self.deserialize_unit(visitor) } fn deserialize_newtype_struct( From 5292da8b953b1acfeacd3c52f2d1ae4e23fd63c0 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 12 Aug 2024 01:41:12 +0500 Subject: [PATCH 07/15] Consistently use visit_content_seq and visit_content_map when deserialize enums Helper methods visit_content_[seq|map] does the same as [Seq|Map]Deserializer::deserialize_any and used everywhere except here. Reuse them for consistency --- serde/src/private/de.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 563575d5c..e4f0b3984 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1624,9 +1624,7 @@ mod content { V: de::Visitor<'de>, { match self.value { - Some(Content::Seq(v)) => { - de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor) - } + Some(Content::Seq(v)) => visit_content_seq(v, visitor), Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"tuple variant", @@ -1647,12 +1645,8 @@ mod content { V: de::Visitor<'de>, { match self.value { - Some(Content::Map(v)) => { - de::Deserializer::deserialize_any(MapDeserializer::new(v.into_iter()), visitor) - } - Some(Content::Seq(v)) => { - de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor) - } + Some(Content::Map(v)) => visit_content_map(v, visitor), + Some(Content::Seq(v)) => visit_content_seq(v, visitor), Some(other) => Err(de::Error::invalid_type( other.unexpected(), &"struct variant", From 9009f631eddfb15b9dba2726c08d79a4516791e6 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 12 Aug 2023 15:17:53 +0500 Subject: [PATCH 08/15] Deserializer should not check correctness of data, this is the responsibility of the Visitor Examples of errors produced during deserialization of internally tagged enums in tests if instead of a Seq/Map a Str("unexpected string") will be provided: In tests/test_annotations.rs flatten::enum_::internally_tagged::tuple: before: `invalid type: string "unexpected string", expected tuple variant` after : `invalid type: string "unexpected string", expected tuple variant Enum::Tuple` flatten::enum_::internally_tagged::struct_from_map: before: `invalid type: string "unexpected string", expected struct variant` after : `invalid type: string "unexpected string", expected struct variant Enum::Struct` --- serde/src/private/de.rs | 683 ++++------------------------------------ 1 file changed, 70 insertions(+), 613 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index e4f0b3984..316157aa3 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -209,10 +209,10 @@ mod content { use crate::lib::*; use crate::actually_private; - use crate::de::value::{MapDeserializer, SeqDeserializer}; + use crate::de::value::{MapDeserializer, SeqDeserializer, UnitDeserializer}; use crate::de::{ - self, size_hint, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected, - IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor, + self, size_hint, Deserialize, DeserializeSeed, Deserializer, EnumAccess, IgnoredAny, + MapAccess, SeqAccess, Unexpected, Visitor, }; /// Used from generated code to buffer the contents of the Deserializer when @@ -261,33 +261,6 @@ mod content { _ => None, } } - - #[cold] - fn unexpected(&self) -> Unexpected { - match *self { - Content::Bool(b) => Unexpected::Bool(b), - Content::U8(n) => Unexpected::Unsigned(n as u64), - Content::U16(n) => Unexpected::Unsigned(n as u64), - Content::U32(n) => Unexpected::Unsigned(n as u64), - Content::U64(n) => Unexpected::Unsigned(n), - Content::I8(n) => Unexpected::Signed(n as i64), - Content::I16(n) => Unexpected::Signed(n as i64), - Content::I32(n) => Unexpected::Signed(n as i64), - Content::I64(n) => Unexpected::Signed(n), - Content::F32(f) => Unexpected::Float(f as f64), - Content::F64(f) => Unexpected::Float(f), - Content::Char(c) => Unexpected::Char(c), - Content::String(ref s) => Unexpected::Str(s), - Content::Str(s) => Unexpected::Str(s), - Content::ByteBuf(ref b) => Unexpected::Bytes(b), - Content::Bytes(b) => Unexpected::Bytes(b), - Content::None | Content::Some(_) => Unexpected::Option, - Content::Unit => Unexpected::Unit, - Content::Newtype(_) => Unexpected::NewtypeStruct, - Content::Seq(_) => Unexpected::Seq, - Content::Map(_) => Unexpected::Map, - } - } } impl<'de> Deserialize<'de> for Content<'de> { @@ -1060,52 +1033,6 @@ mod content { err: PhantomData, } - impl<'de, E> ContentDeserializer<'de, E> - where - E: de::Error, - { - #[cold] - fn invalid_type(self, exp: &Expected) -> E { - de::Error::invalid_type(self.content.unexpected(), exp) - } - - fn deserialize_integer(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_float(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::F32(v) => visitor.visit_f32(v), - Content::F64(v) => visitor.visit_f64(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - } - fn visit_content_seq<'de, V, E>(content: Vec>, visitor: V) -> Result where V: Visitor<'de>, @@ -1139,6 +1066,12 @@ mod content { { type Error = E; + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf seq tuple + tuple_struct map struct identifier + } + fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1169,139 +1102,6 @@ mod content { } } - fn deserialize_bool(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Bool(v) => visitor.visit_bool(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_i8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_f32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_f64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_char(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Char(v) => visitor.visit_char(v), - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_str(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_string(visitor) - } - - fn deserialize_string(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(v) => visitor.visit_byte_buf(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_bytes(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_byte_buf(visitor) - } - - fn deserialize_byte_buf(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(v) => visitor.visit_byte_buf(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::Seq(v) => visit_content_seq(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1400,61 +1200,6 @@ mod content { } } - fn deserialize_seq(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Seq(v) => visit_content_seq(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_tuple(self, _len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_seq(visitor) - } - - fn deserialize_tuple_struct( - self, - _name: &'static str, - _len: usize, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.deserialize_seq(visitor) - } - - fn deserialize_map(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Map(v) => visit_content_map(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_struct( - self, - _name: &'static str, - _fields: &'static [&'static str], - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::Seq(v) => visit_content_seq(v, visitor), - Content::Map(v) => visit_content_map(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_enum( self, _name: &str, @@ -1465,6 +1210,11 @@ mod content { V: Visitor<'de>, { let (variant, value) = match self.content { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::unit + // newtype_enum::newtype + // newtype_enum::tuple + // newtype_enum::struct_ Content::Map(value) => { let mut iter = value.into_iter(); let (variant, value) = match iter.next() { @@ -1485,33 +1235,20 @@ mod content { } (variant, Some(value)) } + // String() covered by tests/test_annotations.rs + // flatten::enum_::adjacently_tagged::newtype + // flatten::enum_::adjacently_tagged::struct_ + // String() covered by tests/test_enum_internally_tagged.rs + // struct_enum::unit + // String() covered by tests/test_macros.rs + // test_internally_tagged_struct_with_flattened_field s @ Content::String(_) | s @ Content::Str(_) => (s, None), - other => { - return Err(de::Error::invalid_type( - other.unexpected(), - &"string or map", - )); - } + _ => return self.deserialize_any(visitor), }; visitor.visit_enum(EnumDeserializer::new(variant, value)) } - fn deserialize_identifier(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match self.content { - Content::String(v) => visitor.visit_string(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(v) => visitor.visit_byte_buf(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U64(v) => visitor.visit_u64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1528,6 +1265,8 @@ mod content { where V: Visitor<'de, Value = Content<'de>>, { + // Covered by tests/test_enum_internally_tagged.rs + // containing_flatten let _ = visitor; Ok(self.content) } @@ -1612,10 +1351,7 @@ mod content { { match self.value { Some(value) => seed.deserialize(ContentDeserializer::new(value)), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"newtype variant", - )), + None => seed.deserialize(UnitDeserializer::new()), } } @@ -1624,15 +1360,8 @@ mod content { V: de::Visitor<'de>, { match self.value { - Some(Content::Seq(v)) => visit_content_seq(v, visitor), - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"tuple variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"tuple variant", - )), + Some(value) => ContentDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } @@ -1645,16 +1374,8 @@ mod content { V: de::Visitor<'de>, { match self.value { - Some(Content::Map(v)) => visit_content_map(v, visitor), - Some(Content::Seq(v)) => visit_content_seq(v, visitor), - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"struct variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"struct variant", - )), + Some(value) => ContentDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } } @@ -1665,52 +1386,6 @@ mod content { err: PhantomData, } - impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> - where - E: de::Error, - { - #[cold] - fn invalid_type(self, exp: &Expected) -> E { - de::Error::invalid_type(self.content.unexpected(), exp) - } - - fn deserialize_integer(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_float(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::F32(v) => visitor.visit_f32(v), - Content::F64(v) => visitor.visit_f64(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U16(v) => visitor.visit_u16(v), - Content::U32(v) => visitor.visit_u32(v), - Content::U64(v) => visitor.visit_u64(v), - Content::I8(v) => visitor.visit_i8(v), - Content::I16(v) => visitor.visit_i16(v), - Content::I32(v) => visitor.visit_i32(v), - Content::I64(v) => visitor.visit_i64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - } - fn visit_content_seq_ref<'a, 'de, V, E>( content: &'a [Content<'de>], visitor: V, @@ -1754,6 +1429,12 @@ mod content { { type Error = E; + forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf unit unit_struct seq tuple + tuple_struct map struct identifier + } + fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1786,139 +1467,6 @@ mod content { } } - fn deserialize_bool(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Bool(v) => visitor.visit_bool(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_i8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_i64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_u64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_integer(visitor) - } - - fn deserialize_f32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_f64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_float(visitor) - } - - fn deserialize_char(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Char(v) => visitor.visit_char(v), - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_str(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(ref v) => visitor.visit_bytes(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_string(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_str(visitor) - } - - fn deserialize_bytes(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(ref v) => visitor.visit_bytes(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::Seq(ref v) => visit_content_seq_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_byte_buf(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_bytes(visitor) - } - fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>, @@ -1940,27 +1488,6 @@ mod content { } } - fn deserialize_unit(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Unit => visitor.visit_unit(), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_unit_struct( - self, - _name: &'static str, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.deserialize_unit(visitor) - } - fn deserialize_newtype_struct(self, _name: &str, visitor: V) -> Result where V: Visitor<'de>, @@ -1984,61 +1511,6 @@ mod content { } } - fn deserialize_seq(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Seq(ref v) => visit_content_seq_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_tuple(self, _len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_seq(visitor) - } - - fn deserialize_tuple_struct( - self, - _name: &'static str, - _len: usize, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.deserialize_seq(visitor) - } - - fn deserialize_map(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Map(ref v) => visit_content_map_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - - fn deserialize_struct( - self, - _name: &'static str, - _fields: &'static [&'static str], - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::Seq(ref v) => visit_content_seq_ref(v, visitor), - Content::Map(ref v) => visit_content_map_ref(v, visitor), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_enum( self, _name: &str, @@ -2049,6 +1521,9 @@ mod content { V: Visitor<'de>, { let (variant, value) = match *self.content { + // Covered by tests/test_annotations.rs + // test_partially_untagged_enum + // test_partially_untagged_enum_desugared Content::Map(ref value) => { let mut iter = value.iter(); let (variant, value) = match iter.next() { @@ -2069,13 +1544,18 @@ mod content { } (variant, Some(value)) } + // String() covered by tests/test_enum_adjacently_tagged.rs + // partially_untagged + // String() covered by tests/test_enum_untagged.rs + // newtype_enum ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None), - ref other => { - return Err(de::Error::invalid_type( - other.unexpected(), - &"string or map", - )); - } + // Bool() covered by tests/test_annotations.rs + // test_partially_untagged_enum_generic + // U32() covered by tests/test_annotations.rs + // test_partially_untagged_enum_desugared + // Seq() covered by tests/test_annotations.rs + // test_partially_untagged_enum + _ => return self.deserialize_any(visitor), }; visitor.visit_enum(EnumRefDeserializer { @@ -2085,21 +1565,6 @@ mod content { }) } - fn deserialize_identifier(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - match *self.content { - Content::String(ref v) => visitor.visit_str(v), - Content::Str(v) => visitor.visit_borrowed_str(v), - Content::ByteBuf(ref v) => visitor.visit_bytes(v), - Content::Bytes(v) => visitor.visit_borrowed_bytes(v), - Content::U8(v) => visitor.visit_u8(v), - Content::U64(v) => visitor.visit_u64(v), - _ => Err(self.invalid_type(&visitor)), - } - } - fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de>, @@ -2115,6 +1580,16 @@ mod content { where V: Visitor<'de, Value = Content<'de>>, { + // Covered by tests/test_annotations.rs + // flatten::enum_::internally_tagged::structs + // flatten::enum_::internally_tagged::unit_enum_with_unknown_fields + // flatten::enum_::untagged::straightforward + // flatten::enum_::untagged::struct_ + // test_partially_untagged_enum + // Covered by tests/test_enum_internally_tagged.rs + // untagged_variant + // Covered by tests/test_enum_untagged.rs + // contains_flatten_with_integer_key let _ = visitor; Ok(self.content.clone()) } @@ -2184,8 +1659,8 @@ mod content { fn unit_variant(self) -> Result<(), E> { match self.value { Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)), - // Covered by tests/test_annotations.rs - // test_partially_untagged_adjacently_tagged_enum + // Covered by tests/test_enum_adjacently_tagged.rs + // partially_untagged // Covered by tests/test_enum_untagged.rs // newtype_enum::unit None => Ok(()), @@ -2203,10 +1678,7 @@ mod content { // Covered by tests/test_enum_untagged.rs // newtype_enum::newtype Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"newtype variant", - )), + None => seed.deserialize(UnitDeserializer::new()), } } @@ -2215,21 +1687,14 @@ mod content { V: de::Visitor<'de>, { match self.value { - // Covered by tests/test_annotations.rs + // Seq() covered by tests/test_annotations.rs // test_partially_untagged_enum // test_partially_untagged_enum_desugared - // Covered by tests/test_enum_untagged.rs + // Seq() covered by tests/test_enum_untagged.rs // newtype_enum::tuple0 // newtype_enum::tuple2 - Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor), - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"tuple variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"tuple variant", - )), + Some(value) => ContentRefDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } @@ -2242,21 +1707,13 @@ mod content { V: de::Visitor<'de>, { match self.value { - // Covered by tests/test_enum_untagged.rs + // Map() covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_map - Some(Content::Map(v)) => visit_content_map_ref(v, visitor), - // Covered by tests/test_enum_untagged.rs + // Seq() covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_seq // newtype_enum::empty_struct_from_seq - Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor), - Some(other) => Err(de::Error::invalid_type( - other.unexpected(), - &"struct variant", - )), - None => Err(de::Error::invalid_type( - de::Unexpected::UnitVariant, - &"struct variant", - )), + Some(value) => ContentRefDeserializer::new(value).deserialize_any(visitor), + None => visitor.visit_unit(), } } } From bf09ca4ca7dcdb21cefbfb1e8f2516188499d421 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 24 Aug 2024 17:16:18 +0500 Subject: [PATCH 09/15] Store deserializer in Enum[Ref]Deserializer and Variant[Ref]Deserializer directly That is cheap, creating a Content[Ref]Deserializer is a no-op --- serde/src/private/de.rs | 68 ++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 316157aa3..b0aba4c12 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1286,9 +1286,8 @@ mod content { where E: de::Error, { - variant: Content<'de>, - value: Option>, - err: PhantomData, + variant: ContentDeserializer<'de, E>, + value: Option>, } impl<'de, E> EnumDeserializer<'de, E> @@ -1297,9 +1296,8 @@ mod content { { pub fn new(variant: Content<'de>, value: Option>) -> EnumDeserializer<'de, E> { EnumDeserializer { - variant, - value, - err: PhantomData, + variant: ContentDeserializer::new(variant), + value: value.map(ContentDeserializer::new), } } } @@ -1316,10 +1314,9 @@ mod content { V: de::DeserializeSeed<'de>, { let visitor = VariantDeserializer { - value: self.value, - err: PhantomData, + de: self.value, }; - seed.deserialize(ContentDeserializer::new(self.variant)) + seed.deserialize(self.variant) .map(|v| (v, visitor)) } } @@ -1328,8 +1325,7 @@ mod content { where E: de::Error, { - value: Option>, - err: PhantomData, + de: Option>, } impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E> @@ -1339,8 +1335,8 @@ mod content { type Error = E; fn unit_variant(self) -> Result<(), E> { - match self.value { - Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)), + match self.de { + Some(de) => de::Deserialize::deserialize(de), None => Ok(()), } } @@ -1349,8 +1345,8 @@ mod content { where T: de::DeserializeSeed<'de>, { - match self.value { - Some(value) => seed.deserialize(ContentDeserializer::new(value)), + match self.de { + Some(de) => seed.deserialize(de), None => seed.deserialize(UnitDeserializer::new()), } } @@ -1359,8 +1355,8 @@ mod content { where V: de::Visitor<'de>, { - match self.value { - Some(value) => ContentDeserializer::new(value).deserialize_any(visitor), + match self.de { + Some(de) => de.deserialize_any(visitor), None => visitor.visit_unit(), } } @@ -1373,8 +1369,8 @@ mod content { where V: de::Visitor<'de>, { - match self.value { - Some(value) => ContentDeserializer::new(value).deserialize_any(visitor), + match self.de { + Some(de) => de.deserialize_any(visitor), None => visitor.visit_unit(), } } @@ -1559,9 +1555,8 @@ mod content { }; visitor.visit_enum(EnumRefDeserializer { - variant, - value, - err: PhantomData, + variant: ContentRefDeserializer::new(variant), + value: value.map(ContentRefDeserializer::new), }) } @@ -1617,9 +1612,8 @@ mod content { where E: de::Error, { - variant: &'a Content<'de>, - value: Option<&'a Content<'de>>, - err: PhantomData, + variant: ContentRefDeserializer<'a, 'de, E>, + value: Option>, } impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E> @@ -1634,10 +1628,9 @@ mod content { V: de::DeserializeSeed<'de>, { let visitor = VariantRefDeserializer { - value: self.value, - err: PhantomData, + de: self.value, }; - seed.deserialize(ContentRefDeserializer::new(self.variant)) + seed.deserialize(self.variant) .map(|v| (v, visitor)) } } @@ -1646,8 +1639,7 @@ mod content { where E: de::Error, { - value: Option<&'a Content<'de>>, - err: PhantomData, + de: Option>, } impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E> @@ -1657,8 +1649,8 @@ mod content { type Error = E; fn unit_variant(self) -> Result<(), E> { - match self.value { - Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)), + match self.de { + Some(de) => de::Deserialize::deserialize(de), // Covered by tests/test_enum_adjacently_tagged.rs // partially_untagged // Covered by tests/test_enum_untagged.rs @@ -1671,13 +1663,13 @@ mod content { where T: de::DeserializeSeed<'de>, { - match self.value { + match self.de { // Covered by tests/test_annotations.rs // test_partially_untagged_enum_desugared // test_partially_untagged_enum_generic // Covered by tests/test_enum_untagged.rs // newtype_enum::newtype - Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), + Some(de) => seed.deserialize(de), None => seed.deserialize(UnitDeserializer::new()), } } @@ -1686,14 +1678,14 @@ mod content { where V: de::Visitor<'de>, { - match self.value { + match self.de { // Seq() covered by tests/test_annotations.rs // test_partially_untagged_enum // test_partially_untagged_enum_desugared // Seq() covered by tests/test_enum_untagged.rs // newtype_enum::tuple0 // newtype_enum::tuple2 - Some(value) => ContentRefDeserializer::new(value).deserialize_any(visitor), + Some(de) => de.deserialize_any(visitor), None => visitor.visit_unit(), } } @@ -1706,13 +1698,13 @@ mod content { where V: de::Visitor<'de>, { - match self.value { + match self.de { // Map() covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_map // Seq() covered by tests/test_enum_untagged.rs // newtype_enum::struct_from_seq // newtype_enum::empty_struct_from_seq - Some(value) => ContentRefDeserializer::new(value).deserialize_any(visitor), + Some(de) => de.deserialize_any(visitor), None => visitor.visit_unit(), } } From 020dacedad2a8626cb94805f22ef3095861bb323 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 24 Aug 2024 19:02:04 +0500 Subject: [PATCH 10/15] cargo fmt --- serde/src/private/de.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index b0aba4c12..df7bb15d9 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1313,11 +1313,8 @@ mod content { where V: de::DeserializeSeed<'de>, { - let visitor = VariantDeserializer { - de: self.value, - }; - seed.deserialize(self.variant) - .map(|v| (v, visitor)) + let visitor = VariantDeserializer { de: self.value }; + seed.deserialize(self.variant).map(|v| (v, visitor)) } } @@ -1627,11 +1624,8 @@ mod content { where V: de::DeserializeSeed<'de>, { - let visitor = VariantRefDeserializer { - de: self.value, - }; - seed.deserialize(self.variant) - .map(|v| (v, visitor)) + let visitor = VariantRefDeserializer { de: self.value }; + seed.deserialize(self.variant).map(|v| (v, visitor)) } } From 55e1c2240f68342695b6eac6c462df4c9a66f5f3 Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 15 Aug 2024 23:05:36 +0500 Subject: [PATCH 11/15] Copy content::EnumDeserializer into FlatEnumDeserializer --- serde/src/private/de.rs | 116 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index df7bb15d9..0d0e0ec26 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1333,7 +1333,16 @@ mod content { fn unit_variant(self) -> Result<(), E> { match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::unit Some(de) => de::Deserialize::deserialize(de), + // Covered by tests/test_annotations.rs + // flatten::enum_::adjacently_tagged::newtype + // flatten::enum_::adjacently_tagged::struct_ + // Covered by tests/test_enum_internally_tagged.rs + // struct_enum::unit + // Covered by tests/test_macros.rs + // test_internally_tagged_struct_with_flattened_field None => Ok(()), } } @@ -1343,6 +1352,8 @@ mod content { T: de::DeserializeSeed<'de>, { match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::newtype Some(de) => seed.deserialize(de), None => seed.deserialize(UnitDeserializer::new()), } @@ -1353,6 +1364,8 @@ mod content { V: de::Visitor<'de>, { match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::tuple Some(de) => de.deserialize_any(visitor), None => visitor.visit_unit(), } @@ -1367,6 +1380,8 @@ mod content { V: de::Visitor<'de>, { match self.de { + // Covered by tests/test_enum_internally_tagged.rs + // newtype_enum::struct_ Some(de) => de.deserialize_any(visitor), None => visitor.visit_unit(), } @@ -2002,7 +2017,10 @@ where { for entry in self.0 { if let Some((key, value)) = flat_map_take_entry(entry, variants) { - return visitor.visit_enum(EnumDeserializer::new(key, Some(value))); + return visitor.visit_enum(FlatEnumDeserializer { + variant: ContentDeserializer::new(key), + value: Some(ContentDeserializer::new(value)), + }); } } @@ -2210,6 +2228,102 @@ fn flat_map_take_entry<'de>( } } +#[cfg(any(feature = "std", feature = "alloc"))] +struct FlatEnumDeserializer<'de, E> +where + E: Error, +{ + variant: ContentDeserializer<'de, E>, + value: Option>, +} + +#[cfg(any(feature = "std", feature = "alloc"))] +impl<'de, E> EnumAccess<'de> for FlatEnumDeserializer<'de, E> +where + E: Error, +{ + type Error = E; + type Variant = FlatVariantDeserializer<'de, Self::Error>; + + fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), E> + where + V: DeserializeSeed<'de>, + { + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::newtype + // flatten::enum_::externally_tagged::tuple + // flatten::enum_::externally_tagged::struct_from_map + // flatten::enum_::externally_tagged::struct_from_seq + let visitor = FlatVariantDeserializer { de: self.value }; + seed.deserialize(self.variant).map(|v| (v, visitor)) + } +} + +#[cfg(any(feature = "std", feature = "alloc"))] +struct FlatVariantDeserializer<'de, E> +where + E: Error, +{ + de: Option>, +} + +#[cfg(any(feature = "std", feature = "alloc"))] +impl<'de, E> VariantAccess<'de> for FlatVariantDeserializer<'de, E> +where + E: Error, +{ + type Error = E; + + fn unit_variant(self) -> Result<(), E> { + match self.de { + Some(de) => Deserialize::deserialize(de), + None => Ok(()), + } + } + + fn newtype_variant_seed(self, seed: T) -> Result + where + T: DeserializeSeed<'de>, + { + match self.de { + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::newtype + Some(de) => seed.deserialize(de), + None => seed.deserialize(crate::de::value::UnitDeserializer::new()), + } + } + + fn tuple_variant(self, _len: usize, visitor: V) -> Result + where + V: Visitor<'de>, + { + match self.de { + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::tuple + Some(de) => de.deserialize_any(visitor), + None => visitor.visit_unit(), + } + } + + fn struct_variant( + self, + _fields: &'static [&'static str], + visitor: V, + ) -> Result + where + V: Visitor<'de>, + { + match self.de { + // Map() covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::struct_from_map + // Seq() covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::struct_from_seq + Some(de) => de.deserialize_any(visitor), + None => visitor.visit_unit(), + } + } +} + pub struct AdjacentlyTaggedEnumVariantSeed { pub enum_name: &'static str, pub variants: &'static [&'static str], From 2ba21c8cb924b4d2a47389df74e96e455e7f7f5f Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 15 Aug 2024 23:11:31 +0500 Subject: [PATCH 12/15] Remove Option because Some variant is always used --- serde/src/private/de.rs | 42 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 0d0e0ec26..a394f987b 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -2019,7 +2019,7 @@ where if let Some((key, value)) = flat_map_take_entry(entry, variants) { return visitor.visit_enum(FlatEnumDeserializer { variant: ContentDeserializer::new(key), - value: Some(ContentDeserializer::new(value)), + value: ContentDeserializer::new(value), }); } } @@ -2234,7 +2234,7 @@ where E: Error, { variant: ContentDeserializer<'de, E>, - value: Option>, + value: ContentDeserializer<'de, E>, } #[cfg(any(feature = "std", feature = "alloc"))] @@ -2264,7 +2264,7 @@ struct FlatVariantDeserializer<'de, E> where E: Error, { - de: Option>, + de: ContentDeserializer<'de, E>, } #[cfg(any(feature = "std", feature = "alloc"))] @@ -2275,34 +2275,25 @@ where type Error = E; fn unit_variant(self) -> Result<(), E> { - match self.de { - Some(de) => Deserialize::deserialize(de), - None => Ok(()), - } + Deserialize::deserialize(self.de) } fn newtype_variant_seed(self, seed: T) -> Result where T: DeserializeSeed<'de>, { - match self.de { - // Covered by tests/test_annotations.rs - // flatten::enum_::externally_tagged::newtype - Some(de) => seed.deserialize(de), - None => seed.deserialize(crate::de::value::UnitDeserializer::new()), - } + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::newtype + seed.deserialize(self.de) } fn tuple_variant(self, _len: usize, visitor: V) -> Result where V: Visitor<'de>, { - match self.de { - // Covered by tests/test_annotations.rs - // flatten::enum_::externally_tagged::tuple - Some(de) => de.deserialize_any(visitor), - None => visitor.visit_unit(), - } + // Covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::tuple + self.de.deserialize_any(visitor) } fn struct_variant( @@ -2313,14 +2304,11 @@ where where V: Visitor<'de>, { - match self.de { - // Map() covered by tests/test_annotations.rs - // flatten::enum_::externally_tagged::struct_from_map - // Seq() covered by tests/test_annotations.rs - // flatten::enum_::externally_tagged::struct_from_seq - Some(de) => de.deserialize_any(visitor), - None => visitor.visit_unit(), - } + // Map() covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::struct_from_map + // Seq() covered by tests/test_annotations.rs + // flatten::enum_::externally_tagged::struct_from_seq + self.de.deserialize_any(visitor) } } From 4c28350a9b6027caad3d9ce47c94cda15f7defca Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 12 Aug 2023 15:33:04 +0500 Subject: [PATCH 13/15] Add support for internally tagged enums in non self-describing formats Deserializer methods are only hints which deserializer is not obliged to follow. Both TaggedContentVisitor and InternallyTaggedUnitVisitor accepts only visit_map and visit_seq and that is what derived implementation of Deserialize does for structs. Therefore it is fine to call deserialize_map here, as that already did in derived deserialize implementation --- serde_derive/src/de.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 5f90dff3d..24371109e 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1067,7 +1067,7 @@ fn deserialize_struct( _serde::de::VariantAccess::struct_variant(__variant, FIELDS, #visitor_expr) }, StructForm::InternallyTagged(_, deserializer) => quote! { - _serde::Deserializer::deserialize_any(#deserializer, #visitor_expr) + _serde::Deserializer::deserialize_map(#deserializer, #visitor_expr) }, StructForm::Untagged(_, deserializer) => quote! { _serde::Deserializer::deserialize_any(#deserializer, #visitor_expr) @@ -1402,7 +1402,7 @@ fn deserialize_internally_tagged_enum( #variants_stmt - let (__tag, __content) = _serde::Deserializer::deserialize_any( + let (__tag, __content) = _serde::Deserializer::deserialize_map( __deserializer, _serde::__private::de::TaggedContentVisitor::<__Field>::new(#tag, #expecting))?; let __deserializer = _serde::__private::de::ContentDeserializer::<__D::Error>::new(__content); @@ -1865,7 +1865,7 @@ fn deserialize_internally_tagged_variant( quote!((#default)) }); quote_block! { - _serde::Deserializer::deserialize_any(#deserializer, _serde::__private::de::InternallyTaggedUnitVisitor::new(#type_name, #variant_name))?; + _serde::Deserializer::deserialize_map(#deserializer, _serde::__private::de::InternallyTaggedUnitVisitor::new(#type_name, #variant_name))?; _serde::__private::Ok(#this_value::#variant_ident #default) } } From 72a74014828dc946d52f4ec6b2c9673bf9b56e3d Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 12 Aug 2023 16:26:35 +0500 Subject: [PATCH 14/15] Add support for struct variants in untagged and adjacently tagged enums in non self-describing formats Visitor passed to the deserialize_any supports only visit_map method, so we can always request deserialize_map --- serde_derive/src/de.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 24371109e..3cff17271 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1070,7 +1070,7 @@ fn deserialize_struct( _serde::Deserializer::deserialize_map(#deserializer, #visitor_expr) }, StructForm::Untagged(_, deserializer) => quote! { - _serde::Deserializer::deserialize_any(#deserializer, #visitor_expr) + _serde::Deserializer::deserialize_map(#deserializer, #visitor_expr) }, }; From 9702ce19a35c1b9d9bdc746ae86e89e5e6984bb8 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 12 Aug 2023 16:32:16 +0500 Subject: [PATCH 15/15] Use deserialize_unit instead of deserialize_any for unit variants of untagged and adjacently tagged enums --- serde_derive/src/de.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 3cff17271..2f3bf7289 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1910,7 +1910,7 @@ fn deserialize_untagged_variant( quote!((#default)) }); quote_expr! { - match _serde::Deserializer::deserialize_any( + match _serde::Deserializer::deserialize_unit( #deserializer, _serde::__private::de::UntaggedUnitVisitor::new(#type_name, #variant_name) ) {