format rust -> rust generated code

This commit is contained in:
neuecc 2023-03-04 14:39:45 +09:00
parent 2ff100f718
commit 4dba240d2b
8 changed files with 637 additions and 755 deletions

View File

@ -40,23 +40,23 @@ fn main() {
// .generate()?
// .write_to_file("src/bullet3.rs")?;
// csbindgen::Builder::default()
// .input_bindgen_file("src/lz4.rs")
// .method_filter(|x| { !x.starts_with("_") && !x.starts_with("XXH") } )
// .rust_method_prefix("csbindgen_")
// .rust_file_header("use super::lz4;")
// .rust_method_type_path("lz4")
// .csharp_class_name("LibLz4")
// .csharp_namespace("CsBindgen")
// .csharp_dll_name("csbindgen_tests")
// .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal")
// .csharp_entry_point_prefix("csbindgen_")
// .csharp_method_prefix("")
// .csharp_class_accessibility("public")
// .csharp_c_long_convert("int")
// .csharp_c_ulong_convert("uint")
// .generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs")
// .unwrap();
csbindgen::Builder::default()
.input_bindgen_file("src/lz4.rs")
.method_filter(|x| { x.starts_with("LZ4")} )
.rust_method_prefix("csbindgen_")
.rust_file_header("use super::lz4;")
.rust_method_type_path("lz4")
.csharp_class_name("LibLz4")
.csharp_namespace("CsBindgen")
.csharp_dll_name("csbindgen_tests")
.csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal")
.csharp_entry_point_prefix("csbindgen_")
.csharp_method_prefix("")
.csharp_class_accessibility("public")
.csharp_c_long_convert("int")
.csharp_c_ulong_convert("uint")
.generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs")
.unwrap();
csbindgen::Builder::default()
.input_extern_file("src/lib.rs")

View File

@ -1,3 +1,5 @@
use std::io::Bytes;
#[allow(dead_code)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
@ -14,15 +16,43 @@ pub extern "C" fn my_add(x: i32, y: i32) -> i32 {
x + y
}
#[allow(unused)]
#[no_mangle]
pub extern "C" fn my_bool(x: bool, y: bool, z: bool, xr: *mut bool, yr: *mut bool, zr: *mut bool) -> bool {
pub extern "C" fn my_bool(
x: bool,
y: bool,
z: bool,
xr: *mut bool,
yr: *mut bool,
zr: *mut bool,
) -> bool {
unsafe {
*xr = x;
*yr = y;
*zr = z;
true
}
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 unsafe_return_string() -> *const u8 {
todo!();
}
#[no_mangle]
pub unsafe extern "C" fn unsafe_return_string2() -> *const u8 {
todo!();
}
#[no_mangle]
pub extern "C" fn unsafe_destroy_string(s: *mut String) {
unsafe { Box::from_raw(s) };
}
#[test]

File diff suppressed because it is too large Load Diff

View File

@ -39,22 +39,20 @@ pub fn emit_rust_method(list: &Vec<ExternMethod>, options: &BindgenOptions) -> S
let parameter_only_names = item
.parameters
.iter()
.map(|p| format!(" {}", p.name))
.map(|p| format!(" {}", p.name))
.collect::<Vec<_>>()
.join(",\n");
let template = format!(
"
#[no_mangle]
pub extern \"C\" fn {method_prefix}{method_name}(
pub unsafe extern \"C\" fn {method_prefix}{method_name}(
{parameters}
){return_line}
{{
unsafe {{
return {method_type_path2}{method_name}(
{method_type_path2}{method_name}(
{parameter_only_names}
)
}}
)
}}
"
);

View File

@ -111,6 +111,7 @@ impl RustType {
fn convert_type_name(type_name: &str, options: &BindgenOptions) -> String {
let name = match type_name {
// std::os::raw https://doc.rust-lang.org/std/os/raw/index.html
// std::ffi::raw https://doc.rust-lang.org/core/ffi/index.html
"c_char" => "byte",
"c_schar" => "sbyte",
"c_uchar" => "byte",
@ -125,6 +126,7 @@ impl RustType {
"c_float" => "float",
"c_double" => "double",
"c_void" => "void",
"CString" => "sbyte",
// rust primitives
"i8" => "sbyte",
"i16" => "short",

View File

@ -1,14 +1,28 @@
// See https://aka.ms/new-console-template for more information

// See https://aka.ms/new-console-template for more information
//using Csbindgen;
using CsBindgen;
using System.Buffers.Text;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
unsafe
{
var a = false;
var b = false;
var c = false;
Console.WriteLine(Encoding.Default);
var p = LibRust.unsafe_return_string();
var s = Encoding.UTF8.GetString(p, 5);
Console.WriteLine(s);
@ -33,4 +47,12 @@ public static unsafe partial class LibraryImportNativeMethods
public static partial bool my_bool([MarshalAs(UnmanagedType.U1)] bool x, [MarshalAs(UnmanagedType.U1)] bool y, [MarshalAs(UnmanagedType.U1)] bool z, bool* xr, bool* yr, bool* zr);
[LibraryImport(__DllName)]
public static partial void foo(Foo f);
}
public struct Foo
{
[MarshalAs(UnmanagedType.U1)] public bool A;
}

3
dotnet-sandbox/asm.cs vendored Normal file
View File

@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly:DisableRuntimeMarshalling()]

View File

@ -18,6 +18,15 @@ namespace CsBindgen
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool my_bool([MarshalAs(UnmanagedType.U1)] bool x, [MarshalAs(UnmanagedType.U1)] bool y, [MarshalAs(UnmanagedType.U1)] bool z, bool* xr, bool* yr, bool* zr);
[DllImport(__DllName, EntryPoint = "unsafe_return_string", CallingConvention = CallingConvention.Cdecl)]
public static extern byte* unsafe_return_string();
[DllImport(__DllName, EntryPoint = "unsafe_return_string2", CallingConvention = CallingConvention.Cdecl)]
public static extern byte* unsafe_return_string2();
[DllImport(__DllName, EntryPoint = "unsafe_destroy_string", CallingConvention = CallingConvention.Cdecl)]
public static extern void unsafe_destroy_string(String* s);
}