It's not obvious how to copy an instance of the Fiddle struct. I found an ugly solution using the Fiddle::Pointer
like this.
require 'fiddle/import'
X = Fiddle::Importer.struct [
'int a',
'int b',
]
x = X.malloc
x.a = 1
x.b = 2
x2 = X.malloc
Fiddle::Pointer.new(x2.to_i)[0, X.size] = x.to_ptr.to_s
x2.b = 3
p [x.b, x2.b]
Is there a better way of doing this? One other way I thought of was using the []=
operator of the super class(Fiddle::Pointer
) of the x2.to_ptr
, but it was worse than the above solution.
The class returned by
Fiddle::Importer.struct
implements[]
and[]=
to retrieve and set the struct members by name:However, when called with 2 arguments, these methods behave like their
Fiddle::Pointer
counterparts and get / set the raw binary data:To copy
x
's entire data tox2
: