Skip to content

Support versionize on packed structs #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/descriptors/struct_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,26 @@ impl Descriptor for StructDescriptor {
}

impl StructDescriptor {
pub fn new(input: &syn::DataStruct, ident: syn::Ident) -> Self {
pub fn new(input: &syn::DataStruct, ident: syn::Ident, packed_struct: bool) -> Self {
let mut descriptor = StructDescriptor {
ty: ident,
version: 1, // struct start at version 1.
fields: vec![],
};

// Fills self.fields.
descriptor.parse_struct_fields(&input.fields);
descriptor.parse_struct_fields(&input.fields, packed_struct);
descriptor.version = compute_version(&descriptor.fields);
descriptor
}

fn parse_struct_fields(&mut self, fields: &syn::Fields) {
fn parse_struct_fields(&mut self, fields: &syn::Fields, packed_struct: bool) {
match fields {
syn::Fields::Named(ref named_fields) => {
let pairs = named_fields.named.pairs();
for field in pairs.into_iter() {
self.fields.push(StructField::new(self.version, field));
self.fields
.push(StructField::new(self.version, field, packed_struct));
}
}
_ => panic!("Only named fields are supported."),
Expand Down
18 changes: 15 additions & 3 deletions src/fields/struct_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub(crate) struct StructField {
start_version: u16,
end_version: u16,
attrs: HashMap<String, syn::Lit>,
packed_struct: bool,
}

impl Exists for StructField {
Expand All @@ -36,6 +37,7 @@ impl StructField {
pub fn new(
base_version: u16,
ast_field: syn::punctuated::Pair<&syn::Field, &syn::token::Comma>,
packed_struct: bool,
) -> Self {
let attrs = parse_field_attributes(&ast_field.value().attrs);

Expand All @@ -45,6 +47,7 @@ impl StructField {
start_version: get_start_version(&attrs).unwrap_or(base_version),
end_version: get_end_version(&attrs).unwrap_or_default(),
attrs,
packed_struct,
}
}

Expand Down Expand Up @@ -87,9 +90,18 @@ impl StructField {
Versionize::serialize(element, writer, version_map, app_version)?;
}
},
syn::Type::Path(_) => quote! {
Versionize::serialize(&copy_of_self.#field_ident, writer, version_map, app_version)?;
},
syn::Type::Path(_) => {
if self.packed_struct {
quote! {
let copy = copy_of_self.#field_ident;
Versionize::serialize(&copy, writer, version_map, app_version)?;
}
} else {
quote! {
Versionize::serialize(&copy_of_self.#field_ident, writer, version_map, app_version)?;
}
}
}
syn::Type::Reference(_) => quote! {
copy_of_self.#field_ident.serialize(writer, version_map, app_version)?;
},
Expand Down
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ mod helpers;
use common::Descriptor;
use descriptors::{enum_desc::EnumDescriptor, struct_desc::StructDescriptor};
use proc_macro::TokenStream;
use proc_macro2::TokenTree;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
use syn::{parse::ParseStream, parse_macro_input, DeriveInput};

pub(crate) const ATTRIBUTE_NAME: &str = "version";

Expand Down Expand Up @@ -215,10 +216,26 @@ pub fn impl_versionize(input: TokenStream) -> proc_macro::TokenStream {
let ident = input.ident.clone();
let generics = input.generics.clone();

let descriptor: Box<dyn Descriptor> = match &input.data {
syn::Data::Struct(data_struct) => {
Box::new(StructDescriptor::new(&data_struct, ident.clone()))
let mut is_packed = false;
for attr in &input.attrs {
if attr.path.is_ident("repr") {
let _ = attr.parse_args_with(|input: ParseStream| {
while let Some(token) = input.parse()? {
if let TokenTree::Ident(ident) = token {
is_packed |= ident == "packed";
}
}
Ok(())
});
}
}

let descriptor: Box<dyn Descriptor> = match &input.data {
syn::Data::Struct(data_struct) => Box::new(StructDescriptor::new(
&data_struct,
ident.clone(),
is_packed,
)),
syn::Data::Enum(data_enum) => Box::new(EnumDescriptor::new(&data_enum, ident.clone())),
syn::Data::Union(_) => {
return (quote! {
Expand Down