Skip to content

Commit

Permalink
Remove TaggedContent, replace it by a tuple
Browse files Browse the repository at this point in the history
That type does not give any benefits so we can avoid that hidden public but no-API struct
  • Loading branch information
Mingun committed May 5, 2023
1 parent e2bcdfc commit 6c65f2c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
26 changes: 7 additions & 19 deletions serde/src/private/de.rs
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,11 +815,11 @@ mod content {
}
}

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 @@ -842,10 +836,7 @@ mod content {
}
};
let rest = de::value::SeqAccessDeserializer::new(seq);
Ok(TaggedContent {
tag: 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 @@ -870,10 +861,7 @@ mod content {
}
match tag {
None => Err(de::Error::missing_field(self.tag_name)),
Some(tag) => Ok(TaggedContent {
tag: tag,
content: Content::Map(vec),
}),
Some(tag) => Ok((tag, Content::Map(vec))),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions serde_derive/src/de.rs
Expand Up @@ -1352,12 +1352,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)*
}
}
Expand Down

0 comments on commit 6c65f2c

Please sign in to comment.