pub type OnProduce = extern "C" fn(*mut ZLMedia, *const u8, size_t);
extern "C" {
pub fn zlmedia_set_on_produce(zl_media: *mut ZLInstance, on_produce: OnProduce);
}
我得到: |
23 | pub fn zlmedia_set_on_produce(zl_media: *mut ZLInstance, on_produce: OnProduce);
| ^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
但无法为类型(仅结构)添加#[repr(C)]
。如您所见,OnProduce
是extern "C"
函数。我以为已经是FFI安全的了
最佳答案
正如@Frxstream指出的,这可能是因为ZLMedia
不是FFI安全的。
Quote:
The trouble is that in Rust when a struct is repr(Rust) (the default) it is then free to use whatever layout it thinks is most efficient for your program. So this could (hypothetically) be different between two Rust programs and is even more likely to be different if the two programs are compiled with different versions of the Rust compiler.
So for FFI you need an explicitly specified layout. Currently C and transparent are the only stable layouts for structs. There may be more more in the future but that's currently a ways off.
我最近在论坛上对此进行了讨论,您可能有兴趣阅读(报价来源):[link]
关于rust - 类型OnProduce = extern “C” fn不安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64743518/