refactoring

This commit is contained in:
vms 2021-04-12 18:02:22 +03:00
parent 7314000623
commit 7e19dfb4fb
2 changed files with 35 additions and 22 deletions

View File

@ -85,8 +85,8 @@ fn generate_serializer_fn(record: &fce_ast_types::AstRecordItem) -> proc_macro2:
fn generate_deserializer_fn(record: &fce_ast_types::AstRecordItem) -> proc_macro2::TokenStream {
let RecordDerDescriptor {
fields_der: deserializer,
record_ctor: type_constructor,
fields_der,
record_ctor,
} = record.generate_der();
let record_size =
@ -96,9 +96,9 @@ fn generate_deserializer_fn(record: &fce_ast_types::AstRecordItem) -> proc_macro
pub unsafe fn __fce_generated_deserialize(record_ptr: *const u8) -> Self {
let raw_record: Vec<u64> = Vec::from_raw_parts(record_ptr as _, #record_size, #record_size);
#deserializer
#fields_der
#type_constructor
#record_ctor
}
}
}

View File

@ -172,25 +172,38 @@ fn build_record_ctor<'a, 'v>(
};
if is_fields_named {
let field_names = ast_fields
.map(|ast_field| {
new_ident!(ast_field
.name
.as_ref()
.expect("all fields should have name"))
})
.collect::<Vec<_>>();
quote! {
Self {
#(#field_names: #field_values),*
}
}
build_named_fields_ctor(ast_fields, field_values)
} else {
quote! {
Self (
#(#field_values),*
)
build_unnamed_fields_ctor(field_values)
}
}
fn build_named_fields_ctor<'a, 'v>(
ast_fields: impl ExactSizeIterator<Item = &'a fce_ast_types::AstRecordField>,
field_values: impl ExactSizeIterator<Item = &'v syn::Ident>,
) -> proc_macro2::TokenStream {
let field_names = ast_fields
.map(|ast_field| {
new_ident!(ast_field
.name
.as_ref()
.expect("all fields should have name"))
})
.collect::<Vec<_>>();
quote! {
Self {
#(#field_names: #field_values),*
}
}
}
fn build_unnamed_fields_ctor<'v>(
field_values: impl ExactSizeIterator<Item = &'v syn::Ident>,
) -> proc_macro2::TokenStream {
quote! {
Self {
#(#field_values),*
}
}
}