diff options
Diffstat (limited to 'bzipper_macros/src/impls')
-rw-r--r-- | bzipper_macros/src/impls/deserialise_enum.rs | 20 | ||||
-rw-r--r-- | bzipper_macros/src/impls/deserialise_struct.rs | 57 | ||||
-rw-r--r-- | bzipper_macros/src/impls/serialise_enum.rs | 19 | ||||
-rw-r--r-- | bzipper_macros/src/impls/serialise_struct.rs | 20 |
4 files changed, 41 insertions, 75 deletions
diff --git a/bzipper_macros/src/impls/deserialise_enum.rs b/bzipper_macros/src/impls/deserialise_enum.rs index 7065c86..4c88a41 100644 --- a/bzipper_macros/src/impls/deserialise_enum.rs +++ b/bzipper_macros/src/impls/deserialise_enum.rs @@ -38,36 +38,30 @@ pub fn deserialise_enum(data: &DataEnum) -> TokenStream { let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); for field in &variant.fields { - let field_ty = &field.ty; - let command = field.ident .as_ref() .map_or_else( - || quote! { stream.take::<#field_ty>()? }, - |field_name| quote! { #field_name: stream.take::<#field_ty>()? } + || quote! { Deserialise::deserialise(stream)? }, + |field_name| quote! { #field_name: Deserialise::deserialise(stream)? } ); chain_commands.push(command); } - let block = match variant.fields { + let value = match variant.fields { Fields::Named( ..) => quote! { Self::#variant_name { #chain_commands } }, Fields::Unnamed(..) => quote! { Self::#variant_name(#chain_commands) }, Fields::Unit => quote! { Self::#variant_name }, }; - match_arms.push(quote! { #discriminant => #block }); + match_arms.push(quote! { #discriminant => #value }); } - match_arms.push(quote! { value => return Err(::bzipper::Error::InvalidDiscriminant { value }) }); + match_arms.push(quote! { value => return Err(::bzipper::Error::InvalidDiscriminant(value)) }); quote! { - fn deserialise(data: &[u8]) -> ::bzipper::Result<Self> { - ::core::debug_assert_eq!(data.len(), <Self as ::bzipper::Serialise>::SERIALISED_SIZE); - - let mut stream = ::bzipper::Dstream::new(data); - - let value = match (stream.take::<u32>()?) { #match_arms }; + fn deserialise(stream: &::bzipper::Dstream) -> ::bzipper::Result<Self> { + let value = match (<u32 as ::bzipper::Deserialise>::deserialise(stream)?) { #match_arms }; Ok(value) } } diff --git a/bzipper_macros/src/impls/deserialise_struct.rs b/bzipper_macros/src/impls/deserialise_struct.rs index 414a313..f8c167b 100644 --- a/bzipper_macros/src/impls/deserialise_struct.rs +++ b/bzipper_macros/src/impls/deserialise_struct.rs @@ -26,52 +26,35 @@ use syn::punctuated::Punctuated; #[must_use] pub fn deserialise_struct(data: &DataStruct) -> TokenStream { - if let Fields::Named(..) = data.fields { - let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); - - for field in &data.fields { - let name = field.ident.as_ref().unwrap(); - let ty = &field.ty; - - chain_commands.push(quote! { #name: stream.take::<#ty>()? }); - } - + if matches!(data.fields, Fields::Unit) { quote! { - fn deserialise(data: &[u8]) -> ::bzipper::Result<Self> { - ::core::debug_assert_eq!(data.len(), <Self as ::bzipper::Serialise>::SERIALISED_SIZE); - - let stream = ::bzipper::Dstream::new(data); - - Ok(Self { #chain_commands }) - } + #[inline(always)] + fn deserialise(_stream: &::bzipper::Dstream) -> ::bzipper::Result<Self> { Ok(Self) } } - } else if let Fields::Unnamed(..) = data.fields { + } else { let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); for field in &data.fields { - let ty = &field.ty; - - chain_commands.push(quote! { stream.take::<#ty>()? }); + let command = field.ident + .as_ref() + .map_or_else( + || quote! { Deserialise::deserialise(stream)? }, + |field_name| quote! { #field_name: Deserialise::deserialise(stream)? } + ); + + chain_commands.push(command); } - quote! { - fn deserialise(data: &[u8]) -> ::bzipper::Result<Self> { - ::core::debug_assert_eq!(data.len(), <Self as ::bzipper::Serialise>::SERIALISED_SIZE); - - let stream = ::bzipper::Dstream::new(data); - - Ok(Self(#chain_commands)) - } - } - } else { - // Fields::Unit + let value = if let Fields::Named(..) = data.fields { + quote! { Self { #chain_commands } } + } else { + quote! { Self(#chain_commands) } + }; quote! { - #[inline(always)] - fn deserialise(data: &[u8]) -> ::bzipper::Result<Self> { - ::core::debug_assert_eq!(data.len(), <Self as ::bzipper::Serialise>::SERIALISED_SIZE); - - Ok(Self) + fn deserialise(stream: &::bzipper::Dstream) -> ::bzipper::Result<Self> { + let value = #value; + Ok(value) } } } diff --git a/bzipper_macros/src/impls/serialise_enum.rs b/bzipper_macros/src/impls/serialise_enum.rs index a554351..825886c 100644 --- a/bzipper_macros/src/impls/serialise_enum.rs +++ b/bzipper_macros/src/impls/serialise_enum.rs @@ -38,15 +38,15 @@ pub fn serialise_enum(data: &DataEnum) -> TokenStream { let variant_name = &variant.ident; let discriminant = u32::try_from(index) - .expect("enumeration discriminants must be representable in `u32`"); + .expect("enumeration discriminants must be representable as `u32`"); // Discriminant size: - serialised_size.push(quote! { <u32 as ::bzipper::Serialise>::SERIALISED_SIZE }); + serialised_size.push(quote! { <u32 as ::bzipper::Serialise>::MAX_SERIALISED_SIZE }); let mut captures = Punctuated::<Capture, Token![,]>::new(); let mut chain_commands = Punctuated::<TokenStream, Token![;]>::new(); - chain_commands.push(quote! { stream.append(&#discriminant)? }); + chain_commands.push(quote! { #discriminant.serialise(stream)? }); for (index, field) in variant.fields.iter().enumerate() { let field_ty = &field.ty; @@ -55,14 +55,14 @@ pub fn serialise_enum(data: &DataEnum) -> TokenStream { .as_ref() .map_or_else(|| Ident::new(&format!("v{index}"), Span::call_site()), Clone::clone); - serialised_size.push(quote! { <#field_ty as ::bzipper::Serialise>::SERIALISED_SIZE }); + serialised_size.push(quote! { <#field_ty as ::bzipper::Serialise>::MAX_SERIALISED_SIZE }); captures.push(Capture { ref_token: Token![ref](Span::call_site()), ident: field_name.clone(), }); - chain_commands.push(quote! { stream.append(#field_name)? }); + chain_commands.push(quote! { #field_name.serialise(stream)? }); } chain_commands.push_punct(Token![;](Span::call_site())); @@ -90,14 +90,11 @@ pub fn serialise_enum(data: &DataEnum) -> TokenStream { size_tests.push(quote! { { core::unreachable!(); } }); quote! { - const SERIALISED_SIZE: usize = const { #size_tests }; - - fn serialise(&self, buf: &mut [u8]) -> ::bzipper::Result<()> { - ::core::debug_assert_eq!(buf.len(), Self::SERIALISED_SIZE); - - let mut stream = ::bzipper::Sstream::new(buf); + const MAX_SERIALISED_SIZE: usize = const { #size_tests }; + fn serialise(&self, stream: &mut ::bzipper::Sstream) -> ::bzipper::Result<()> { match (*self) { #match_arms } + Ok(()) } } diff --git a/bzipper_macros/src/impls/serialise_struct.rs b/bzipper_macros/src/impls/serialise_struct.rs index 308a6bb..bd81a39 100644 --- a/bzipper_macros/src/impls/serialise_struct.rs +++ b/bzipper_macros/src/impls/serialise_struct.rs @@ -33,14 +33,10 @@ use syn::{ pub fn serialise_struct(data: &DataStruct) -> TokenStream { if matches!(data.fields, Fields::Unit) { quote! { - const SERIALISED_SIZE: usize = 0x0; + const MAX_SERIALISED_SIZE: usize = 0x0; #[inline(always)] - fn serialise(&self, buf: &mut [u8]) -> ::bzipper::Result<()> { - ::core::debug_assert_eq!(buf.len(), Self::SERIALISED_SIZE); - - Ok(()) - } + fn serialise(&self, stream: &mut ::bzipper::Sstream) -> ::bzipper::Result<()> { Ok(()) } } } else { let mut serialised_size = Punctuated::<TokenStream, Token![+]>::new(); @@ -53,21 +49,17 @@ pub fn serialise_struct(data: &DataStruct) -> TokenStream { .as_ref() .map_or_else(|| Index::from(index).to_token_stream(), ToTokens::to_token_stream); - serialised_size.push(quote! { <#ty as ::bzipper::Serialise>::SERIALISED_SIZE }); + serialised_size.push(quote! { <#ty as ::bzipper::Serialise>::MAX_SERIALISED_SIZE }); - chain_commands.push(quote! { stream.append(&self.#name)? }); + chain_commands.push(quote! { self.#name.serialise(stream)? }); } chain_commands.push_punct(Token![;](Span::call_site())); quote! { - const SERIALISED_SIZE: usize = #serialised_size; - - fn serialise(&self, buf: &mut [u8]) -> ::bzipper::Result<()> { - ::core::debug_assert_eq!(buf.len(), Self::SERIALISED_SIZE); - - let mut stream = ::bzipper::Sstream::new(buf); + const MAX_SERIALISED_SIZE: usize = #serialised_size; + fn serialise(&self, stream: &mut ::bzipper::Sstream) -> ::bzipper::Result<()> { #chain_commands Ok(()) |