I'm trying to make a Rust dylib and use it from other languages, like C, Python and others. I've successfully called a Rust function taking an i32 argument from Python. Now I'm trying to make a function that takes an array (or a pointer to it, or whatever is necessary to pass a dataset to Rust).
#![crate_type = "dylib"]
#[no_mangle]
pub extern "C" fn rust_multiply(size: i32, arrayPointer: &i32) -> i32 {
*(arrayPointer)
}
This works as expected. But
#![crate_type = "dylib"]
#[no_mangle]
pub extern "C" fn rust_multiply(size: i32, arrayPointer: &i32) -> i32 {
*(arrayPointer + 1) // trying to get next element
}
fails with
error[E0614]: type `i32` cannot be dereferenced
--> src/lib.rs:4:5
|
4 | *(arrayPointer + 1) // trying to get next element
| ^^^^^^^^^^^^^^^^^^^
Doing this:
pub extern fn rust_multiply(size: i32, array: &[i32]) -> i32
and doing something like array[0] fails with "length = 0" error.
externfunctions that accepti32or references (such as&i32). It's better to use the C types that are guaranteed to match your platform -libc::uint32_tas shown in the answer. Also, Rust references are guaranteed to be non-NULL, but there's nothing enforcing that when you cal it via FFI. It'd be safer to accept a*const libc::uint32_t(again, as shown in the answer) and then assert it is non-NULL before making it into a reference.