Sha256: cd4bd559880ba4bdec63d1e81fe7290ad7ac58fa3dac576569d2b1396c3bf1f3

Contents?: true

Size: 935 Bytes

Versions: 6

Compression:

Stored size: 935 Bytes

Contents

# frozen_string_literal: true

require 'ffi'

module FFI
  # Module to extend struct classes that are present in callbacks as fixed length arrays
  module StructArray
    # Generate a one way converter for a fixed size array of struct
    # @return [DataConverter]
    def array(size)
      ArrayConverter.new(self, size)
    end

    # @!visibility private
    # Helper to handle callbacks containing fixed length array of struct
    class ArrayConverter
      include DataConverter

      def initialize(type, size)
        @type = type
        @size = size
      end

      def native_type(_type = nil)
        FFI::Type::POINTER
      end

      def from_native(ptr, _ctx)
        return [] if ptr.null?

        Array.new(@size) { |i| @type.new(ptr + (i * @type.size)) }
      end

      def to_native(ary, _ctx)
        raise NotImplementedError, "#{self.class.name} Cannot convert #{ary} to pointer"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ffi-libfuse-0.3.4 lib/ffi/struct_array.rb
ffi-libfuse-0.3.3 lib/ffi/struct_array.rb
ffi-libfuse-0.1.0.rc20220550 lib/ffi/struct_array.rb
ffi-libfuse-0.0.1.rctest12 lib/ffi/struct_array.rb
ffi-libfuse-0.0.1.rctest11 lib/ffi/struct_array.rb
ffi-libfuse-0.0.1.pre lib/ffi/struct_array.rb