Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to add enum explicit variant support once again #2676

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions serde/src/private/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ pub fn constrain<T: ?Sized>(t: &T) -> &T {
}

/// Not public API.
pub fn serialize_tagged_newtype<S, T>(
pub fn serialize_tagged_newtype<S, T, I>(
serializer: S,
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: I,
value: &T,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Serialize,
I: Serialize,
{
value.serialize(TaggedSerializer {
type_ident,
Expand All @@ -35,11 +36,11 @@ where
})
}

struct TaggedSerializer<S> {
struct TaggedSerializer<S, I> {
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: I,
delegate: S,
}

Expand Down Expand Up @@ -79,9 +80,10 @@ impl Display for Unsupported {
}
}

impl<S> TaggedSerializer<S>
impl<S, I> TaggedSerializer<S, I>
where
S: Serializer,
I: Serialize,
{
fn bad_type(self, what: Unsupported) -> S::Error {
ser::Error::custom(format_args!(
Expand All @@ -91,9 +93,10 @@ where
}
}

impl<S> Serializer for TaggedSerializer<S>
impl<S, I> Serializer for TaggedSerializer<S, I>
where
S: Serializer,
I: Serialize,
{
type Ok = S::Ok;
type Error = S::Error;
Expand Down Expand Up @@ -183,13 +186,13 @@ where

fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
map.end()
}

fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
map.end()
}

Expand All @@ -200,7 +203,7 @@ where
inner_variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_entry(inner_variant, &()));
map.end()
}
Expand All @@ -227,7 +230,7 @@ where
T: Serialize,
{
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_entry(inner_variant, inner_value));
map.end()
}
Expand Down Expand Up @@ -270,7 +273,7 @@ where
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_key(inner_variant));
Ok(SerializeTupleVariantAsMapValue::new(
map,
Expand All @@ -281,7 +284,7 @@ where

fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(len.map(|len| len + 1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
Ok(map)
}

Expand All @@ -291,7 +294,7 @@ where
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
let mut state = tri!(self.delegate.serialize_struct(name, len + 1));
tri!(state.serialize_field(self.tag, self.variant_name));
tri!(state.serialize_field(self.tag, &self.variant_name));
Ok(state)
}

Expand All @@ -317,7 +320,7 @@ where
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_key(inner_variant));
Ok(SerializeStructVariantAsMapValue::new(
map,
Expand Down
Loading
Loading