Sha256: 7d187ea94f021133d7066703a249cac0454e52eb604d1a3b5d7080bfe9a95ca6

Contents?: true

Size: 1019 Bytes

Versions: 14

Compression:

Stored size: 1019 Bytes

Contents

# frozen_string_literal: true

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
    # NOTE: Needed due to mis-identification of the element-type of the
    # resulting sized array for the default binding.
    def get_data
      length_ptr = FFI::MemoryPointer.new :size_t
      data_ptr = Lib.g_bytes_get_data self, length_ptr
      length = length_ptr.get_size_t(0)
      GirFFI::SizedArray.wrap(:guint8, length, data_ptr)
    end

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

    def self.from(obj)
      case obj
      when self
        obj
      when FFI::Pointer
        wrap obj
      else
        new obj
      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

14 entries across 14 versions & 1 rubygems

Version Path
gir_ffi-0.15.9 lib/ffi-glib/bytes.rb
gir_ffi-0.15.8 lib/ffi-glib/bytes.rb
gir_ffi-0.15.7 lib/ffi-glib/bytes.rb
gir_ffi-0.15.6 lib/ffi-glib/bytes.rb
gir_ffi-0.15.5 lib/ffi-glib/bytes.rb
gir_ffi-0.15.4 lib/ffi-glib/bytes.rb
gir_ffi-0.15.3 lib/ffi-glib/bytes.rb
gir_ffi-0.15.2 lib/ffi-glib/bytes.rb
gir_ffi-0.15.1 lib/ffi-glib/bytes.rb
gir_ffi-0.15.0 lib/ffi-glib/bytes.rb
gir_ffi-0.14.1 lib/ffi-glib/bytes.rb
gir_ffi-0.14.0 lib/ffi-glib/bytes.rb
gir_ffi-0.13.1 lib/ffi-glib/bytes.rb
gir_ffi-0.13.0 lib/ffi-glib/bytes.rb