Sha256: 5b8ac671f02b22f243e8b5ad54d2469c34bfaf587c62e7a4330dd7ce9a467816

Contents?: true

Size: 983 Bytes

Versions: 6

Compression:

Stored size: 983 Bytes

Contents

GLib.load_class :Bytes

module GLib
  # Overrides for GBytes, GLib's immutable array of bytes.
  class Bytes
    include Enumerable

    remove_method :get_data if method_defined? :get_data

    # @override
    def get_data
      length_ptr = GirFFI::InOutPointer.for :gsize
      data_ptr = Lib.g_bytes_get_data self, length_ptr
      length = length_ptr.to_value
      # NOTE: Needed due to mis-identification of the element-type of the
      # resulting sized array for the default binding.
      GirFFI::SizedArray.wrap(:guint8, length, data_ptr)
    end

    def each(&block)
      data.each(&block)
    end

    def self.from(it)
      case it
      when self
        it
      when FFI::Pointer
        wrap it
      else
        new it
      end
    end

    def initialize(arr)
      data = GirFFI::SizedArray.from :guint8, arr.size, arr
      store_pointer Lib.g_bytes_new data.to_ptr, data.size
    end

    private

    def data
      @data ||= get_data
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gir_ffi-0.9.2 lib/ffi-glib/bytes.rb
gir_ffi-0.9.1 lib/ffi-glib/bytes.rb
gir_ffi-0.9.0 lib/ffi-glib/bytes.rb
gir_ffi-0.8.6 lib/ffi-glib/bytes.rb
gir_ffi-0.8.5 lib/ffi-glib/bytes.rb
gir_ffi-0.8.4 lib/ffi-glib/bytes.rb