diff options
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | bzipper/Cargo.toml | 4 | ||||
-rw-r--r-- | bzipper/src/deserialise/test.rs | 4 | ||||
-rw-r--r-- | bzipper_macros/Cargo.toml | 2 | ||||
-rw-r--r-- | bzipper_macros/src/impls/deserialise_enum.rs | 34 | ||||
-rw-r--r-- | bzipper_macros/src/impls/serialise_enum.rs | 46 |
6 files changed, 46 insertions, 49 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 94831b4..6ffd76f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ This is the changelog of bzipper. See `"README.md"` for more information. +## 0.6.2 + +* Fix `Deserialise` derive for unit variants +* Refactor `Serialise` derive for enumerations + ## 0.6.1 * Bump dependency version diff --git a/bzipper/Cargo.toml b/bzipper/Cargo.toml index 2a4bfe1..8f6f98d 100644 --- a/bzipper/Cargo.toml +++ b/bzipper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bzipper" -version = "0.6.1" +version = "0.6.2" edition = "2021" rust-version = "1.81" documentation = "https://docs.rs/bzipper/" @@ -20,7 +20,7 @@ alloc = [] std = [] [dependencies] -bzipper_macros = { path = "../bzipper_macros", version = "0.6.1"} +bzipper_macros = { path = "../bzipper_macros", version = "0.6.2"} [lints] workspace = true diff --git a/bzipper/src/deserialise/test.rs b/bzipper/src/deserialise/test.rs index f593978..3caebae 100644 --- a/bzipper/src/deserialise/test.rs +++ b/bzipper/src/deserialise/test.rs @@ -37,7 +37,7 @@ fn test() { #[derive(Debug, Deserialise, PartialEq, Serialise)] enum UnitOrFields { - Unit(Unit), + Unit, Unnamed(i32), Named { timestamp: u64 }, } @@ -104,7 +104,7 @@ fn test() { test!(UnitOrFields: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - ] => UnitOrFields::Unit(Unit)); + ] => UnitOrFields::Unit); test!(UnitOrFields: [ 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, diff --git a/bzipper_macros/Cargo.toml b/bzipper_macros/Cargo.toml index 30a6eab..5b72f9d 100644 --- a/bzipper_macros/Cargo.toml +++ b/bzipper_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bzipper_macros" -version = "0.6.1" +version = "0.6.2" edition = "2021" documentation = "https://docs.rs/bzipper_macros/" diff --git a/bzipper_macros/src/impls/deserialise_enum.rs b/bzipper_macros/src/impls/deserialise_enum.rs index 7bf0220..7065c86 100644 --- a/bzipper_macros/src/impls/deserialise_enum.rs +++ b/bzipper_macros/src/impls/deserialise_enum.rs @@ -35,29 +35,25 @@ pub fn deserialise_enum(data: &DataEnum) -> TokenStream { let discriminant = Discriminant::unwrap_from(index); - let block = if matches!(variant.fields, Fields::Unit) { - quote! { Self } - } else { - let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); + let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); - for field in &variant.fields { - let field_ty = &field.ty; + 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>()? } - ); + let command = field.ident + .as_ref() + .map_or_else( + || quote! { stream.take::<#field_ty>()? }, + |field_name| quote! { #field_name: stream.take::<#field_ty>()? } + ); - chain_commands.push(command); - } + chain_commands.push(command); + } - match variant.fields { - Fields::Named( ..) => quote! { Self::#variant_name { #chain_commands } }, - Fields::Unnamed(..) => quote! { Self::#variant_name(#chain_commands) }, - Fields::Unit => unreachable!(), - } + let block = 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 }); diff --git a/bzipper_macros/src/impls/serialise_enum.rs b/bzipper_macros/src/impls/serialise_enum.rs index 8f0693a..a554351 100644 --- a/bzipper_macros/src/impls/serialise_enum.rs +++ b/bzipper_macros/src/impls/serialise_enum.rs @@ -35,7 +35,7 @@ pub fn serialise_enum(data: &DataEnum) -> TokenStream { for (index, variant) in data.variants.iter().enumerate() { let mut serialised_size = Punctuated::<TokenStream, Token![+]>::new(); - let name = &variant.ident; + let variant_name = &variant.ident; let discriminant = u32::try_from(index) .expect("enumeration discriminants must be representable in `u32`"); @@ -43,38 +43,34 @@ pub fn serialise_enum(data: &DataEnum) -> TokenStream { // Discriminant size: serialised_size.push(quote! { <u32 as ::bzipper::Serialise>::SERIALISED_SIZE }); - let arm = if matches!(variant.fields, Fields::Unit) { - quote! { Self::#name => stream.append(&#discriminant)? } - } else { - let mut captures = Punctuated::<Capture, Token![,]>::new(); + let mut captures = Punctuated::<Capture, Token![,]>::new(); - let mut chain_commands = Punctuated::<TokenStream, Token![;]>::new(); - chain_commands.push(quote! { stream.append(&#discriminant)? }); + let mut chain_commands = Punctuated::<TokenStream, Token![;]>::new(); + chain_commands.push(quote! { stream.append(&#discriminant)? }); - for (index, field) in variant.fields.iter().enumerate() { - let field_ty = &field.ty; + 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); + 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>::SERIALISED_SIZE }); + serialised_size.push(quote! { <#field_ty as ::bzipper::Serialise>::SERIALISED_SIZE }); - captures.push(Capture { - ref_token: Token![ref](Span::call_site()), - ident: field_name.clone(), - }); + 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! { stream.append(#field_name)? }); + } - chain_commands.push_punct(Token![;](Span::call_site())); + chain_commands.push_punct(Token![;](Span::call_site())); - match variant.fields { - Fields::Named( ..) => quote! { Self::#name { #captures } => { #chain_commands } }, - Fields::Unnamed(..) => quote! { Self::#name(#captures) => { #chain_commands } }, - Fields::Unit => unreachable!(), - } + 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); |