使用Ruby FFI调用Rust lib时出现分段错误
发布时间:2020-12-17 04:19:41 所属栏目:百科 来源:网络整理
导读:我想将一个String传递给Rust lib,但它总是抛出一个分段错误. 这是代码: // lib.rs #[no_mangle] pub extern fn process(foo: String) - String { foo } 和Ruby文件: # embed.rb require 'ffi' module Hello extend FFI::Library ffi_lib 'target/release/l
我想将一个String传递给Rust lib,但它总是抛出一个分段错误.
这是代码: // lib.rs #[no_mangle] pub extern fn process(foo: String) -> String { foo } 和Ruby文件: # embed.rb require 'ffi' module Hello extend FFI::Library ffi_lib 'target/release/libembed.dylib' attach_function :process,[ :string ],:string end puts Hello.process("foo") 解决方法
免责声明:我之前从未使用过Ruby-FFI;我正在继续我在文档中找到的内容.
根据Ruby-FFI wiki page on types,:string相当于NUL终止的C字符串.这与Rust String不同. Rust中的字符串(目前)大三倍! Rust中相应的类型是* const :: libc :: c_char.值得注意的是,还有 总之,要处理Rust中的C字符串,您将不得不处理类型.还要记住,根据您实际尝试的操作,您可能还需要使用libc :: malloc和libc :: free手动管理内存. This answer to “Rust FFI C string handling”提供了有关如何处理Rust中C字符串的更多详细信息.虽然问题的上下文是与C代码集成,但在您的情况下它应该同样有用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |