diff --git a/csbindgen-tests/src/lib.rs b/csbindgen-tests/src/lib.rs index 0b35ff6..1e73fb1 100644 --- a/csbindgen-tests/src/lib.rs +++ b/csbindgen-tests/src/lib.rs @@ -124,6 +124,16 @@ pub extern "C" fn comment_one() { pub extern "C" fn long_jpn_comment() { } + + +#[repr(C)] +pub struct my_int_vec3(i32,i32,i32); + +pub extern "C" fn use_vec3(_v3: my_int_vec3) { + +} + + #[repr(C)] pub struct NfcCard { pub delegate: unsafe extern "C" fn(ByteArray) -> ByteArray diff --git a/csbindgen/src/parser.rs b/csbindgen/src/parser.rs index f47c569..fba9e34 100644 --- a/csbindgen/src/parser.rs +++ b/csbindgen/src/parser.rs @@ -1,6 +1,6 @@ use crate::{alias_map::AliasMap, builder::BindgenOptions, field_map::FieldMap, type_meta::*}; use regex::Regex; -use std::{collections::HashSet, fmt::format}; +use std::{collections::HashSet}; use syn::{ForeignItem, Item, Pat, ReturnType}; enum FnItem { @@ -156,7 +156,15 @@ pub fn collect_struct(ast: &syn::File, result: &mut Vec) { 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, + }); + } } } } @@ -177,6 +185,23 @@ fn collect_fields(fields: &syn::FieldsNamed) -> Vec { result } +fn collect_fields_unnamed(fields: &syn::FieldsUnnamed) -> Vec { + let mut result = Vec::new(); + + let mut i = 0; + for field in &fields.unnamed { + i += 1; + let name = format!("Item{i}"); + let t = parse_type(&field.ty); + result.push(FieldMember { + name: name, + rust_type: t, + }); + } + + result +} + pub fn collect_enum(ast: &syn::File, result: &mut Vec) { for item in ast.items.iter() { if let Item::Enum(t) = item { @@ -208,7 +233,7 @@ pub fn collect_enum(ast: &syn::File, result: &mut Vec) { enum_name, fields, repr, - is_flags: false + is_flags: false, }); } else if let Item::Macro(t) = item { let last_segment = t.mac.path.segments.last().unwrap(); @@ -238,7 +263,16 @@ pub fn collect_enum(ast: &syn::File, result: &mut Vec) { .map(|x| { ( x.get(1).unwrap().as_str().to_string(), - Some(x.get(2).unwrap().as_str().to_string().replace("Self :: ", "").replace(" . bits", "").trim().to_string()), + Some( + x.get(2) + .unwrap() + .as_str() + .to_string() + .replace("Self :: ", "") + .replace(" . bits", "") + .trim() + .to_string(), + ), ) }) .collect::>(); @@ -247,7 +281,7 @@ pub fn collect_enum(ast: &syn::File, result: &mut Vec) { enum_name, fields, repr, - is_flags: true + is_flags: true, }); } } diff --git a/dotnet-sandbox/NativeMethods.cs b/dotnet-sandbox/NativeMethods.cs index c6362c5..707df9f 100644 --- a/dotnet-sandbox/NativeMethods.cs +++ b/dotnet-sandbox/NativeMethods.cs @@ -24,6 +24,9 @@ namespace CsBindgen [DllImport(__DllName, EntryPoint = "long_jpn_comment", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern void long_jpn_comment(); + [DllImport(__DllName, EntryPoint = "use_vec3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void use_vec3(my_int_vec3 _v3); + [DllImport(__DllName, EntryPoint = "other_2", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern void other_2(NfcCard _hoge); @@ -157,6 +160,14 @@ namespace CsBindgen public fixed byte png_name[5]; } + [StructLayout(LayoutKind.Sequential)] + internal unsafe partial struct my_int_vec3 + { + public int Item1; + public int Item2; + public int Item3; + } + [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct NfcCard {