Open lionelperrin opened 8 years ago
Here follows a possible workaround:
module FFI
class Struct
alias :orig_array_aset :[]=
alias :orig_array_aref :[]
def sub_structs
@sub_structs ||= {}
end
def []=(index, value)
sub_structs[index] = value if value.is_a? Struct
orig_array_aset(index, value)
end
def [](index)
v = orig_array_aref(index)
if v.is_a? Struct
vv = sub_structs[index]
# always return the same instance of the structure
# if the underlying pointer hasn't changed
return vv if vv && vv.to_ptr == v.to_ptr
# update sub_structs if the pointer has changed
sub_structs[index] = v
end
v
end
end
end
This issue is a sibling of #517.
Consider the following C struct
FFI looks like
It appears that the operator [] for FFI::Struct returns a new instance of struct when accessing a substructure.