Eliminate even more allocations

This commit is contained in:
Mingun
2023-05-19 22:47:00 +05:00
parent 855acaf112
commit 089aae1292
+7 -11
View File
@@ -2228,19 +2228,12 @@ fn deserialize_identifier(
quote!(#(#aliases)|* => _serde::__private::Ok(#this_value::#ident)) quote!(#(#aliases)|* => _serde::__private::Ok(#this_value::#ident))
}); });
let main_constructors: &Vec<_> = &fields
.iter()
.map(|(_, ident, _)| quote!(#this_value::#ident))
.collect();
let expecting = expecting.unwrap_or(if is_variant { let expecting = expecting.unwrap_or(if is_variant {
"variant identifier" "variant identifier"
} else { } else {
"field identifier" "field identifier"
}); });
let index_expecting = if is_variant { "variant" } else { "field" };
let bytes_to_str = if fallthrough.is_some() || collect_other_fields { let bytes_to_str = if fallthrough.is_some() || collect_other_fields {
None None
} else { } else {
@@ -2292,6 +2285,7 @@ fn deserialize_identifier(
let u64_fallthrough_arm = if let Some(fallthrough) = &fallthrough { let u64_fallthrough_arm = if let Some(fallthrough) = &fallthrough {
fallthrough fallthrough
} else { } else {
let index_expecting = if is_variant { "variant" } else { "field" };
let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len()); let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len());
u64_fallthrough_arm_tokens = quote! { u64_fallthrough_arm_tokens = quote! {
_serde::__private::Err(_serde::de::Error::invalid_value( _serde::__private::Err(_serde::de::Error::invalid_value(
@@ -2302,7 +2296,6 @@ fn deserialize_identifier(
&u64_fallthrough_arm_tokens &u64_fallthrough_arm_tokens
}; };
let variant_indices = 0_u64..;
let visit_other = if collect_other_fields { let visit_other = if collect_other_fields {
quote! { quote! {
fn visit_bool<__E>(self, __value: bool) -> _serde::__private::Result<Self::Value, __E> fn visit_bool<__E>(self, __value: bool) -> _serde::__private::Result<Self::Value, __E>
@@ -2397,15 +2390,18 @@ fn deserialize_identifier(
} }
} }
} else { } else {
let u64_mapping = fields.iter().enumerate().map(|(i, (_, ident, _))| {
let i = i as u64;
quote!(#i => _serde::__private::Ok(#this_value::#ident))
});
quote! { quote! {
fn visit_u64<__E>(self, __value: u64) -> _serde::__private::Result<Self::Value, __E> fn visit_u64<__E>(self, __value: u64) -> _serde::__private::Result<Self::Value, __E>
where where
__E: _serde::de::Error, __E: _serde::de::Error,
{ {
match __value { match __value {
#( #(#u64_mapping,)*
#variant_indices => _serde::__private::Ok(#main_constructors),
)*
_ => #u64_fallthrough_arm, _ => #u64_fallthrough_arm,
} }
} }