Subject:
Re: [ruby-ffi] struct questions
From:
Evan Phoenix
Date:
12/18/09 7:10 PM
To:
ruby-ffi@googlegroups.com


On Dec 18, 2009, at 5:00 PM, rogerdpack wrote:

> A few random questions for members of the group...
> 
> I've noticed that inheritance doesn't seem to work...
> 
>  class SimpleStruct < FFI::Struct
>    layout  :value, :double,
>            :name, :string
>  end
> 
> class S2 < SimpleStruct; end
> puts S2.members # fails NoMethodError: undefined method `members' for
> nil:NilClass
> 
> Is there any means for inheritance? Is this a bug?  (as another side-
> effect, the parent marshal's correctly but the child doesn't--is that
> a bug?).

They were never designed to be subclassed, so I don't consider it a bug.

> 
> With char arrays, it seems "difficult" to insert a string
> 
> (rdb:1) ss[:title] = "abc"
> ArgumentError Exception: put not supported for
> FFI::StructLayout::Array
> 
> is there an easier way than ss[:title][0] = 33; ss[:title][1] = 34; //
> one at a time?

If this works, it should not. The return value should not be references the memory of the array itself in the struct, thats not safe.

Because char_array is an explicit type, #put could support it since the size of the char_array should be known by #put.

> 
> If the only reason "put" is not supported is because it is on the todo
> list, I could help with that.
> 
> I would also be happy to hack up a patch that made Struct#inspect more
> visually friendly
> 
> i.e.
> #<SnarlStruct:0x13221c0>
> becomes
> #<SnarlStruct field1: value1, field2: value2...>
> 
> if there's any interest.

I prefer this not happen. This would require all fields to be read, which is hardly cheap. Plus, reading a field can cause a segfault, so I'd prefer all field reads are explicit by the user.

> 
> Thoughts?
> 
> In good news, I was able to successfully convert ruby/dl of the Snarl
> gem into ffi and it works [now ffi compatible--yea].
> 
> Thanks!
> -r
>