简体   繁体   English

type OnProduce = extern "C" fn 不是 FFI 安全的

[英]type OnProduce = extern "C" fn is not FFI safe

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);
}

I get:我得到:

   |
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

but it's not possible to add #[repr(C)] for a type, only structs.但不能为类型添加#[repr(C)] ,只能为结构添加。 As you can see, OnProduce is an extern "C" function.如您所见, OnProduce是一个extern "C"函数。 I thought it'd be already FFI-safe我以为它已经是 FFI 安全的

As @Frxstream pointed out, it's probably because ZLMedia isn't FFI-safe.正如@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.问题在于,在 Rust 中,当一个结构体是 repr(Rust)(默认)时,它可以自由地使用它认为对您的程序最有效的任何布局。 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.因此,这可能(假设)在两个 Rust 程序之间有所不同,如果这两个程序是使用不同版本的 Rust 编译器编译的,则更有可能不同。

So for FFI you need an explicitly specified layout.因此,对于 FFI,您需要明确指定的布局。 Currently C and transparent are the only stable layouts for structs.目前 C 和透明是唯一稳定的结构布局。 There may be more more in the future but that's currently a ways off.未来可能会有更多,但目前还有很长的路要走。

I recently had a discussion about this on the forums, which you might be interested to read (origin of the quote): [link]我最近在论坛上对此进行了讨论,您可能有兴趣阅读(引用的来源): [链接]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rust中extern fn和extern“C”fn有什么区别? - What's the difference between extern fn and extern “C” fn in Rust? 如何使闭包键入“ extern“ C” fn” - How to make a closure typed 'extern “C” fn' 如何提供外部“C”FFI 绑定的替代定义? - How can I provide an alternate definition of an extern "C" FFI binding? 类型不匹配:`预期的fn @(&amp;&amp; @ type)-&gt; uint`,但发现了`extern fn(@map_a)-&gt; uint`(预期的参数模式++,但发现&amp;&amp;) - Mismatched types: `expected fn@(&&@type) -> uint` but found `extern fn(@map_a) -> uint` (expected argument mode ++ but found &&) 什么是transmute::&lt;*mut c_void, Option<unsafe extern "C" fn() -> ()&gt;&gt;是什么意思? - What does transmute::<*mut c_void, Option<unsafe extern "C" fn() -> ()>> mean? 在全局静态变量中存储对外部 C 函数的静态函数引用:不匹配的类型:预期的 fn 指针,找到了 fn 项 - Store static function reference to extern C function in global static var: mismatched types: expected fn pointer, found fn item 如何在发布外部“ C” fn中返回动态长度的向量? - How do I return an vector of dynamic length in a pub extern “C” fn? Rust不在lib extern FFI中创建函数 - Rust not creating function in lib extern FFI Dart FFI 类型映射 - Dart FFI type mappings FFI 原始类型大小 - FFI Primitive Type Size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM