Skip to content

Commit

Permalink
Merge pull request #2290 from Mingun/enum-tests-and-cleanup
Browse files Browse the repository at this point in the history
Remove unused `impl` and unnecessary struct-wrapper around tuple
  • Loading branch information
dtolnay committed Jul 6, 2023
2 parents fa0312a + 3783a30 commit 92bfc8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
42 changes: 7 additions & 35 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
tag_name: &'static str,
expecting: &'static str,
value: PhantomData<TaggedContent<'de, T>>,
value: PhantomData<T>,
}

impl<'de, T> TaggedContentVisitor<'de, T> {
impl<T> TaggedContentVisitor<T> {
/// Visitor for the content of an internally tagged enum with the given
/// tag name.
pub fn new(name: &'static str, expecting: &'static str) -> Self {
Expand All @@ -821,27 +815,11 @@ mod content {
}
}

impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T>
where
T: Deserialize<'de>,
{
type Value = TaggedContent<'de, T>;

fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
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>
impl<'de, T> Visitor<'de> for TaggedContentVisitor<T>
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)
Expand All @@ -858,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<M>(self, mut map: M) -> Result<Self::Value, M::Error>
Expand All @@ -886,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))),
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand All @@ -1374,11 +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(__content);

match __tagged.tag {
match __tag {
#(#variant_arms)*
}
}
Expand Down

0 comments on commit 92bfc8d

Please sign in to comment.