I would like to use Fiddle to access a native library compiled from Rust code. The C representation of the struct is very simple, it is just a pointer and a length:
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);
However, all examples I can find accept or return pointers to structs, and not the structs themselves. I'd like to avoid the double indirection if at all possible.
I've borrowed an example from the Fiddle::Importer
documentation. However, I do not see how to properly call the extern
method with a structure instead of a pointer to a structure:
require 'fiddle'
require 'fiddle/import'
module LibSum
extend Fiddle::Importer
dlload './libsum.so'
extern 'double sum(double*, int)'
extern 'double split(double)'
end
Note
Fiddle is not the same as the FFI gem. Fiddle is a component of the Ruby standard library, and is not provided as a separate gem. These related questions refer to the FFI gem, and not to Fiddle: