support NonZero

This commit is contained in:
neuecc 2023-09-05 16:42:44 +09:00
parent 510750c6b6
commit 6e27a5ba08
4 changed files with 70 additions and 48 deletions

View File

@ -31,7 +31,7 @@ Install on `Cargo.toml` as `build-dependencies` and set up `bindgen::Builder` on
crate-type = ["cdylib"]
[build-dependencies]
csbindgen = "1.7.4"
csbindgen = "1.7.5"
```
### Rust to C#.
@ -423,21 +423,6 @@ Rust types will map these C# types.
| Rust | C# |
| ---- | -- |
| `c_char` | `byte` |
| `c_schar` | `sbyte` |
| `c_uchar` | `byte` |
| `c_short` | `short` |
| `c_ushort` | `ushort` |
| `c_int` | `int` |
| `c_uint` | `uint` |
| `c_long` | `CLong` |
| `c_ulong` | `CULong` |
| `c_longlong` | `long` |
| `c_ulonglong` | `ulong` |
| `c_float` | `float` |
| `c_double` | `double` |
| `c_void` | `void` |
| `CString` | `sbyte` |
| `i8` | `sbyte` |
| `i16` | `short` |
| `i32` | `int` |
@ -455,6 +440,33 @@ Rust types will map these C# types.
| `bool` | `[MarshalAs(UnmanagedType.U1)]bool` |
| `char` | `uint` |
| `()` | `void` |
| `c_char` | `byte` |
| `c_schar` | `sbyte` |
| `c_uchar` | `byte` |
| `c_short` | `short` |
| `c_ushort` | `ushort` |
| `c_int` | `int` |
| `c_uint` | `uint` |
| `c_long` | `CLong` |
| `c_ulong` | `CULong` |
| `c_longlong` | `long` |
| `c_ulonglong` | `ulong` |
| `c_float` | `float` |
| `c_double` | `double` |
| `c_void` | `void` |
| `CString` | `sbyte` |
| `NonZeroI8` | `sbyte` |
| `NonZeroI16` | `short` |
| `NonZeroI32` | `int` |
| `NonZeroI64` | `long` |
| `NonZeroI128` | `Int128` |
| `NonZeroIsize` | `nint` |
| `NonZeroU8` | `byte` |
| `NonZeroU16` | `ushort` |
| `NonZeroU32` | `uint` |
| `NonZeroU64` | `ulong` |
| `NonZeroU128` | `UInt128` |
| `NonZeroUsize` | `nuint` |
| `#[repr(C)]Struct` | `[StructLayout(LayoutKind.Sequential)]Struct` |
| `#[repr(C)]Union` | `[StructLayout(LayoutKind.Explicit)]Struct` |
| `#[repr(u*/i*)]Enum` | `Enum` |
@ -467,6 +479,8 @@ Rust types will map these C# types.
| `*const *const T` | `T**` |
| `*mut *const T` | `T**` |
| `*const *mut T` | `T**` |
| `NonNull<T>` | `T*` |
| `Box<T>` | `T*` |
csbindgen is designed to return primitives that do not cause marshalling. It is better to convert from pointers to Span yourself than to do the conversion implicitly and in a black box. This is a recent trend, such as the addition of [DisableRuntimeMarshalling](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.disableruntimemarshallingattribute) from .NET 7.

View File

@ -1,6 +1,8 @@
use std::{
collections::HashSet,
ffi::{c_char, c_long, c_ulong, CString}, ptr::NonNull, num::NonZeroI8,
ffi::{c_char, c_long, c_ulong, CString},
num::*,
ptr::NonNull,
};
#[allow(dead_code)]
@ -171,33 +173,26 @@ pub extern "C" fn event(event: event) {
}
#[no_mangle]
pub extern "C" fn test_func_issue_39(_f: extern "C" fn(i32)){
}
pub extern "C" fn test_func_issue_39(_f: extern "C" fn(i32)) {}
#[no_mangle]
pub extern "C" fn test_func_issue_39_variation1(_f: extern "C" fn(i32, i32, i32)){
}
pub extern "C" fn test_func_issue_39_variation1(_f: extern "C" fn(i32, i32, i32)) {}
#[no_mangle]
pub extern "C" fn nonnull_parameter(_output_word_uuid: NonNull<[u8; 16]>){
}
pub extern "C" fn nonnull_parameter(_output_word_uuid: NonNull<[u8; 16]>) {}
// #[no_mangle]
// pub extern "C" fn non_nonnull_parameter(_output_word_uuid: [u8; 16]){
// }
#[no_mangle]
pub extern "C" fn non_nonnull_parameter2(_output_word_uuid: NonNull<u8>){
}
pub extern "C" fn non_nonnull_parameter2(_output_word_uuid: NonNull<u8>) {}
#[no_mangle]
pub extern "C" fn nonzero_test(_a: NonZeroI8){
}
pub extern "C" fn nonzero_test(_a: NonZeroI8, _b: NonZeroU8, _c: NonZeroU32) {}
#[no_mangle]
pub extern "C" fn ge(_f: extern "C" fn(i32, i32, i32)){
}
pub extern "C" fn ge(_f: extern "C" fn(i32, i32, i32)) {}
#[no_mangle]
pub extern "C" fn nest_test(

View File

@ -217,23 +217,6 @@ impl RustType {
fn convert_type_name(type_name: &str) -> String {
let temp_string: 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",
"c_short" => "short",
"c_ushort" => "ushort",
"c_int" => "int",
"c_uint" => "uint",
"c_long" => "CLong", // .NET 6
"c_ulong" => "CULong", // .NET 6
"c_longlong" => "long",
"c_ulonglong" => "ulong",
"c_float" => "float",
"c_double" => "double",
"c_void" => "void",
"CString" => "sbyte",
// rust primitives
"i8" => "sbyte",
"i16" => "short",
@ -252,6 +235,36 @@ impl RustType {
"char" => "uint",
"usize" => "nuint", // C# 9.0
"()" => "void",
// 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",
"c_short" => "short",
"c_ushort" => "ushort",
"c_int" => "int",
"c_uint" => "uint",
"c_long" => "CLong", // .NET 6
"c_ulong" => "CULong", // .NET 6
"c_longlong" => "long",
"c_ulonglong" => "ulong",
"c_float" => "float",
"c_double" => "double",
"c_void" => "void",
"CString" => "sbyte",
// std::num https://doc.rust-lang.org/std/num/index.html
"NonZeroI8" => "sbyte",
"NonZeroI16" => "short",
"NonZeroI32" => "int",
"NonZeroI64" => "long",
"NonZeroI128" => "Int128",
"NonZeroIsize" => "nint",
"NonZeroU8" => "byte",
"NonZeroU16" => "ushort",
"NonZeroU32" => "uint",
"NonZeroU64" => "ulong",
"NonZeroU128" => "UInt128",
"NonZeroUsize" => "nuint",
_ => {
temp_string = escape_name(type_name);
temp_string.as_str()

View File

@ -59,7 +59,7 @@ namespace CsBindgen
public static extern void non_nonnull_parameter2(byte* _output_word_uuid);
[DllImport(__DllName, EntryPoint = "nonzero_test", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void nonzero_test(NonZeroI8 _a, U32Transparent _b);
public static extern void nonzero_test(sbyte _a, byte _b, uint _c);
[DllImport(__DllName, EntryPoint = "ge", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void ge(delegate* unmanaged[Cdecl]<int, int, int, void> _f);