class GLib::Bytes

Overview

A simple reference counted data type representing an immutable sequence of zero or more bytes from an unspecified origin.

The purpose of a GBytes is to keep the memory region that it holds alive for as long as anyone holds a reference to the bytes. When the last reference count is dropped, the memory is released. Multiple unrelated callers can use byte data in the GBytes without coordinating their activities, resting assured that the byte data will not change or move while they hold a reference.

A GBytes can come from many different origins that may have different procedures for freeing the memory region. Examples are memory from GLib::malloc, from memory slices, from a GLib::MappedFile or memory from other allocators.

GBytes work well as keys in GLib::HashTable. Use GLib::Bytes#equal and GLib::Bytes#hash as parameters to GLib::HashTable.new or GLib::HashTable#new_full. GBytes can also be used as keys in a GLib::Tree by passing the GLib::Bytes#compare function to GLib::Tree.new.

The data pointed to by this bytes must not be modified. For a mutable array of bytes see GLib::ByteArray. Use GLib::Bytes#unref_to_array to create a mutable array for a GBytes sequence. To create an immutable GBytes from a mutable GLib::ByteArray, use the GLib::ByteArray#free_to_bytes function.

Defined in:

lib/gi-crystal/src/auto/g_lib-2.0/bytes.cr
lib/gi-crystal/src/bindings/g_lib/bytes.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(pointer : Pointer(Void), transfer : GICrystal::Transfer) #

def self.new(data : Pointer, size : Int32) #

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


Instance Method Detail

def compare(bytes2 : GLib::Bytes) : Int32 #

def data : ::Bytes | Nil #

def equal(bytes2 : GLib::Bytes) : Bool #

def finalize #

def hash : UInt32 #
Description copied from class Object

Generates an UInt64 hash value for this object.

This method must have the property that a == b implies a.hash == b.hash.

The hash value is used along with == by the Hash class to determine if two objects reference the same hash key.

Subclasses must not override this method. Instead, they must define hash(hasher), though usually the macro def_hash can be used to generate this method.


def new_from_bytes(offset : UInt64, length : UInt64) : GLib::Bytes #

def region(element_size : UInt64, offset : UInt64, n_elements : UInt64) : Pointer(Void) | Nil #

def size : UInt64 #

def to_unsafe : Pointer(Void) #