ruby / fiddle

A libffi wrapper for Ruby.
BSD 2-Clause "Simplified" License
163 stars 36 forks source link

Fiddle::CParser should expose module methods #125

Open catlee opened 1 year ago

catlee commented 1 year ago

The methods in Fiddle::CParser such as parse_struct_signature are useful on their own, outside of a class instance.

Would it make sense to be able to call these as module methods, in addition to the existing instance method interface?

i.e.

require 'fiddle/cparser'
Fiddle::CParser.parse_struct_signature(...)
kou commented 1 year ago

Could you share your use case? We may introduce a new more convenient API instead of exporting parse_struct_signature with the current specification.

catlee commented 1 year ago

I was trying to create a representation of a C structure without having to include Fiddle::CParser or some other module into my namespace.

kou commented 1 year ago

OK. Then, does the following code satisfy your use case with the current API?

require "fiddle/import"

cparser_class = Class.new do
  include Fiddle::CParser
end
cparser = cparser_class.new
pp cparser.parse_struct_signature(["int i", "char c"]) # => [[4, 2], ["i", "c"]]