lib/rust_require/ruby_wrapper_generator.rb
# attaches fns via FFI to mod
attaches fns via FFI to mod
attaches fns via FFI to mod
def attach_fns(fn_headers, mod, mod_string)
      mod.extend FFI::Library
      mod.ffi_lib @rust_lib
      fn_headers.each do |fn|
        rust_fn_name = fn['name']
        wrapper_name = "_#{mod_string+fn['name']}_wrapper".to_sym
        input_types = fn['inputs'].map { |t| Rust::Types.find_type(t) }
        ffi_input_types = input_types.map { |t| t.ffi_input_type }
        output_type = Rust::Types.find_type(fn['output'])
        ffi_output_type = output_type.ffi_output_type
        mod.attach_function wrapper_name, ffi_input_types, ffi_output_type
        mod.instance_eval do
          define_method(rust_fn_name) do |*args|
            raise ArgumentError, "wrong number of arguments (#{args.count} for #{input_types.count})" unless args.count == input_types.count
            args.map!.with_index do |arg,i|
              input_types[i].ruby_input_conversion(arg)
            end
            raw_output = send wrapper_name, *args
            output_type.ruby_output_conversion raw_output
          end
        end
      end
    end
def attach_fns ( fn_headers , mod , mod_string ) mod . extend FFI :: Library mod . ffi_lib @rust_lib fn_headers . each do | fn | rust_fn_name = fn [ 'name' ] wrapper_name = "_#{mod_string+fn['name']}_wrapper" . to_sym input_types = fn [ 'inputs' ] . map { | t | Rust :: Types . find_type ( t ) } ffi_input_types = input_types . map { | t | t . ffi_input_type } output_type = Rust :: Types . find_type ( fn [ 'output' ] ) ffi_output_type = output_type . ffi_output_type mod . attach_function wrapper_name , ffi_input_types , ffi_output_type mod . instance_eval do define_method ( rust_fn_name ) do | * args | raise ArgumentError , "wrong number of arguments (#{args.count} for #{input_types.count})" unless args . count == input_types . count args . map! . with_index do | arg , i | input_types [ i ] . ruby_input_conversion ( arg ) end raw_output = send wrapper_name , * args output_type . ruby_output_conversion raw_output end end end end
attaches fns via FFI to mod
attaches fns via FFI to mod
# add ffi and the rust lib to mod # fn mod::fn() => fn _mod_fn_wrapper # attach fn and define ruby wrapper # check input parameter count # check & convert ruby objects before handling them to FFI # call FFI function and convert output
fn_headers
<not_specific>
mod
<not_specific>
mod_string
<not_specific>