Pass a C array to a Rust function

aspcartman

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.

swizard

You have to make some efforts to provide a pure C API and implement some conversions using unsafe code. Fortunately, it is not so difficult:

extern crate libc;

#[no_mangle]
pub extern "C" fn rust_multiply(
    size: libc::size_t,
    array_pointer: *const libc::uint32_t,
) -> libc::uint32_t {
    internal_rust_multiply(unsafe {
        std::slice::from_raw_parts(array_pointer as *const i32, size as usize)
    }) as libc::uint32_t
}

fn internal_rust_multiply(array: &[i32]) -> i32 {
    assert!(!array.is_empty());
    array[0]
}

There is a good introduction for Rust FFI on the official site.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I pass an array to a function in Rust and change its content?

From Dev

How to pass a function as argument in Rust

From Dev

Pass Python list to Rust function

From Dev

How to pass a HashMap to a function in Rust

From Dev

How to pass an array of char's to a function in C

From Dev

c++ pass array directy to function

From Dev

ctypes: Initialize array of arrays and pass to C function

From Dev

pass array of specific size to a function in C

From Dev

How to pass in function a multidimensional char array in C?

From Dev

How to pass array index into another function in c

From Dev

C - Pass Array of Strings as Function Parameter

From Dev

Pass back an array from a function (C programing)

From Dev

How to pass array of structures to function in c++

From Dev

How to pass in function a multidimensional char array in C?

From Dev

pass array of specific size to a function in C

From Dev

C++ How to pass array by reference to function

From Dev

pass and modify char array[][] in function C

From Dev

How to pass element of bit array into function in C

From Dev

Pass a pointer of an array to a function to use in printf` in C

From Dev

C: pass array to a function and iterate over in recipient function

From Dev

Cython memoryviews: wrapping c function with array parameter to pass numpy array

From Dev

Pass Python list to embedded Rust function

From Java

How do you pass a Rust function as a parameter?

From Dev

How do a pass a database variable to a function in Rust?

From Dev

Fortran pass an array to function

From Dev

Syntax to pass an array to a function

From Dev

Excel - pass an array into a function

From Dev

PHP Array pass to function

From Dev

Pass struct array to function

Related Related

HotTag

Archive