是否可以调用从C获取Vec的Rust函数? [英] Is it possible to call a Rust function taking a Vec from C?
问题描述
假设我有以下Rust库:
// lib.rs
#![crate_type = staticlib]
#[no_mangle]
pub extern fn do_something(number: i32) {
// something
}
#[no_mangle]
pub extern fn do_something_else(collection: &Vec<i32>) {
// something
}
我知道,要从C调用do_something
,我只需要声明一个带有int32_t
的extern
函数,但是可以调用do_something_else
吗?如果可以,怎么办?
您可以,但是更好的问题是应该您吗?
由于无法从C构造Vec
,因此必须在Rust中构造它,然后返回指向C的指针.C代码将拥有指向Vec
的指针,然后在调用时将其传递回do_something_else
.
然后就是一个问题,除了创建反映所有Rust方法的新FFI方法之外,您也无法在C中真正修改Vec
.
您也可能不应该使用&Vec<i32>
,因为保证不能使NULL为Rust引用,并且在从C调用时没有任何强制要求.*const Vec<i32>
,断言它为非NULL并将其转换为引用.
您可能希望通过FFI边界接受C数组. C数组是一个指针,并且是一个长度,因此您将接受它们并重新构建一个Rust slice (因为您将不拥有该数组):
use std::slice;
pub extern fn do_something_else(p: *const i32, len: libc::size_t) {
let slice = unsafe {
assert!(!p.is_null());
slice::from_raw_parts(p, len)
};
}
到 Rust FFI Omnibus 的必填链接.. >
如果您真的需要按照您的要求做,它可能看起来像这样:
extern crate libc;
#[no_mangle]
pub extern fn make_vec() -> *mut Vec<i32> {
Box::into_raw(Box::new(Vec::new()))
}
#[no_mangle]
pub extern fn add_number(vec: *mut Vec<i32>, val: libc::int32_t) {
let vec = unsafe {
assert!(!vec.is_null());
&mut *vec
};
vec.push(val);
}
#[no_mangle]
pub extern fn print_vec(vec: *const Vec<i32>) {
let vec = unsafe {
assert!(!vec.is_null());
&*vec
};
println!("{:?}", vec);
}
#[no_mangle]
pub extern fn drop_vec(vec: *mut Vec<i32>) {
unsafe {
assert!(!vec.is_null());
Box::from_raw(vec);
}
}
它的使用方式(未经测试):
// Add extern declarations
int main(int argc, char *argv[]) {
void *v = make_vec(); // Use a real typedef here
add_number(v, 42);
print_vec(v);
drop_vec(v);
}
您想在valgrind下运行此命令,以确保我没有在内存方面做任何愚蠢的事情.
Suppose I have the following Rust library:
// lib.rs
#![crate_type = staticlib]
#[no_mangle]
pub extern fn do_something(number: i32) {
// something
}
#[no_mangle]
pub extern fn do_something_else(collection: &Vec<i32>) {
// something
}
I know that, to call do_something
from C, I'd just need to declare an extern
function taking an int32_t
, but is it possible to call do_something_else
? If so, how?
You can, but the better question is should you?
Since you cannot construct a Vec
from C, you'd have to construct it in Rust and then return a pointer to C. C code would own the pointer to the Vec
and would then pass it back when calling do_something_else
.
Then there's the problem that you can't really modify the Vec
in C either, other than by creating new FFI methods that mirror all of the Rust methods.
You also probably shouldn't take a &Vec<i32>
because Rust references are guaranteed to not be NULL, and there's nothing that enforces that when called from C. It's better to take a *const Vec<i32>
, assert that it's non-NULL and convert it to a reference.
Chances are that you want to accept a C array through the FFI boundary. C arrays are a pointer and a length, so you'd accept both and reconstitute a Rust slice (since you wouldn't own the array):
use std::slice;
pub extern fn do_something_else(p: *const i32, len: libc::size_t) {
let slice = unsafe {
assert!(!p.is_null());
slice::from_raw_parts(p, len)
};
}
Obligatory link to The Rust FFI Omnibus.
If you really needed to do what you asked, it would probably look something like this:
extern crate libc;
#[no_mangle]
pub extern fn make_vec() -> *mut Vec<i32> {
Box::into_raw(Box::new(Vec::new()))
}
#[no_mangle]
pub extern fn add_number(vec: *mut Vec<i32>, val: libc::int32_t) {
let vec = unsafe {
assert!(!vec.is_null());
&mut *vec
};
vec.push(val);
}
#[no_mangle]
pub extern fn print_vec(vec: *const Vec<i32>) {
let vec = unsafe {
assert!(!vec.is_null());
&*vec
};
println!("{:?}", vec);
}
#[no_mangle]
pub extern fn drop_vec(vec: *mut Vec<i32>) {
unsafe {
assert!(!vec.is_null());
Box::from_raw(vec);
}
}
And would be used like (untested):
// Add extern declarations
int main(int argc, char *argv[]) {
void *v = make_vec(); // Use a real typedef here
add_number(v, 42);
print_vec(v);
drop_vec(v);
}
You'd want to run this under valgrind to make sure I didn't do anything stupid memory-wise.
这篇关于是否可以调用从C获取Vec的Rust函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!