// An opaque, non-owning handle to a `GHashTable*`. Generated wrappers copy a
// table into a MoonBit `Map` by iterating it (return direction), or build one
// to pass to C (parameter direction). Ownership is explicit: call `unref` on a
// table you own (transfer full / one you built); leave borrowed tables alone.
///|
#external
pub type HashTable
///|
/// GC-managed iterator over a `HashTable`'s entries. Reclaimed automatically;
/// the underlying table must outlive it (it always does within a wrapper).
pub type HashTableIter
///|
/// Open an iterator positioned before the first entry.
#borrow(self)
pub extern "c" fn HashTable::iter(self : HashTable) -> HashTableIter = "moonbit_glib_hash_table_iter"
///|
/// Advance to the next entry; returns `false` once the entries are exhausted.
#borrow(self)
pub extern "c" fn HashTableIter::next(self : HashTableIter) -> Bool = "moonbit_glib_hash_table_iter_next"
///|
/// Current entry's key as the raw bytes of a C string (utf8/filename keys).
#borrow(self)
pub extern "c" fn HashTableIter::key_bytes(self : HashTableIter) -> Bytes = "moonbit_glib_hash_table_iter_key_bytes"
///|
/// Current entry's value as the raw bytes of a C string.
#borrow(self)
pub extern "c" fn HashTableIter::val_bytes(self : HashTableIter) -> Bytes = "moonbit_glib_hash_table_iter_val_bytes"
///|
/// Current entry's key as an int (primitive / enum / bool keys).
#borrow(self)
pub extern "c" fn HashTableIter::key_int(self : HashTableIter) -> Int = "moonbit_glib_hash_table_iter_key_int"
///|
/// Current entry's value as an int.
#borrow(self)
pub extern "c" fn HashTableIter::val_int(self : HashTableIter) -> Int = "moonbit_glib_hash_table_iter_val_int"
///|
/// Number of entries.
#borrow(self)
pub extern "c" fn HashTable::size(self : HashTable) -> Int = "moonbit_glib_hash_table_size"
///|
/// Drop a logical reference. Call this on tables you own (a transfer-full
/// return, or one you built with `new_str`), never on a borrowed table.
#borrow(self)
pub extern "c" fn HashTable::unref(self : HashTable) -> Unit = "moonbit_glib_hash_table_unref"
///|
/// Build an empty owned `GHashTable` (string hash/equal, frees both
/// key and value). For the parameter direction.
pub extern "c" fn HashTable::new_str() -> HashTable = "moonbit_glib_hash_table_new_str"
///|
/// Insert a string key/value (both copied into the table).
#borrow(self, key, value)
pub extern "c" fn HashTable::insert_str_str(
self : HashTable,
key : Bytes,
value : Bytes,
) -> Unit = "moonbit_glib_hash_table_insert_str_str"
///|
/// Test helper: a freshly-built `GHashTable` handed to MoonBit with
/// transfer-full ownership.
extern "c" fn ffi_test_hash_table_str_str() -> HashTable = "moonbit_glib_test_hash_table_str_str"
///|
/// Copy a C-owned table into a `Map` (return direction), then release it.
pub fn test_get_hash_table() -> Map[String, String] {
let table = ffi_test_hash_table_str_str()
let map : Map[String, String] = Map([])
let iter = table.iter()
while iter.next() {
map[@encoding/utf8.decode_lossy(iter.key_bytes())] = @encoding/utf8.decode_lossy(
iter.val_bytes(),
)
}
table.unref()
map
}
///|
/// Build a table from a `Map` (parameter direction), hand it to C, release it.
pub fn test_roundtrip_hash_table(m : Map[String, String]) -> Int {
let table = HashTable::new_str()
for k, v in m {
table.insert_str_str(@encoding/utf8.encode(k), @encoding/utf8.encode(v))
}
let n = table.size()
table.unref()
n
}