我正在尝试将一个字符串从Ruby传递给Rust可执行文件,对其进行操作并将操作后的字符串传回。
到目前为止,我已经成功地传递了字符串并将其返回,但是我不知道如何将其转换为Rust字符串、操作它,然后将其传回Ruby。以下是我的代码:
// lib.rs
use std::ffi::CStr;
#[no_mangle]
pub extern fn return_string(test_str: &CStr) -> &CStr {
// working funciton
test_str
}
#[no_mangle]
pub extern fn manipulate_and_return_string(mystr: &CStr) -> &CStr {
// mystr type == &std::ffi::c_str::CStr
// println!("{:?}", mystr); => std::ffi::c_str::CStr` cannot be formatted using `:?`
let cstr = mystr.to_bytes_with_nul();
// println!("{:?}", mystr); => []
// cstr type == &[u8]
let ptr = cstr.as_ptr();
// ptr type == *const u8
// println!("{:?}", mystr); => 0x7fd898edb520
let str_slice: &str = std::str::from_utf8(cstr).unwrap();
// str type == &str
// println!("{:?}", mystr); => ""
let str_buf: String = str_slice.to_owned();
// str_bug == collections::string::String
// println!("{:?}", mystr); => ""
}
# rust.rb
require 'ffi'
module Rust
extend FFI::Library
ffi_lib './bin/libembed.dylib'
attach_function :return_string, [:string], :string
attach_function :manipulate_and_return_string, [:string], :string
end
CStr
不是一个FFI安全类型,不能像这样使用。您应该使用*const libc::c_char
。其次,答案完全取决于谁拥有生成的字符串。Ruby是否管理它?如果是这样,它是如何管理的?它需要在哪个堆上分配?Rust是否仍然拥有它?如果是这样,如何提供清理函数?我能建议的就是找出如何在C中做到这一点,然后用Rust以同样的方式实现。 - DK.