jacius / nice-ffi

Nice-FFI: Convenience layer atop Ruby-FFI
http://jacius.github.com/nice-ffi/
23 stars 9 forks source link

NiceFFI::Struct constructor does not allow zero arguments #3

Closed tmilker closed 14 years ago

tmilker commented 14 years ago

I am trying to switch my wrapper over to NiceFFI, from FFI, but when I change my FFI::Struct inheritances to NiceFFI::Struct, ruby starts throwing wrong number of arguments errors in my test app because I am trying to new stuff up without a ptr(valid with FFI).

Am I doing something wrong?

tmilker commented 14 years ago

Another thing is that NiceFFI does not allow partial initialization like FFI(well, I guess FFI does not have this initialization at all). For example:

     class Color_t < NiceFFI::Struct
        layout :r, :uint8,
               :g, :uint8,
               :b, :uint8,
      end

And then you try:

    Color_t.new({ :r => 127 })

will throw an error:

    gems/nice-ffi-0.4/lib/nice-ffi/struct.rb:372:in `[]=': no implicit conversion from nil to integer (TypeError)
jacius commented 14 years ago

You're not doing anything wrong, I just hadn't thought about creating a new struct without initializing all the values. Would it meet your needs for the struct to be created with a new, zero-filled Buffer if no arguments are provided?

tmilker commented 14 years ago

Yeah sounds like what Ruby-FFI does. I am still pretty new to this so you may want to check that source.

jacius commented 14 years ago

Okay, I've implemented the new behaviors in my next branch (9110be614b4043ccf2f5908b0a434b31179b0fde). You can now create a struct with no arguments (or nil), or a hash with some members missing, or an Array that is too short or has nil for some members. Thanks for the suggestion, tmilker!