treat and parse structs without repr attributes as unit structs

This commit is contained in:
yamachu 2023-09-06 19:30:21 +09:00
parent 7b614935e5
commit dd6330a573

View File

@ -170,23 +170,42 @@ pub fn collect_struct(ast: &syn::File, result: &mut Vec<RustStruct>) {
is_union: true, is_union: true,
}); });
} else if let Item::Struct(t) = item { } else if let Item::Struct(t) = item {
if let syn::Fields::Named(f) = &t.fields { let mut repr = None;
let struct_name = t.ident.to_string(); for attr in &t.attrs {
let fields = collect_fields(f); let last_segment = attr.path.segments.last().unwrap();
result.push(RustStruct { if last_segment.ident == "repr" {
struct_name, repr = Some(attr.tokens.to_string());
fields, }
is_union: false, }
});
} else if let syn::Fields::Unnamed(f) = &t.fields { if let Some(_) = repr {
let struct_name = t.ident.to_string(); if let syn::Fields::Named(f) = &t.fields {
let fields = collect_fields_unnamed(f); let struct_name = t.ident.to_string();
result.push(RustStruct { let fields = collect_fields(f);
struct_name, result.push(RustStruct {
fields, struct_name,
is_union: false, fields,
}); is_union: false,
} else if let syn::Fields::Unit = &t.fields { });
} else if let syn::Fields::Unnamed(f) = &t.fields {
let struct_name = t.ident.to_string();
let fields = collect_fields_unnamed(f);
result.push(RustStruct {
struct_name,
fields,
is_union: false,
});
} else if let syn::Fields::Unit = &t.fields {
let struct_name = t.ident.to_string();
let fields: Vec<FieldMember> = Vec::new();
result.push(RustStruct {
struct_name,
fields,
is_union: false,
});
}
} else {
// non #[repr(?)] struct, treat as Unit struct
let struct_name = t.ident.to_string(); let struct_name = t.ident.to_string();
let fields: Vec<FieldMember> = Vec::new(); let fields: Vec<FieldMember> = Vec::new();
result.push(RustStruct { result.push(RustStruct {