ruby字符串生rust并再次返回
Ruby string to rust and back again
我正在尝试将 Ruby 中的字符串传递给 rust 可执行文件,对其进行操作并将被操作的字符串传回。
到目前为止,我可以将字符串传入并返回,但我不知道如何将其转换为 rust 字符串,对其进行操作,然后将其传递回 ruby??。这是我到目前为止所拥有的:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// lib.rs
use std::ffi::CStr; #[no_mangle] #[no_mangle] |
|
1
2 3 4 5 6 7 8 9 10 |
# rust.rb
require 'ffi' module Rust attach_function :return_string, [:string], :string |
相关讨论
- 快速评论:这有两个直接的问题。首先,CStr 不是 FFI 安全类型,不应该这样使用。您应该使用 *const libc::c_char。其次,答案完全取决于谁拥有结果字符串。 Ruby 管理它吗?如果是这样,怎么做?它需要分配在哪个堆上? Rust 还拥有它吗?如果是这样,您如何提供清理功能?我所能推荐的就是找出如何在 C 中做到这一点,然后在 Rust 中这样做。
- 另请查看 stackoverflow.com/q/30510764/155423,因为它涵盖了所有重要部分。在这一点上使其适应 Ruby 很容易。
- clalance.blogspot.ca/2011/01/... 会有帮助,github.com/ruby/ruby/blob/trunk/doc/extension.rdoc 也会有帮助
感谢 Steve Klabnik、shepmaster 和 DK 的一些指导,我想出了如何在 Rust 中编写外部字符串 concat 函数并在 Ruby 中使用它。
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// lib.rs
#![feature(libc)] #![feature(cstr_to_str)] #![feature(cstr_memory)] extern crate libc; use std::ffi::{CStr,CString}; #[no_mangle] let mut s1_string = s1_and_str.to_string(); // collections::string::String s1_string.push_str(s2_and_str); let concated_string = CString::new(s1_string).unwrap(); // std::ffi::c_str::CString concated_string.into_ptr() // const i8 |
|
1
2 3 4 5 6 7 8 9 |
# rust.rb
require 'ffi' module Rust attach_function :concat, [:string, :string], :string |
|
1
2 3 |
#calling the function in Ruby
Rust.concat('This is a ', 'cohesive sentence') # => 'This is a cohesive sentence' |