Merge pull request #45 from yamachu/non-repr-attribute-struct-treat-as-empty-struct

Non repr attribute struct treat as unit struct
This commit is contained in:
Yoshifumi Kawai 2023-09-07 17:19:12 +09:00 committed by GitHub
commit 47458c0f0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 17 deletions

View File

@ -606,6 +606,20 @@ pub struct CallbackTable {
pub foobar: extern "C" fn(i: i32) -> i32,
}
pub struct InternalHiddenContext {
pub a: i32
}
pub struct TreatAsEmptyStruct {
internal: std::sync::Arc<InternalHiddenContext>
}
#[no_mangle]
pub unsafe extern "C" fn init_treat_as_empty_struct_context(_out: NonNull<Box<TreatAsEmptyStruct>>) {}
#[no_mangle]
pub unsafe extern "C" fn free_treat_as_empty_struct_context(_src: *mut TreatAsEmptyStruct) {}
// fn run_physix(){
// unsafe {
// let foundation = physx_create_foundation();

View File

@ -170,23 +170,42 @@ pub fn collect_struct(ast: &syn::File, result: &mut Vec<RustStruct>) {
is_union: true,
});
} else if let Item::Struct(t) = item {
if let syn::Fields::Named(f) = &t.fields {
let struct_name = t.ident.to_string();
let fields = collect_fields(f);
result.push(RustStruct {
struct_name,
fields,
is_union: false,
});
} 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 mut repr = None;
for attr in &t.attrs {
let last_segment = attr.path.segments.last().unwrap();
if last_segment.ident == "repr" {
repr = Some(attr.tokens.to_string());
}
}
if let Some(_) = repr {
if let syn::Fields::Named(f) = &t.fields {
let struct_name = t.ident.to_string();
let fields = collect_fields(f);
result.push(RustStruct {
struct_name,
fields,
is_union: false,
});
} 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 fields: Vec<FieldMember> = Vec::new();
result.push(RustStruct {

View File

@ -176,6 +176,12 @@ namespace CsBindgen
[DllImport(__DllName, EntryPoint = "call_bindgen_lz4", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void call_bindgen_lz4();
[DllImport(__DllName, EntryPoint = "init_treat_as_empty_struct_context", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void init_treat_as_empty_struct_context(TreatAsEmptyStruct** _out);
[DllImport(__DllName, EntryPoint = "free_treat_as_empty_struct_context", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void free_treat_as_empty_struct_context(TreatAsEmptyStruct* _src);
}
@ -260,6 +266,11 @@ namespace CsBindgen
public delegate* unmanaged[Cdecl]<int, int> foobar;
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe partial struct TreatAsEmptyStruct
{
}
[Flags]
internal enum EnumFlags : uint