我想使用Fiddle來訪問從Rust代碼編譯的本地庫。該結構的C表現是非常簡單的,它只是一個指針和長度:是否可以使用Fiddle將結構傳遞給本機代碼?
typedef struct {
char *data;
size_t len;
} my_thing_t;
// Example function that somehow accepts a struct
void accepts_a_struct(my_thing_t thing);
// Example function that somehow returns a struct
my_thing_t returns_a_struct(void);
然而,所有的例子我能找到接受或返回指針結構,而不是結構本身。如果可能的話,我想避免雙重間接。
我借了一個Fiddle::Importer
documentation的例子。不過,我不明白如何正確地調用extern
方法與結構,而不是一個指針的結構:
require 'fiddle'
require 'fiddle/import'
module LibSum
extend Fiddle::Importer
dlload './libsum.so'
extern 'double sum(double*, int)'
extern 'double split(double)'
end
注意
小提琴是不一樣FFI gem。 Fiddle是Ruby標準庫的一個組件,不作爲單獨的gem提供。這些相關的問題指的是FFI的寶石,而不是擺弄:
- How to wrap function in Ruby FFI method that takes struct as argument?
- How do I specify a struct as the return value of a function in RubyFFI?
有趣的問題,但我懷疑它可能作爲FFI(其中小提琴是一個包裝)似乎不支持直接傳遞結構,請參閱[本文檔]的「函數參數和返回值」部分(http:// fistfvck .sakura.ne.jp/ruby/rurima-2.1.0/library/fiddle.html)(日語,我正在閱讀谷歌的翻譯),如果你期望提高性能,你將不太可能得到任何有意義的東西 - ruby的變量是指向結構與數據('RBasic'和子類),甚至是從那裏到堆中實際數據的另一個指針。 –
此外,函數簽名中支持的類型列表可以從['parse_ctype']中獲取(https://github.com/ruby/ruby/blob/c8b3f1b470e343e7408ab5883f046b1056d94ccc/ext/fiddle/lib/fiddle/cparser.rb#L120 )功能。 –