usecrate::de::{deserialize_seq, has_flatten, Parameters, TupleForm}; #[cfg(feature = "deserialize_in_place")] usecrate::de::{deserialize_seq_in_place, place_lifetime}; usecrate::fragment::{Fragment, Stmts}; usecrate::internals::ast::Field; usecrate::internals::attr; usecrate::private; use proc_macro2::TokenStream; use quote::{quote, quote_spanned}; use syn::spanned::Spanned;
/// Generates `Deserialize::deserialize` body for a `struct Tuple(...);` including `struct Newtype(T);` pub(super) fn deserialize(
params: &Parameters,
fields: &[Field],
cattrs: &attr::Container,
form: TupleForm,
) -> Fragment {
assert!(
!has_flatten(fields), "tuples and tuple variants cannot have flatten fields"
);
let field_count = fields
.iter()
.filter(|field| !field.attrs.skip_deserializing())
.count();
let this_type = ¶ms.this_type; let this_value = ¶ms.this_value; let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
params.generics_with_de_lifetime(); let delife = params.borrowed.de_lifetime();
// If there are getters (implying private fields), construct the local type // and use an `Into` conversion to get the remote type. If there are no // getters then construct the target type directly. let construct = if params.has_getter { let local = ¶ms.local;
quote!(#local)
} else {
quote!(#this_value)
};
let type_path = match form {
TupleForm::Tuple => construct,
TupleForm::ExternallyTagged(variant_ident) | TupleForm::Untagged(variant_ident) => {
quote!(#construct::#variant_ident)
}
}; let expecting = match form {
TupleForm::Tuple => format!("tuple struct {}", params.type_name()),
TupleForm::ExternallyTagged(variant_ident) | TupleForm::Untagged(variant_ident) => {
format!("tuple variant {}::{}", params.type_name(), variant_ident)
}
}; let expecting = cattrs.expecting().unwrap_or(&expecting);
let nfields = fields.len();
let visit_newtype_struct = match form {
TupleForm::Tuple if nfields == 1 => {
Some(deserialize_newtype_struct(&type_path, params, &fields[0]))
}
_ => None,
};
#[automatically_derived] impl#de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics#where_clause { type Value = #this_type#ty_generics;
fn deserialize_newtype_struct(
type_path: &TokenStream,
params: &Parameters,
field: &Field,
) -> TokenStream { let delife = params.borrowed.de_lifetime(); let field_ty = field.ty; let deserializer_var = quote!(__e);
let value = match field.attrs.deserialize_with() {
None => { let span = field.original.span(); let func = quote_spanned!(span=> <#field_tyas _serde::Deserialize>::deserialize);
quote! { #func(#deserializer_var)?
}
}
Some(path) => { // If #path returns wrong type, error will be reported here (^^^^^). // We attach span of the path to the function so it will be reported // on the #[serde(with = "...")] // ^^^^^
quote_spanned! {path.span()=> #path(#deserializer_var)?
}
}
};
letmut result = quote!(#type_path(__field0)); if params.has_getter { let this_type = ¶ms.this_type; let (_, ty_generics, _) = params.generics.split_for_impl();
result = quote! {
_serde::#private::Into::<#this_type#ty_generics>::into(#result)
};
}
/// Generates `Deserialize::deserialize_in_place` body for a `struct Tuple(...);` including `struct Newtype(T);` #[cfg(feature = "deserialize_in_place")] pub(super) fn deserialize_in_place(
params: &Parameters,
fields: &[Field],
cattrs: &attr::Container,
) -> Fragment {
assert!(
!has_flatten(fields), "tuples and tuple variants cannot have flatten fields"
);
let field_count = fields
.iter()
.filter(|field| !field.attrs.skip_deserializing())
.count();
let this_type = ¶ms.this_type; let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
params.generics_with_de_lifetime(); let delife = params.borrowed.de_lifetime();
let expecting = format!("tuple struct {}", params.type_name()); let expecting = cattrs.expecting().unwrap_or(&expecting);
let nfields = fields.len();
let visit_newtype_struct = if nfields == 1 { // We do not generate deserialize_in_place if every field has a // deserialize_with.
assert!(fields[0].attrs.deserialize_with().is_none());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.