From 92072f4b051009bf81ed13f5e4e80bd1b6c0561c Mon Sep 17 00:00:00 2001 From: vms Date: Mon, 19 Apr 2021 20:18:37 +0300 Subject: [PATCH] update record ser --- .../token_stream_generator/record_generator.rs | 3 ++- .../record_generator/record_serializer.rs | 18 +++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/crates/wit/src/token_stream_generator/record_generator.rs b/crates/wit/src/token_stream_generator/record_generator.rs index 642c534..40fa7da 100644 --- a/crates/wit/src/token_stream_generator/record_generator.rs +++ b/crates/wit/src/token_stream_generator/record_generator.rs @@ -78,7 +78,8 @@ fn generate_serializer_fn(record: &AstRecordItem) -> proc_macro2::TokenStream { quote::quote! { pub fn __fce_generated_serialize(&self) -> *const u8 { - let mut raw_record: Vec = Vec::with_capacity(2 * #fields_count); + // 4 is an average size of a possible record field + let mut raw_record: Vec = Vec::with_capacity(4 * #fields_count); #serializer diff --git a/crates/wit/src/token_stream_generator/record_generator/record_serializer.rs b/crates/wit/src/token_stream_generator/record_generator/record_serializer.rs index fdf0b7d..a8fc9a1 100644 --- a/crates/wit/src/token_stream_generator/record_generator/record_serializer.rs +++ b/crates/wit/src/token_stream_generator/record_generator/record_serializer.rs @@ -40,15 +40,10 @@ impl RecordSerGlueCodeGenerator for AstRecordItem { let field_ident = field_ident(field, id); let field_serialization = match &field.ty { - ParsedType::F64(_) => { - quote! { - raw_record.push(#field_ident.to_bits()); - } - } ParsedType::Utf8Str(_) | ParsedType::Utf8String(_) => { quote! { - raw_record.push(#field_ident.as_ptr() as _); - raw_record.push(#field_ident.len() as _); + raw_record.push(&#field_ident.as_ptr().to_le_bytes()); + raw_record.push(&#field_ident.len().to_le_bytes()); } } ParsedType::Vector(ty, passing_style) => { @@ -69,17 +64,18 @@ impl RecordSerGlueCodeGenerator for AstRecordItem { quote::quote! { #vector_serializer let #serialized_field_ident = unsafe { #generated_serializer_ident(&#field_ident) }; - raw_record.push(#serialized_field_ident.0 as _); - raw_record.push(#serialized_field_ident.1 as _); + + raw_record.push(&#serialized_field_ident.0.to_le_bytes()); + raw_record.push(&#serialized_field_ident.1.to_le_bytes()); } } ParsedType::Record(..) => { quote! { - raw_record.push(#field_ident.__fce_generated_serialize() as _); + raw_record.push(&#field_ident.__fce_generated_serialize().to_le_bytes()); } } _ => quote! { - raw_record.push(#field_ident as u64); + raw_record.push(&#field_ident.to_le_bytes()); }, };