Even though `Int` and `isize` should be the same in practice, we can't cleanly convert, as the type information isn't quite there. And anyway, strictly speaking per the report, `Int` is only guaranteed to hold 30 bits. Note that this is essentially unchanged even if we specify `usize_is_size_t = true` for `cbindgen`.
41 lines
720 B
Rust
41 lines
720 B
Rust
#![allow(dead_code)]
|
|
|
|
use std::ffi::{CStr, c_char};
|
|
|
|
fn say_hello(name: &str) {
|
|
println!("Hello from Rust, {name}!");
|
|
}
|
|
|
|
#[unsafe(no_mangle)]
|
|
extern "C" fn hello(c: *const c_char) -> () {
|
|
say_hello(unsafe { CStr::from_ptr(c) }.to_str().unwrap())
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Debug)]
|
|
struct T {
|
|
a: bool,
|
|
b: u8,
|
|
}
|
|
#[unsafe(no_mangle)]
|
|
extern "C" fn hello_struct(t: T) -> () {
|
|
say_hello(&format!("{:?}", t))
|
|
}
|
|
|
|
#[repr(C, u8)]
|
|
#[derive(Debug)]
|
|
enum Shape {
|
|
Circle { radius: f64 },
|
|
Rectangle { width: f64, height: f64 },
|
|
}
|
|
|
|
#[unsafe(no_mangle)]
|
|
extern "C" fn hello_shape(s: Shape) -> () {
|
|
say_hello(&format!("{:?}", s))
|
|
}
|
|
|
|
#[unsafe(no_mangle)]
|
|
extern "C" fn add(a: i64, b: i64) -> i64 {
|
|
a + b
|
|
}
|