From e826e6cee9760613a28abbed765f09c7f15c23e0 Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 20 Apr 2021 00:46:41 +0300 Subject: [PATCH] update --- crates/wit/src/parsed_type/vector_utils.rs | 45 ++++--------------- .../record_generator.rs | 2 +- .../record_generator/record_serializer.rs | 14 +++--- 3 files changed, 18 insertions(+), 43 deletions(-) diff --git a/crates/wit/src/parsed_type/vector_utils.rs b/crates/wit/src/parsed_type/vector_utils.rs index 4ffc35e..9da0704 100644 --- a/crates/wit/src/parsed_type/vector_utils.rs +++ b/crates/wit/src/parsed_type/vector_utils.rs @@ -25,12 +25,8 @@ pub(crate) fn generate_vector_serializer( arg_name: &str, ) -> proc_macro2::TokenStream { let values_serializer = match value_ty { - ParsedType::Boolean(_) => { - quote! { - unimplemented!("Vector of booleans is unsupported") - } - } - ParsedType::I8(_) + ParsedType::Boolean(_) + | ParsedType::I8(_) | ParsedType::U8(_) | ParsedType::I16(_) | ParsedType::U16(_) @@ -120,31 +116,16 @@ pub(crate) fn generate_vector_deserializer( let values_deserializer = match value_ty { ParsedType::Boolean(_) => { quote! { - unimplemented!("Vector of booleans is unsupported") - } - } - ParsedType::F32(_) => { - quote! { - Vec::from_raw_parts(offset as _, size as _, size as _) - } - } - ParsedType::F64(_) => { - quote! { - let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); - let mut result = Vec::with_capacity(arg.len()); - - for value in arg { - result.push(f64::from_bits(value as _)); - } - - result + let arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); + arg.into_iter().map(|v| v == 1).collect::>() } } ParsedType::Utf8Str(_) | ParsedType::Utf8String(_) => { quote! { - let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); + let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); let mut arg = arg.into_iter(); let mut result = Vec::with_capacity(arg.len() / 2); + while let Some(offset) = arg.next() { let size = arg.next().unwrap(); let value = String::from_raw_parts(offset as _, size as _, size as _); @@ -164,7 +145,7 @@ pub(crate) fn generate_vector_deserializer( quote! { #inner_vector_deserializer - let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); + let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); @@ -182,7 +163,7 @@ pub(crate) fn generate_vector_deserializer( let record_name_ident = crate::new_ident!(record_name); quote! { - let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); + let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); let mut result = Vec::with_capacity(arg.len()); for offset in arg { @@ -195,21 +176,13 @@ pub(crate) fn generate_vector_deserializer( } _ => { quote! { - let mut arg: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); - let mut result = Vec::with_capacity(arg.len()); - - for value in arg { - result.push(value as _); - } - - result + Vec::from_raw_parts(offset as _, size as _, size as _) } } }; quote! { unsafe fn #arg(offset: u32, size: u32) -> Vec<#value_ty> { - let size = size / 8; #values_deserializer } } diff --git a/crates/wit/src/token_stream_generator/record_generator.rs b/crates/wit/src/token_stream_generator/record_generator.rs index 40fa7da..1cdfe08 100644 --- a/crates/wit/src/token_stream_generator/record_generator.rs +++ b/crates/wit/src/token_stream_generator/record_generator.rs @@ -107,7 +107,7 @@ fn generate_deserializer_fn(record: &AstRecordItem) -> proc_macro2::TokenStream quote::quote! { pub unsafe fn __fce_generated_deserialize(record_ptr: *const u8) -> Self { - let raw_record: Vec = Vec::from_raw_parts(record_ptr as _, #record_size, #record_size); + let raw_record: Vec = Vec::from_raw_parts(record_ptr as _, #record_size, #record_size); #fields_der 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 a8fc9a1..62bfcd3 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 @@ -42,8 +42,9 @@ impl RecordSerGlueCodeGenerator for AstRecordItem { let field_serialization = match &field.ty { ParsedType::Utf8Str(_) | ParsedType::Utf8String(_) => { quote! { - raw_record.push(&#field_ident.as_ptr().to_le_bytes()); - raw_record.push(&#field_ident.len().to_le_bytes()); + let field_ident_ptr = #field_ident.as_ptr() as usize; + raw_record.extend(&field_ident_ptr.to_le_bytes()); + raw_record.extend(&#field_ident.len().to_le_bytes()); } } ParsedType::Vector(ty, passing_style) => { @@ -65,17 +66,18 @@ impl RecordSerGlueCodeGenerator for AstRecordItem { #vector_serializer let #serialized_field_ident = unsafe { #generated_serializer_ident(&#field_ident) }; - raw_record.push(&#serialized_field_ident.0.to_le_bytes()); - raw_record.push(&#serialized_field_ident.1.to_le_bytes()); + raw_record.extend(&#serialized_field_ident.0.to_le_bytes()); + raw_record.extend(&#serialized_field_ident.1.to_le_bytes()); } } ParsedType::Record(..) => { quote! { - raw_record.push(&#field_ident.__fce_generated_serialize().to_le_bytes()); + let serialized_struct_ptr = #field_ident.__fce_generated_serialize() as usize; + raw_record.extend(&serialized_struct_ptr.to_le_bytes()); } } _ => quote! { - raw_record.push(&#field_ident.to_le_bytes()); + raw_record.extend(&#field_ident.to_le_bytes()); }, };