From 780a461d926a05c0d18847a24b1bec596e4f399d Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 26 Oct 2020 22:06:00 +0500 Subject: [PATCH 1/3] Generate one deserializer rather than in each arm --- serde_derive/src/de.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 2dfe87ef0..2e86fd867 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1356,9 +1356,7 @@ fn deserialize_internally_tagged_enum( params, variant, cattrs, - quote! { - _serde::__private::de::ContentDeserializer::<__D::Error>::new(__tagged.content) - }, + quote!(__deserializer), )); quote! { @@ -1377,6 +1375,7 @@ fn deserialize_internally_tagged_enum( let __tagged = try!(_serde::Deserializer::deserialize_any( __deserializer, _serde::__private::de::TaggedContentVisitor::<__Field>::new(#tag, #expecting))); + let __deserializer = _serde::__private::de::ContentDeserializer::<__D::Error>::new(__tagged.content); match __tagged.tag { #(#variant_arms)* From b61ec84886b0689d571ff7bfdbbca0cf74012b1f Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 2 Oct 2022 21:12:02 +0500 Subject: [PATCH 2/3] Remove implementation of `DeserializeSeed` for `TaggedContentVisitor` It is not used anywhere --- serde/src/private/de.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 1396b1031..49dec18f8 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -821,22 +821,6 @@ mod content { } } - impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T> - where - T: Deserialize<'de>, - { - type Value = TaggedContent<'de, T>; - - fn deserialize(self, deserializer: D) -> Result - where - D: Deserializer<'de>, - { - // Internally tagged enums are only supported in self-describing - // formats. - deserializer.deserialize_any(self) - } - } - impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T> where T: Deserialize<'de>, From 3783a30ae7b02ab5870615af86f850dd0953df69 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 2 Oct 2022 19:34:33 +0500 Subject: [PATCH 3/3] Remove `TaggedContent`, replace it by a tuple That type does not give any benefits so we can avoid that hidden public but no-API struct --- serde/src/private/de.rs | 26 +++++++------------------- serde_derive/src/de.rs | 6 +++--- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 49dec18f8..78cdef672 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -797,19 +797,13 @@ mod content { /// Used by generated code to deserialize an internally tagged enum. /// /// Not public API. - pub struct TaggedContent<'de, T> { - pub tag: T, - pub content: Content<'de>, - } - - /// Not public API. - pub struct TaggedContentVisitor<'de, T> { + pub struct TaggedContentVisitor { tag_name: &'static str, expecting: &'static str, - value: PhantomData>, + value: PhantomData, } - impl<'de, T> TaggedContentVisitor<'de, T> { + impl TaggedContentVisitor { /// Visitor for the content of an internally tagged enum with the given /// tag name. pub fn new(name: &'static str, expecting: &'static str) -> Self { @@ -821,11 +815,11 @@ mod content { } } - impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T> + impl<'de, T> Visitor<'de> for TaggedContentVisitor where T: Deserialize<'de>, { - type Value = TaggedContent<'de, T>; + type Value = (T, Content<'de>); fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(self.expecting) @@ -842,10 +836,7 @@ mod content { } }; let rest = de::value::SeqAccessDeserializer::new(seq); - Ok(TaggedContent { - tag, - content: try!(Content::deserialize(rest)), - }) + Ok((tag, try!(Content::deserialize(rest)))) } fn visit_map(self, mut map: M) -> Result @@ -870,10 +861,7 @@ mod content { } match tag { None => Err(de::Error::missing_field(self.tag_name)), - Some(tag) => Ok(TaggedContent { - tag, - content: Content::Map(vec), - }), + Some(tag) => Ok((tag, Content::Map(vec))), } } } diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 2e86fd867..1366fb769 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1372,12 +1372,12 @@ fn deserialize_internally_tagged_enum( #variants_stmt - let __tagged = try!(_serde::Deserializer::deserialize_any( + let (__tag, __content) = try!(_serde::Deserializer::deserialize_any( __deserializer, _serde::__private::de::TaggedContentVisitor::<__Field>::new(#tag, #expecting))); - let __deserializer = _serde::__private::de::ContentDeserializer::<__D::Error>::new(__tagged.content); + let __deserializer = _serde::__private::de::ContentDeserializer::<__D::Error>::new(__content); - match __tagged.tag { + match __tag { #(#variant_arms)* } }