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

Eliminate duplicated borrowed and non-borrowed identifier deserialization #1962

Merged
merged 2 commits into from Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 60 additions & 58 deletions serde_derive/src/de.rs
Expand Up @@ -1904,30 +1904,25 @@ fn deserialize_generated_identifier(
let (ignore_variant, fallthrough) = if !is_variant && cattrs.has_flatten() {
let ignore_variant = quote!(__other(_serde::__private::de::Content<'de>),);
let fallthrough = quote!(_serde::__private::Ok(__Field::__other(__value)));
(
Some(ignore_variant),
Some((fallthrough.clone(), fallthrough)),
)
(Some(ignore_variant), Some(fallthrough))
} else if let Some(other_idx) = other_idx {
let ignore_variant = fields[other_idx].1.clone();
let fallthrough = quote!(_serde::__private::Ok(__Field::#ignore_variant));
(None, Some((fallthrough.clone(), fallthrough)))
(None, Some(fallthrough))
} else if is_variant || cattrs.deny_unknown_fields() {
(None, None)
} else {
let ignore_variant = quote!(__ignore,);
let fallthrough = quote!(_serde::__private::Ok(__Field::__ignore));
(
Some(ignore_variant),
Some((fallthrough.clone(), fallthrough)),
)
(Some(ignore_variant), Some(fallthrough))
};

let visitor_impl = Stmts(deserialize_identifier(
&this,
fields,
is_variant,
fallthrough,
None,
!is_variant && cattrs.has_flatten(),
None,
));
Expand Down Expand Up @@ -1981,15 +1976,15 @@ fn deserialize_custom_identifier(
let this = &params.this;
let this = quote!(#this);

let (ordinary, fallthrough) = if let Some(last) = variants.last() {
let (ordinary, fallthrough, fallthrough_borrowed) = if let Some(last) = variants.last() {
let last_ident = &last.ident;
if last.attrs.other() {
// Process `serde(other)` attribute. It would always be found on the
// last variant (checked in `check_identifier`), so all preceding
// are ordinary variants.
let ordinary = &variants[..variants.len() - 1];
let fallthrough = quote!(_serde::__private::Ok(#this::#last_ident));
(ordinary, Some((fallthrough.clone(), fallthrough)))
(ordinary, Some(fallthrough), None)
} else if let Style::Newtype = last.style {
let ordinary = &variants[..variants.len() - 1];
let fallthrough = |value| {
Expand All @@ -2003,16 +1998,16 @@ fn deserialize_custom_identifier(
};
(
ordinary,
Some((
fallthrough(quote!(__value)),
fallthrough(quote!(_serde::__private::de::Borrowed(__value))),
)),
Some(fallthrough(quote!(__value))),
Some(fallthrough(quote!(_serde::__private::de::Borrowed(
__value
)))),
)
} else {
(variants, None)
(variants, None, None)
}
} else {
(variants, None)
(variants, None, None)
};

let names_idents: Vec<_> = ordinary
Expand Down Expand Up @@ -2050,6 +2045,7 @@ fn deserialize_custom_identifier(
&names_idents,
is_variant,
fallthrough,
fallthrough_borrowed,
false,
cattrs.expecting(),
));
Expand Down Expand Up @@ -2080,8 +2076,8 @@ fn deserialize_identifier(
this: &TokenStream,
fields: &[(String, Ident, Vec<String>)],
is_variant: bool,
// .0 for referenced data, .1 -- for borrowed
fallthrough: Option<(TokenStream, TokenStream)>,
fallthrough: Option<TokenStream>,
fallthrough_borrowed: Option<TokenStream>,
collect_other_fields: bool,
expecting: Option<&str>,
) -> Fragment {
Expand Down Expand Up @@ -2145,22 +2141,19 @@ fn deserialize_identifier(
(None, None, None, None)
};

let (fallthrough_arm, fallthrough_borrowed_arm) = if let Some(fallthrough) = fallthrough.clone()
{
let fallthrough_arm = if let Some(fallthrough) = fallthrough.clone() {
fallthrough
} else if is_variant {
let fallthrough = quote! {
quote! {
_serde::__private::Err(_serde::de::Error::unknown_variant(__value, VARIANTS))
};
(fallthrough.clone(), fallthrough)
}
} else {
let fallthrough = quote! {
quote! {
_serde::__private::Err(_serde::de::Error::unknown_field(__value, FIELDS))
};
(fallthrough.clone(), fallthrough)
}
};

let u64_fallthrough_arm = if let Some((fallthrough, _)) = fallthrough {
let u64_fallthrough_arm = if let Some(fallthrough) = fallthrough {
fallthrough
} else {
let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len());
Expand Down Expand Up @@ -2282,6 +2275,44 @@ fn deserialize_identifier(
}
};

let visit_borrowed = if fallthrough_borrowed.is_some() || collect_other_fields {
let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(&fallthrough_arm);
Some(quote! {
fn visit_borrowed_str<__E>(self, __value: &'de str) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_strs => _serde::__private::Ok(#constructors),
)*
_ => {
#value_as_borrowed_str_content
#fallthrough_borrowed_arm
}
}
}

fn visit_borrowed_bytes<__E>(self, __value: &'de [u8]) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_bytes => _serde::__private::Ok(#constructors),
)*
_ => {
#bytes_to_str
#value_as_borrowed_bytes_content
#fallthrough_borrowed_arm
}
}
}
})
} else {
None
};

quote_block! {
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
_serde::__private::Formatter::write_str(__formatter, #expecting)
Expand All @@ -2304,21 +2335,6 @@ fn deserialize_identifier(
}
}

fn visit_borrowed_str<__E>(self, __value: &'de str) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_strs => _serde::__private::Ok(#constructors),
)*
_ => {
#value_as_borrowed_str_content
#fallthrough_borrowed_arm
}
}
}

fn visit_bytes<__E>(self, __value: &[u8]) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
Expand All @@ -2335,21 +2351,7 @@ fn deserialize_identifier(
}
}

fn visit_borrowed_bytes<__E>(self, __value: &'de [u8]) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_bytes => _serde::__private::Ok(#constructors),
)*
_ => {
#bytes_to_str
#value_as_borrowed_bytes_content
#fallthrough_borrowed_arm
}
}
}
#visit_borrowed
}
}

Expand Down
101 changes: 0 additions & 101 deletions test_suite/tests/expand/de_enum.expanded.rs
Expand Up @@ -332,25 +332,6 @@ const _: () = {
)),
}
}
fn visit_borrowed_str<__E>(
self,
__value: &'de str,
) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
"Unit" => _serde::__private::Ok(__Field::__field0),
"Seq" => _serde::__private::Ok(__Field::__field1),
"Map" => _serde::__private::Ok(__Field::__field2),
"_Unit2" => _serde::__private::Ok(__Field::__field3),
"_Seq2" => _serde::__private::Ok(__Field::__field4),
"_Map2" => _serde::__private::Ok(__Field::__field5),
_ => _serde::__private::Err(_serde::de::Error::unknown_variant(
__value, VARIANTS,
)),
}
}
fn visit_bytes<__E>(
self,
__value: &[u8],
Expand All @@ -373,28 +354,6 @@ const _: () = {
}
}
}
fn visit_borrowed_bytes<__E>(
self,
__value: &'de [u8],
) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
b"Unit" => _serde::__private::Ok(__Field::__field0),
b"Seq" => _serde::__private::Ok(__Field::__field1),
b"Map" => _serde::__private::Ok(__Field::__field2),
b"_Unit2" => _serde::__private::Ok(__Field::__field3),
b"_Seq2" => _serde::__private::Ok(__Field::__field4),
b"_Map2" => _serde::__private::Ok(__Field::__field5),
_ => {
let __value = &_serde::__private::from_utf8_lossy(__value);
_serde::__private::Err(_serde::de::Error::unknown_variant(
__value, VARIANTS,
))
}
}
}
}
impl<'de> _serde::Deserialize<'de> for __Field {
#[inline]
Expand Down Expand Up @@ -632,21 +591,6 @@ const _: () = {
_ => _serde::__private::Ok(__Field::__ignore),
}
}
fn visit_borrowed_str<__E>(
self,
__value: &'de str,
) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
"a" => _serde::__private::Ok(__Field::__field0),
"b" => _serde::__private::Ok(__Field::__field1),
"c" => _serde::__private::Ok(__Field::__field2),
"d" => _serde::__private::Ok(__Field::__field3),
_ => _serde::__private::Ok(__Field::__ignore),
}
}
fn visit_bytes<__E>(
self,
__value: &[u8],
Expand All @@ -662,21 +606,6 @@ const _: () = {
_ => _serde::__private::Ok(__Field::__ignore),
}
}
fn visit_borrowed_bytes<__E>(
self,
__value: &'de [u8],
) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
b"a" => _serde::__private::Ok(__Field::__field0),
b"b" => _serde::__private::Ok(__Field::__field1),
b"c" => _serde::__private::Ok(__Field::__field2),
b"d" => _serde::__private::Ok(__Field::__field3),
_ => _serde::__private::Ok(__Field::__ignore),
}
}
}
impl<'de> _serde::Deserialize<'de> for __Field {
#[inline]
Expand Down Expand Up @@ -1165,21 +1094,6 @@ const _: () = {
_ => _serde::__private::Ok(__Field::__ignore),
}
}
fn visit_borrowed_str<__E>(
self,
__value: &'de str,
) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
"a" => _serde::__private::Ok(__Field::__field0),
"b" => _serde::__private::Ok(__Field::__field1),
"c" => _serde::__private::Ok(__Field::__field2),
"d" => _serde::__private::Ok(__Field::__field3),
_ => _serde::__private::Ok(__Field::__ignore),
}
}
fn visit_bytes<__E>(
self,
__value: &[u8],
Expand All @@ -1195,21 +1109,6 @@ const _: () = {
_ => _serde::__private::Ok(__Field::__ignore),
}
}
fn visit_borrowed_bytes<__E>(
self,
__value: &'de [u8],
) -> _serde::__private::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
b"a" => _serde::__private::Ok(__Field::__field0),
b"b" => _serde::__private::Ok(__Field::__field1),
b"c" => _serde::__private::Ok(__Field::__field2),
b"d" => _serde::__private::Ok(__Field::__field3),
_ => _serde::__private::Ok(__Field::__ignore),
}
}
}
impl<'de> _serde::Deserialize<'de> for __Field {
#[inline]
Expand Down