generate unsafe extern for Rust to Rust

This commit is contained in:
neuecc 2023-03-08 16:48:11 +09:00
parent 5f1207ae20
commit 6f15957032
8 changed files with 91 additions and 48 deletions

View File

@ -31,14 +31,14 @@ fn main() -> Result<(), Box<dyn Error>> {
// .generate()? // .generate()?
// .write_to_file("src/quiche.rs")?; // .write_to_file("src/quiche.rs")?;
bindgen::Builder::default() // bindgen::Builder::default()
.header("c/bullet3/PhysicsClientC_API.h") // .header("c/bullet3/PhysicsClientC_API.h")
.header("c/bullet3/PhysicsClientSharedMemory_C_API.h") // .header("c/bullet3/PhysicsClientSharedMemory_C_API.h")
.header("c/bullet3/PhysicsClientSharedMemory2_C_API.h") // .header("c/bullet3/PhysicsClientSharedMemory2_C_API.h")
.header("c/bullet3/PhysicsDirectC_API.h") // .header("c/bullet3/PhysicsDirectC_API.h")
.header("c/bullet3/SharedMemoryPublic.h") // .header("c/bullet3/SharedMemoryPublic.h")
.generate()? // .generate()?
.write_to_file("src/bullet3.rs")?; // .write_to_file("src/bullet3.rs")?;
csbindgen::Builder::default() csbindgen::Builder::default()
.input_bindgen_file("src/lz4.rs") .input_bindgen_file("src/lz4.rs")
@ -71,6 +71,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// .input_bindgen_file("src/zstd.rs") // .input_bindgen_file("src/zstd.rs")
// .method_filter(|x| x.starts_with("ZSTD_")) // .method_filter(|x| x.starts_with("ZSTD_"))
// .rust_method_prefix("csbindgen_zstd_") // .rust_method_prefix("csbindgen_zstd_")
// .rust_file_header("use super::zstd::*;")
// .csharp_class_name("LibZstd") // .csharp_class_name("LibZstd")
// .csharp_dll_name("libzsd") // .csharp_dll_name("libzsd")
// .generate_to_file("src/zstd_ffi.rs", "../dotnet-sandbox/zstd_bindgen.cs")?; // .generate_to_file("src/zstd_ffi.rs", "../dotnet-sandbox/zstd_bindgen.cs")?;
@ -78,19 +79,21 @@ fn main() -> Result<(), Box<dyn Error>> {
// csbindgen::Builder::new() // csbindgen::Builder::new()
// .input_bindgen_file("src/quiche.rs") // .input_bindgen_file("src/quiche.rs")
// .method_filter(|x| x.starts_with("quiche_")) // .method_filter(|x| x.starts_with("quiche_"))
// .rust_file_header("use super::quiche::*;")
// .rust_method_prefix("csbindgen_quiche_") // .rust_method_prefix("csbindgen_quiche_")
// .csharp_class_name("LibQuiche") // .csharp_class_name("LibQuiche")
// .csharp_dll_name("libquiche") // .csharp_dll_name("libquiche")
// .generate_to_file("src/quiche_ffi.rs", "../dotnet-sandbox/quiche_bindgen.cs")?; // .generate_to_file("src/quiche_ffi.rs", "../dotnet-sandbox/quiche_bindgen.cs")?;
csbindgen::Builder::new() // csbindgen::Builder::new()
.input_bindgen_file("src/bullet3.rs") // .input_bindgen_file("src/bullet3.rs")
.method_filter(|x| x.starts_with("b3")) // .method_filter(|x| x.starts_with("b3"))
.rust_method_prefix("csbindgen_bullet3_") // .rust_file_header("use super::bullet3::*;")
.csharp_entry_point_prefix("csbindgen_bullet3_") // .rust_method_prefix("csbindgen_bullet3_")
.csharp_class_name("LibBullet3") // .csharp_entry_point_prefix("csbindgen_bullet3_")
.csharp_dll_name("libbullet3") // .csharp_class_name("LibBullet3")
.generate_to_file("src/bullet3_ffi.rs", "../dotnet-sandbox/bullet3_bindgen.cs")?; // .csharp_dll_name("libbullet3")
// .generate_to_file("src/bullet3_ffi.rs", "../dotnet-sandbox/bullet3_bindgen.cs")?;
Ok(()) Ok(())
} }

View File

@ -3,7 +3,7 @@
#[allow(unused)] #[allow(unused)]
use ::std::os::raw::*; use ::std::os::raw::*;
use super::bullet3::*;
#[no_mangle] #[no_mangle]
@ -246,10 +246,10 @@ pub unsafe extern "C" fn csbindgen_bullet3_b3GetStatusActualState(
bodyUniqueId: *mut c_int, bodyUniqueId: *mut c_int,
numDegreeOfFreedomQ: *mut c_int, numDegreeOfFreedomQ: *mut c_int,
numDegreeOfFreedomU: *mut c_int, numDegreeOfFreedomU: *mut c_int,
rootLocalInertialFrame: *mut *mut f64, rootLocalInertialFrame: *mut *const f64,
actualStateQ: *mut *mut f64, actualStateQ: *mut *const f64,
actualStateQdot: *mut *mut f64, actualStateQdot: *mut *const f64,
jointReactionForces: *mut *mut f64 jointReactionForces: *mut *const f64
) -> c_int ) -> c_int
{ {
b3GetStatusActualState( b3GetStatusActualState(
@ -271,14 +271,14 @@ pub unsafe extern "C" fn csbindgen_bullet3_b3GetStatusActualState2(
numLinks: *mut c_int, numLinks: *mut c_int,
numDegreeOfFreedomQ: *mut c_int, numDegreeOfFreedomQ: *mut c_int,
numDegreeOfFreedomU: *mut c_int, numDegreeOfFreedomU: *mut c_int,
rootLocalInertialFrame: *mut *mut f64, rootLocalInertialFrame: *mut *const f64,
actualStateQ: *mut *mut f64, actualStateQ: *mut *const f64,
actualStateQdot: *mut *mut f64, actualStateQdot: *mut *const f64,
jointReactionForces: *mut *mut f64, jointReactionForces: *mut *const f64,
linkLocalInertialFrames: *mut *mut f64, linkLocalInertialFrames: *mut *const f64,
jointMotorForces: *mut *mut f64, jointMotorForces: *mut *const f64,
linkStates: *mut *mut f64, linkStates: *mut *const f64,
linkWorldVelocities: *mut *mut f64 linkWorldVelocities: *mut *const f64
) -> c_int ) -> c_int
{ {
b3GetStatusActualState2( b3GetStatusActualState2(
@ -565,7 +565,7 @@ pub unsafe extern "C" fn csbindgen_bullet3_b3GetUserDataInfo(
physClient: b3PhysicsClientHandle, physClient: b3PhysicsClientHandle,
bodyUniqueId: c_int, bodyUniqueId: c_int,
userDataIndex: c_int, userDataIndex: c_int,
keyOut: *mut *mut c_char, keyOut: *mut *const c_char,
userDataIdOut: *mut c_int, userDataIdOut: *mut c_int,
linkIndexOut: *mut c_int, linkIndexOut: *mut c_int,
visualShapeIndexOut: *mut c_int visualShapeIndexOut: *mut c_int

View File

@ -14,6 +14,45 @@ mod lz4;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
mod lz4_ffi; mod lz4_ffi;
// #[allow(dead_code)]
// #[allow(non_snake_case)]
// #[allow(non_camel_case_types)]
// #[allow(non_upper_case_globals)]
// mod bullet3;
// #[allow(dead_code)]
// #[allow(non_snake_case)]
// #[allow(non_camel_case_types)]
// mod bullet3_ffi;
// #[allow(dead_code)]
// #[allow(non_snake_case)]
// #[allow(non_camel_case_types)]
// #[allow(non_upper_case_globals)]
// mod quiche;
// #[allow(dead_code)]
// #[allow(non_snake_case)]
// #[allow(non_camel_case_types)]
// mod quiche_ffi;
// #[allow(dead_code)]
// #[allow(non_snake_case)]
// #[allow(non_camel_case_types)]
// #[allow(non_upper_case_globals)]
// mod zstd;
// #[allow(dead_code)]
// #[allow(non_snake_case)]
// #[allow(non_camel_case_types)]
// mod zstd_ffi;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub type LONG_PTR = ::std::os::raw::c_longlong; pub type LONG_PTR = ::std::os::raw::c_longlong;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]

View File

@ -3,7 +3,7 @@
#[allow(unused)] #[allow(unused)]
use ::std::os::raw::*; use ::std::os::raw::*;
use super::quiche::*;
#[no_mangle] #[no_mangle]
@ -18,7 +18,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_version(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_enable_debug_logging( pub unsafe extern "C" fn csbindgen_quiche_quiche_enable_debug_logging(
cb: Option<extern "C" fn(line: *const c_char, argp: *mut c_void)>, cb: Option<unsafe extern "C" fn(line: *const c_char, argp: *mut c_void)>,
argp: *mut c_void argp: *mut c_void
) -> c_int ) -> c_int
{ {
@ -899,7 +899,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_close(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_trace_id( pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_trace_id(
conn: *const quiche_conn, conn: *const quiche_conn,
out: *mut *mut u8, out: *mut *const u8,
out_len: *mut usize out_len: *mut usize
) )
{ {
@ -913,7 +913,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_trace_id(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_source_id( pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_source_id(
conn: *const quiche_conn, conn: *const quiche_conn,
out: *mut *mut u8, out: *mut *const u8,
out_len: *mut usize out_len: *mut usize
) )
{ {
@ -927,7 +927,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_source_id(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_destination_id( pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_destination_id(
conn: *const quiche_conn, conn: *const quiche_conn,
out: *mut *mut u8, out: *mut *const u8,
out_len: *mut usize out_len: *mut usize
) )
{ {
@ -941,7 +941,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_destination_id(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_application_proto( pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_application_proto(
conn: *const quiche_conn, conn: *const quiche_conn,
out: *mut *mut u8, out: *mut *const u8,
out_len: *mut usize out_len: *mut usize
) )
{ {
@ -955,7 +955,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_application_proto(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_peer_cert( pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_peer_cert(
conn: *const quiche_conn, conn: *const quiche_conn,
out: *mut *mut u8, out: *mut *const u8,
out_len: *mut usize out_len: *mut usize
) )
{ {
@ -969,7 +969,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_peer_cert(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_session( pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_session(
conn: *const quiche_conn, conn: *const quiche_conn,
out: *mut *mut u8, out: *mut *const u8,
out_len: *mut usize out_len: *mut usize
) )
{ {
@ -1065,7 +1065,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_peer_error(
conn: *const quiche_conn, conn: *const quiche_conn,
is_app: *mut bool, is_app: *mut bool,
error_code: *mut u64, error_code: *mut u64,
reason: *mut *mut u8, reason: *mut *const u8,
reason_len: *mut usize reason_len: *mut usize
) -> bool ) -> bool
{ {
@ -1083,7 +1083,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_local_error(
conn: *const quiche_conn, conn: *const quiche_conn,
is_app: *mut bool, is_app: *mut bool,
error_code: *mut u64, error_code: *mut u64,
reason: *mut *mut u8, reason: *mut *const u8,
reason_len: *mut usize reason_len: *mut usize
) -> bool ) -> bool
{ {
@ -1261,7 +1261,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_dgram_send(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_dgram_purge_outgoing( pub unsafe extern "C" fn csbindgen_quiche_quiche_conn_dgram_purge_outgoing(
conn: *mut quiche_conn, conn: *mut quiche_conn,
f: Option<extern "C" fn(arg1: *mut u8, arg2: usize) -> bool> f: Option<unsafe extern "C" fn(arg1: *mut u8, arg2: usize) -> bool>
) )
{ {
quiche_conn_dgram_purge_outgoing( quiche_conn_dgram_purge_outgoing(
@ -1427,7 +1427,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_event_type(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_event_for_each_header( pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_event_for_each_header(
ev: *mut quiche_h3_event, ev: *mut quiche_h3_event,
cb: Option<extern "C" fn(name: *mut u8, name_len: usize, value: *mut u8, value_len: usize, argp: *mut c_void) -> c_int>, cb: Option<unsafe extern "C" fn(name: *mut u8, name_len: usize, value: *mut u8, value_len: usize, argp: *mut c_void) -> c_int>,
argp: *mut c_void argp: *mut c_void
) -> c_int ) -> c_int
{ {
@ -1441,7 +1441,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_event_for_each_header(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_for_each_setting( pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_for_each_setting(
conn: *mut quiche_h3_conn, conn: *mut quiche_h3_conn,
cb: Option<extern "C" fn(identifier: u64, value: u64, argp: *mut c_void) -> c_int>, cb: Option<unsafe extern "C" fn(identifier: u64, value: u64, argp: *mut c_void) -> c_int>,
argp: *mut c_void argp: *mut c_void
) -> c_int ) -> c_int
{ {
@ -1614,7 +1614,7 @@ pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_send_priority_update_for_req
pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_take_last_priority_update( pub unsafe extern "C" fn csbindgen_quiche_quiche_h3_take_last_priority_update(
conn: *mut quiche_h3_conn, conn: *mut quiche_h3_conn,
prioritized_element_id: u64, prioritized_element_id: u64,
cb: Option<extern "C" fn(priority_field_value: *mut u8, priority_field_value_len: u64, argp: *mut c_void) -> c_int>, cb: Option<unsafe extern "C" fn(priority_field_value: *mut u8, priority_field_value_len: u64, argp: *mut c_void) -> c_int>,
argp: *mut c_void argp: *mut c_void
) -> c_int ) -> c_int
{ {

View File

@ -3,7 +3,7 @@
#[allow(unused)] #[allow(unused)]
use ::std::os::raw::*; use ::std::os::raw::*;
use super::zstd::*;
#[no_mangle] #[no_mangle]

View File

@ -349,7 +349,7 @@ fn parse_type(t: &syn::Type) -> RustType {
}; };
return RustType { return RustType {
type_name: "extern \"C\" fn".to_string(), type_name: "unsafe extern \"C\" fn".to_string(),
type_kind: TypeKind::Function(parameters, ret), type_kind: TypeKind::Function(parameters, ret),
}; };
} }

View File

@ -106,7 +106,7 @@ impl RustType {
ConstPointerPointer => sb.push_str("*const *const"), ConstPointerPointer => sb.push_str("*const *const"),
MutPointerPointer => sb.push_str("*mut *mut"), MutPointerPointer => sb.push_str("*mut *mut"),
ConstMutPointerPointer =>sb.push_str("*const *mut"), ConstMutPointerPointer =>sb.push_str("*const *mut"),
MutConstPointerPointer =>sb.push_str("*mut *mut"), MutConstPointerPointer =>sb.push_str("*mut *const"),
}; };
} }

View File

@ -14,10 +14,11 @@ using System.Text;
unsafe unsafe
{ {
LibRust.call_bindgen_lz4();
LibRust.alias_test1(null); LibRust.alias_test1(null);
//LibRust.call_bindgen_lz4();
// C# -> Rust, pass static UnmanagedCallersOnly method with `&` // C# -> Rust, pass static UnmanagedCallersOnly method with `&`