struct bool emit MarshalAs

This commit is contained in:
neuecc 2023-03-04 14:54:48 +09:00
parent 4dba240d2b
commit dabad3323a
3 changed files with 46 additions and 17 deletions

View File

@ -1,5 +1,3 @@
use std::io::Bytes;
#[allow(dead_code)] #[allow(dead_code)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
@ -34,11 +32,11 @@ pub extern "C" fn my_bool(
true true
} }
#[no_mangle] // #[no_mangle]
pub unsafe extern "C" fn new(x: *mut *mut Vec<u8>) { // pub unsafe extern "C" fn new(x: *mut *mut Vec<u8>) {
let v = Box::new(Vec::new()); // let v = Box::new(Vec::new());
*x = Box::into_raw(v); // *x = Box::into_raw(v);
} // }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn unsafe_return_string() -> *const u8 { pub unsafe extern "C" fn unsafe_return_string() -> *const u8 {
@ -55,6 +53,23 @@ pub extern "C" fn unsafe_destroy_string(s: *mut String) {
unsafe { Box::from_raw(s) }; unsafe { Box::from_raw(s) };
} }
#[no_mangle]
pub extern "C" fn create_context() -> *mut Context {
let ctx = Box::new(Context { foo: true });
Box::into_raw(ctx)
}
#[no_mangle]
pub extern "C" fn delete_context(context: *mut Context) {
unsafe { Box::from_raw(context) };
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Context {
pub foo: bool,
}
#[test] #[test]
fn build_test() { fn build_test() {
// let path = std::env::current_dir().unwrap(); // let path = std::env::current_dir().unwrap();

View File

@ -154,20 +154,22 @@ pub fn emit_csharp(
structs_string structs_string
.push_str_ln(format!(" [StructLayout(LayoutKind.{layout_kind})]").as_str()); .push_str_ln(format!(" [StructLayout(LayoutKind.{layout_kind})]").as_str());
structs_string.push_str_ln(format!(" public unsafe struct {name}").as_str()); structs_string.push_str_ln(format!(" {accessibility} unsafe struct {name}").as_str());
structs_string.push_str_ln(" {"); structs_string.push_str_ln(" {");
for field in &item.fields { for field in &item.fields {
if item.is_union { if item.is_union {
structs_string.push_str_ln(" [FieldOffset(0)]"); structs_string.push_str_ln(" [FieldOffset(0)]");
} }
structs_string.push_str(
format!( let type_name = field.rust_type.to_csharp_string(options, aliases);
" public {} {}", let attr = if type_name == "bool" {
field.rust_type.to_csharp_string(options, aliases), "[MarshalAs(UnmanagedType.U1)] ".to_string()
field.name } else {
) "".to_string()
.as_str(), };
);
structs_string
.push_str(format!(" {}public {} {}", attr, type_name, field.name).as_str());
if field.rust_type.is_fixed_array { if field.rust_type.is_fixed_array {
let mut digits = field.rust_type.fixed_array_digits.clone(); let mut digits = field.rust_type.fixed_array_digits.clone();
if digits == "0" { if digits == "0" {

View File

@ -27,7 +27,19 @@ namespace CsBindgen
[DllImport(__DllName, EntryPoint = "unsafe_destroy_string", CallingConvention = CallingConvention.Cdecl)] [DllImport(__DllName, EntryPoint = "unsafe_destroy_string", CallingConvention = CallingConvention.Cdecl)]
public static extern void unsafe_destroy_string(String* s); public static extern void unsafe_destroy_string(String* s);
[DllImport(__DllName, EntryPoint = "create_context", CallingConvention = CallingConvention.Cdecl)]
public static extern Context* create_context();
[DllImport(__DllName, EntryPoint = "delete_context", CallingConvention = CallingConvention.Cdecl)]
public static extern void delete_context(Context* context);
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct Context
{
[MarshalAs(UnmanagedType.U1)] public bool foo;
} }