diff options
Diffstat (limited to 'bzipper_macros/src/impls')
-rw-r--r-- | bzipper_macros/src/impls/decode_enum.rs | 67 | ||||
-rw-r--r-- | bzipper_macros/src/impls/decode_struct.rs | 55 | ||||
-rw-r--r-- | bzipper_macros/src/impls/deserialise_enum.rs | 68 | ||||
-rw-r--r-- | bzipper_macros/src/impls/deserialise_struct.rs | 61 | ||||
-rw-r--r-- | bzipper_macros/src/impls/encode_enum.rs | 77 | ||||
-rw-r--r-- | bzipper_macros/src/impls/encode_struct.rs | 46 | ||||
-rw-r--r-- | bzipper_macros/src/impls/mod.rs | 18 | ||||
-rw-r--r-- | bzipper_macros/src/impls/serialise_enum.rs | 101 | ||||
-rw-r--r-- | bzipper_macros/src/impls/serialise_struct.rs | 69 | ||||
-rw-r--r-- | bzipper_macros/src/impls/sized_encode_enum.rs | 54 | ||||
-rw-r--r-- | bzipper_macros/src/impls/sized_encode_struct.rs | 37 |
11 files changed, 346 insertions, 307 deletions
diff --git a/bzipper_macros/src/impls/decode_enum.rs b/bzipper_macros/src/impls/decode_enum.rs new file mode 100644 index 0000000..c773de2 --- /dev/null +++ b/bzipper_macros/src/impls/decode_enum.rs @@ -0,0 +1,67 @@ +// Copyright 2024 Gabriel Bjørnager Jensen. +// +// This file is part of bZipper. +// +// bZipper is free software: you can redistribute +// it and/or modify it under the terms of the GNU +// Lesser General Public License as published by +// the Free Software Foundation, either version 3 +// of the License, or (at your option) any later +// version. +// +// bZipper is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without +// even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Less- +// er General Public License along with bZipper. If +// not, see <https://www.gnu.org/licenses/>. + +use crate::DiscriminantIter; + +use proc_macro2::TokenStream; +use quote::quote; +use syn::{DataEnum, Fields, Token, Variant}; +use syn::punctuated::Punctuated; + +#[must_use] +pub fn decode_enum(data: &DataEnum) -> TokenStream { + let mut match_arms = Punctuated::<TokenStream, Token![,]>::new(); + + for (discriminant, variant) in DiscriminantIter::new(&data.variants) { + let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); + + for field in &variant.fields { + let command = field.ident + .as_ref() + .map_or_else( + || quote! { ::bzipper::Decode::decode(stream)? }, + |field_name| quote! { #field_name: ::bzipper::Decode::decode(stream)? }, + ); + + chain_commands.push(command); + } + + let value = match *variant { + Variant { ident: ref variant_name, fields: Fields::Named( ..), .. } => quote! { Self::#variant_name { #chain_commands } }, + Variant { ident: ref variant_name, fields: Fields::Unnamed(..), .. } => quote! { Self::#variant_name(#chain_commands) }, + Variant { ident: ref variant_name, fields: Fields::Unit, .. } => quote! { Self::#variant_name }, + }; + + match_arms.push(quote! { #discriminant => #value }); + } + + match_arms.push(quote! { + value => return ::core::result::Result::Err(::bzipper::error::DecodeError::InvalidDiscriminant(value)) + }); + + quote! { + #[inline] + fn decode(stream: &mut ::bzipper::IStream) -> ::core::result::Result<Self, ::bzipper::error::DecodeError> { + let value = match (<isize as ::bzipper::Decode>::decode(stream)?) { #match_arms }; + Ok(value) + } + } +} diff --git a/bzipper_macros/src/impls/decode_struct.rs b/bzipper_macros/src/impls/decode_struct.rs new file mode 100644 index 0000000..9688e91 --- /dev/null +++ b/bzipper_macros/src/impls/decode_struct.rs @@ -0,0 +1,55 @@ +// Copyright 2024 Gabriel Bjørnager Jensen. +// +// This file is part of bZipper. +// +// bZipper is free software: you can redistribute +// it and/or modify it under the terms of the GNU +// Lesser General Public License as published by +// the Free Software Foundation, either version 3 +// of the License, or (at your option) any later +// version. +// +// bZipper is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without +// even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Less- +// er General Public License along with bZipper. If +// not, see <https://www.gnu.org/licenses/>. + +use proc_macro2::TokenStream; +use quote::quote; +use syn::{DataStruct, Fields, Token}; +use syn::punctuated::Punctuated; + +#[must_use] +pub fn decode_struct(data: &DataStruct) -> TokenStream { + let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); + + for field in &data.fields { + let command = field.ident + .as_ref() + .map_or_else( + || quote! { ::bzipper::Decode::decode(stream)? }, + |field_name| quote! { #field_name: ::bzipper::Decode::decode(stream)? }, + ); + + chain_commands.push(command); + } + + let value = match data.fields { + Fields::Named( ..) => quote! { Self { #chain_commands } }, + Fields::Unnamed(..) => quote! { Self(#chain_commands) }, + Fields::Unit => quote! { Self }, + }; + + quote! { + #[inline] + fn decode(stream: &mut ::bzipper::IStream) -> ::core::result::Result<Self, ::bzipper::error::DecodeError> { + let value = #value; + Ok(value) + } + } +} diff --git a/bzipper_macros/src/impls/deserialise_enum.rs b/bzipper_macros/src/impls/deserialise_enum.rs deleted file mode 100644 index 4c88a41..0000000 --- a/bzipper_macros/src/impls/deserialise_enum.rs +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2024 Gabriel Bjørnager Jensen. -// -// This file is part of bzipper. -// -// bzipper is free software: you can redistribute -// it and/or modify it under the terms of the GNU -// Lesser General Public License as published by -// the Free Software Foundation, either version 3 -// of the License, or (at your option) any later -// version. -// -// bzipper is distributed in the hope that it will -// be useful, but WITHOUT ANY WARRANTY; without -// even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Less- -// er General Public License along with bzipper. If -// not, see <https://www.gnu.org/licenses/>. - -use crate::Discriminant; - -use proc_macro2::TokenStream; -use quote::quote; -use syn::{DataEnum, Fields, Token}; -use syn::punctuated::Punctuated; - -#[must_use] -pub fn deserialise_enum(data: &DataEnum) -> TokenStream { - let mut match_arms = Punctuated::<TokenStream, Token![,]>::new(); - - for (index, variant) in data.variants.iter().enumerate() { - let variant_name = &variant.ident; - - let discriminant = Discriminant::unwrap_from(index); - - let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); - - for field in &variant.fields { - 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); - } - - 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 => #value }); - } - - match_arms.push(quote! { value => return Err(::bzipper::Error::InvalidDiscriminant(value)) }); - - quote! { - 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 deleted file mode 100644 index f8c167b..0000000 --- a/bzipper_macros/src/impls/deserialise_struct.rs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2024 Gabriel Bjørnager Jensen. -// -// This file is part of bzipper. -// -// bzipper is free software: you can redistribute -// it and/or modify it under the terms of the GNU -// Lesser General Public License as published by -// the Free Software Foundation, either version 3 -// of the License, or (at your option) any later -// version. -// -// bzipper is distributed in the hope that it will -// be useful, but WITHOUT ANY WARRANTY; without -// even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Less- -// er General Public License along with bzipper. If -// not, see <https://www.gnu.org/licenses/>. - -use proc_macro2::TokenStream; -use quote::quote; -use syn::{DataStruct, Fields, Token}; -use syn::punctuated::Punctuated; - -#[must_use] -pub fn deserialise_struct(data: &DataStruct) -> TokenStream { - if matches!(data.fields, Fields::Unit) { - quote! { - #[inline(always)] - fn deserialise(_stream: &::bzipper::Dstream) -> ::bzipper::Result<Self> { Ok(Self) } - } - } else { - let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); - - for field in &data.fields { - 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); - } - - let value = if let Fields::Named(..) = data.fields { - quote! { Self { #chain_commands } } - } else { - quote! { Self(#chain_commands) } - }; - - quote! { - fn deserialise(stream: &::bzipper::Dstream) -> ::bzipper::Result<Self> { - let value = #value; - Ok(value) - } - } - } -} diff --git a/bzipper_macros/src/impls/encode_enum.rs b/bzipper_macros/src/impls/encode_enum.rs new file mode 100644 index 0000000..37acd34 --- /dev/null +++ b/bzipper_macros/src/impls/encode_enum.rs @@ -0,0 +1,77 @@ +// Copyright 2024 Gabriel Bjørnager Jensen. +// +// This file is part of bZipper. +// +// bZipper is free software: you can redistribute +// it and/or modify it under the terms of the GNU +// Lesser General Public License as published by +// the Free Software Foundation, either version 3 +// of the License, or (at your option) any later +// version. +// +// bZipper is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without +// even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Less- +// er General Public License along with bZipper. If +// not, see <https://www.gnu.org/licenses/>. + +use crate::DiscriminantIter; + +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use syn::{ + DataEnum, + Fields, + Ident, + Variant, +}; + +#[must_use] +pub fn encode_enum(data: &DataEnum) -> TokenStream { + let mut match_arms = Vec::new(); + + // Iterate over each variant and give it a unique + // encoding scheme. + for (discriminant, variant) in DiscriminantIter::new(&data.variants) { + // The original identifiers of the fields: + let mut field_names = Vec::new(); + + // The captured field identifiers: + let mut field_captures = Vec::new(); + + for (index, field) in variant.fields.iter().enumerate() { + let capture = Ident::new(&format!("v{index}"), Span::call_site()); + + field_names.push(&field.ident); + field_captures.push(capture); + } + + let pattern = match *variant { + Variant { ident: ref variant_name, fields: Fields::Named( ..), .. } => quote! { Self::#variant_name { #(#field_names: ref #field_captures, )* } }, + Variant { ident: ref variant_name, fields: Fields::Unnamed(..), .. } => quote! { Self::#variant_name(#(ref #field_captures)*) }, + Variant { ident: ref variant_name, fields: Fields::Unit, .. } => quote! { Self::#variant_name }, + }; + + match_arms.push(quote! { + #pattern => { + ::bzipper::Encode::encode(&#discriminant, stream)?; + #(::bzipper::Encode::encode(#field_captures, stream)?;)* + } + }); + } + + quote! { + #[inline] + fn encode(&self, stream: &mut ::bzipper::OStream) -> ::core::result::Result<(), ::bzipper::error::EncodeError> { + match *self { + #(#match_arms)* + } + + Ok(()) + } + } +} diff --git a/bzipper_macros/src/impls/encode_struct.rs b/bzipper_macros/src/impls/encode_struct.rs new file mode 100644 index 0000000..e853e44 --- /dev/null +++ b/bzipper_macros/src/impls/encode_struct.rs @@ -0,0 +1,46 @@ +// Copyright 2024 Gabriel Bjørnager Jensen. +// +// This file is part of bZipper. +// +// bZipper is free software: you can redistribute +// it and/or modify it under the terms of the GNU +// Lesser General Public License as published by +// the Free Software Foundation, either version 3 +// of the License, or (at your option) any later +// version. +// +// bZipper is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without +// even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Less- +// er General Public License along with bZipper. If +// not, see <https://www.gnu.org/licenses/>. + +use proc_macro2::TokenStream; +use quote::{quote, ToTokens}; +use syn::{DataStruct, Index}; + +#[must_use] +pub fn encode_struct(data: &DataStruct) -> TokenStream { + let mut fields = Vec::new(); + + for (index, field) in data.fields.iter().enumerate() { + let name = field.ident + .as_ref() + .map_or_else(|| Index::from(index).to_token_stream(), ToTokens::to_token_stream); + + fields.push(name); + } + + quote! { + #[inline] + fn encode(&self, stream: &mut ::bzipper::OStream) -> ::core::result::Result<(), ::bzipper::error::EncodeError> { + #(::bzipper::Encode::encode(&self.#fields, stream)?;)* + + Ok(()) + } + } +} diff --git a/bzipper_macros/src/impls/mod.rs b/bzipper_macros/src/impls/mod.rs index d61cf90..cdbc9ac 100644 --- a/bzipper_macros/src/impls/mod.rs +++ b/bzipper_macros/src/impls/mod.rs @@ -1,26 +1,28 @@ // Copyright 2024 Gabriel Bjørnager Jensen. // -// This file is part of bzipper. +// This file is part of bZipper. // -// bzipper is free software: you can redistribute +// bZipper is free software: you can redistribute // it and/or modify it under the terms of the GNU // Lesser General Public License as published by // the Free Software Foundation, either version 3 // of the License, or (at your option) any later // version. // -// bzipper is distributed in the hope that it will +// bZipper is distributed in the hope that it will // be useful, but WITHOUT ANY WARRANTY; without // even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Less- -// er General Public License along with bzipper. If +// er General Public License along with bZipper. If // not, see <https://www.gnu.org/licenses/>. use crate::use_mod; -use_mod!(pub deserialise_enum); -use_mod!(pub deserialise_struct); -use_mod!(pub serialise_enum); -use_mod!(pub serialise_struct); +use_mod!(pub decode_enum); +use_mod!(pub decode_struct); +use_mod!(pub encode_enum); +use_mod!(pub encode_struct); +use_mod!(pub sized_encode_enum); +use_mod!(pub sized_encode_struct); diff --git a/bzipper_macros/src/impls/serialise_enum.rs b/bzipper_macros/src/impls/serialise_enum.rs deleted file mode 100644 index 825886c..0000000 --- a/bzipper_macros/src/impls/serialise_enum.rs +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2024 Gabriel Bjørnager Jensen. -// -// This file is part of bzipper. -// -// bzipper is free software: you can redistribute -// it and/or modify it under the terms of the GNU -// Lesser General Public License as published by -// the Free Software Foundation, either version 3 -// of the License, or (at your option) any later -// version. -// -// bzipper is distributed in the hope that it will -// be useful, but WITHOUT ANY WARRANTY; without -// even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Less- -// er General Public License along with bzipper. If -// not, see <https://www.gnu.org/licenses/>. - -use crate::Capture; - -use proc_macro2::{Span, TokenStream}; -use quote::quote; -use syn::{DataEnum, Fields, Ident, Token}; -use syn::punctuated::Punctuated; - -#[must_use] -pub fn serialise_enum(data: &DataEnum) -> TokenStream { - let mut sizes = Vec::new(); - - let mut match_arms = Punctuated::<TokenStream, Token![,]>::new(); - - for (index, variant) in data.variants.iter().enumerate() { - let mut serialised_size = Punctuated::<TokenStream, Token![+]>::new(); - - let variant_name = &variant.ident; - - let discriminant = u32::try_from(index) - .expect("enumeration discriminants must be representable as `u32`"); - - // Discriminant 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! { #discriminant.serialise(stream)? }); - - for (index, field) in variant.fields.iter().enumerate() { - let field_ty = &field.ty; - - let field_name = field.ident - .as_ref() - .map_or_else(|| Ident::new(&format!("v{index}"), Span::call_site()), Clone::clone); - - 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! { #field_name.serialise(stream)? }); - } - - chain_commands.push_punct(Token![;](Span::call_site())); - - let arm = match variant.fields { - Fields::Named( ..) => quote! { Self::#variant_name { #captures } => { #chain_commands } }, - Fields::Unnamed(..) => quote! { Self::#variant_name(#captures) => { #chain_commands } }, - Fields::Unit => quote! { Self::#variant_name => { #chain_commands } }, - }; - - sizes.push(serialised_size); - match_arms.push(arm); - } - - let mut size_tests = Punctuated::<TokenStream, Token![else]>::new(); - - for size in &sizes { - let mut test = Punctuated::<TokenStream, Token![&&]>::new(); - - for other_size in &sizes { test.push(quote! { #size >= #other_size }) } - - size_tests.push(quote! { if #test { #size } }); - } - - size_tests.push(quote! { { core::unreachable!(); } }); - - quote! { - 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 deleted file mode 100644 index bd81a39..0000000 --- a/bzipper_macros/src/impls/serialise_struct.rs +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2024 Gabriel Bjørnager Jensen. -// -// This file is part of bzipper. -// -// bzipper is free software: you can redistribute -// it and/or modify it under the terms of the GNU -// Lesser General Public License as published by -// the Free Software Foundation, either version 3 -// of the License, or (at your option) any later -// version. -// -// bzipper is distributed in the hope that it will -// be useful, but WITHOUT ANY WARRANTY; without -// even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Less- -// er General Public License along with bzipper. If -// not, see <https://www.gnu.org/licenses/>. - -use proc_macro2::{Span, TokenStream}; -use quote::{quote, ToTokens}; -use syn::{ - DataStruct, - Fields, - Index, - Token, - punctuated::Punctuated -}; - -#[must_use] -pub fn serialise_struct(data: &DataStruct) -> TokenStream { - if matches!(data.fields, Fields::Unit) { - quote! { - const MAX_SERIALISED_SIZE: usize = 0x0; - - #[inline(always)] - fn serialise(&self, stream: &mut ::bzipper::Sstream) -> ::bzipper::Result<()> { Ok(()) } - } - } else { - let mut serialised_size = Punctuated::<TokenStream, Token![+]>::new(); - let mut chain_commands = Punctuated::<TokenStream, Token![;]>::new(); - - for (index, field) in data.fields.iter().enumerate() { - let ty = &field.ty; - - let name = field.ident - .as_ref() - .map_or_else(|| Index::from(index).to_token_stream(), ToTokens::to_token_stream); - - serialised_size.push(quote! { <#ty as ::bzipper::Serialise>::MAX_SERIALISED_SIZE }); - - chain_commands.push(quote! { self.#name.serialise(stream)? }); - } - - chain_commands.push_punct(Token![;](Span::call_site())); - - quote! { - const MAX_SERIALISED_SIZE: usize = #serialised_size; - - fn serialise(&self, stream: &mut ::bzipper::Sstream) -> ::bzipper::Result<()> { - #chain_commands - - Ok(()) - } - } - } -} diff --git a/bzipper_macros/src/impls/sized_encode_enum.rs b/bzipper_macros/src/impls/sized_encode_enum.rs new file mode 100644 index 0000000..3bfc961 --- /dev/null +++ b/bzipper_macros/src/impls/sized_encode_enum.rs @@ -0,0 +1,54 @@ +// Copyright 2024 Gabriel Bjørnager Jensen. +// +// This file is part of bZipper. +// +// bZipper is free software: you can redistribute +// it and/or modify it under the terms of the GNU +// Lesser General Public License as published by +// the Free Software Foundation, either version 3 +// of the License, or (at your option) any later +// version. +// +// bZipper is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without +// even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Less- +// er General Public License along with bZipper. If +// not, see <https://www.gnu.org/licenses/>. + +use proc_macro2::TokenStream; +use quote::quote; +use syn::DataEnum; + +#[must_use] +pub fn sized_encode_enum(data: &DataEnum) -> TokenStream { + let mut sizes = Vec::new(); + + // Iterate over each variant and give it a unique + // encoding scheme. + for variant in &data.variants { + let mut field_tys = Vec::new(); + + for field in &variant.fields { + field_tys.push(&field.ty); + } + + sizes.push(quote! { + <isize as ::bzipper::SizedEncode>::MAX_ENCODED_SIZE + #(+ <#field_tys as ::bzipper::SizedEncode>::MAX_ENCODED_SIZE)* + }); + } + + quote! { + const MAX_ENCODED_SIZE: usize = const { + let mut max_encoded_size = 0x0usize; + + #(if #sizes > max_encoded_size { max_encoded_size = #sizes };)* + + max_encoded_size + }; + } +} diff --git a/bzipper_macros/src/impls/sized_encode_struct.rs b/bzipper_macros/src/impls/sized_encode_struct.rs new file mode 100644 index 0000000..e194e08 --- /dev/null +++ b/bzipper_macros/src/impls/sized_encode_struct.rs @@ -0,0 +1,37 @@ +// Copyright 2024 Gabriel Bjørnager Jensen. +// +// This file is part of bZipper. +// +// bZipper is free software: you can redistribute +// it and/or modify it under the terms of the GNU +// Lesser General Public License as published by +// the Free Software Foundation, either version 3 +// of the License, or (at your option) any later +// version. +// +// bZipper is distributed in the hope that it will +// be useful, but WITHOUT ANY WARRANTY; without +// even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Less- +// er General Public License along with bZipper. If +// not, see <https://www.gnu.org/licenses/>. + +use proc_macro2::TokenStream; +use quote::quote; +use syn::DataStruct; + +#[must_use] +pub fn sized_encode_struct(data: &DataStruct) -> TokenStream { + let mut field_tys = Vec::new(); + + for field in &data.fields { + field_tys.push(&field.ty); + } + + quote! { + const MAX_ENCODED_SIZE: usize = 0x0 #( + <#field_tys as ::bzipper::SizedEncode>::MAX_ENCODED_SIZE)*; + } +} |