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(non_snake_case)]
#[allow(non_camel_case_types)]
@ -34,15 +32,15 @@ pub extern "C" fn my_bool(
true
}
#[no_mangle]
pub unsafe extern "C" fn new(x: *mut *mut Vec<u8>) {
let v = Box::new(Vec::new());
*x = Box::into_raw(v);
}
// #[no_mangle]
// pub unsafe extern "C" fn new(x: *mut *mut Vec<u8>) {
// let v = Box::new(Vec::new());
// *x = Box::into_raw(v);
// }
#[no_mangle]
pub unsafe extern "C" fn unsafe_return_string() -> *const u8 {
todo!();
todo!();
}
#[no_mangle]
@ -55,6 +53,23 @@ pub extern "C" fn unsafe_destroy_string(s: *mut String) {
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]
fn build_test() {
// let path = std::env::current_dir().unwrap();

View File

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

View File

@ -27,7 +27,19 @@ namespace CsBindgen
[DllImport(__DllName, EntryPoint = "unsafe_destroy_string", CallingConvention = CallingConvention.Cdecl)]
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;
}