Fix 'Deserialise' derive for unit variants; Refactor 'Serialise' derive for enumerations;
This commit is contained in:
parent
1d14168c30
commit
3b29e72624
6 changed files with 46 additions and 49 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "bzipper_macros"
|
||||
version = "0.6.1"
|
||||
version = "0.6.2"
|
||||
edition = "2021"
|
||||
documentation = "https://docs.rs/bzipper_macros/"
|
||||
|
||||
|
|
|
@ -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 });
|
||||
|
|
|
@ -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),
|
||||
ident: field_name.clone(),
|
||||
});
|
||||
captures.push(Capture {
|
||||
ref_token: Token),
|
||||
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));
|
||||
chain_commands.push_punct(Token));
|
||||
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue