I have ruby code which passes a struct to a native Rust library, via FFI gem.
A struct contains a string field. I need to be able to specify string on Ruby side.
class MyStruct < FFI::Struct
layout :s1, :string,
:field2, :uint32,
# other fields
I've tried this:
def initialize(ruby_str)
self[:s1] = ruby_str
# [.......]
And have gotten this:
`[]=': Cannot set :string fields (ArgumentError)
And this blows segmentation fault:
def initialize(ruby_str)
p1 = FFI::MemoryPointer.from_string(ruby_str)
self[:content] = p1
self[:len] = ruby_str.length
I've read about this issue, but I've not found a clear solution, nor example. I need to be able to pass a string field at least from Ruby (one way, that is).
And, additionally, also convert a string received from a native Rust library (not NULL terminated, but with a length specified in a separate field) into a Ruby string.
How can it be done then?