I am making a library with these structs:
Vaultis just a light wrapper aroundStringHashMapUnmanaged(Item).Itemis light wrapper aroundStringHashMapUnmanaged(Value).
For both of these structs the user interacts with the hashmap directly, they own the memory too, so when they free the vault it would look like this:
var vault1: Vault = .init();
defer {
var items_it = vault1.items.iterator();
while (items_it.next()) |item_entry| {
std.testing.allocator.free(item_entry.key_ptr.*);
var fields_it = item_entry.value_ptr.fields.iterator();
while (fields_it.next()) |field_entry| {
std.testing.allocator.free(field_entry.key_ptr.*);
field_entry.value_ptr.deinit(std.testing.allocator);
}
item_entry.value_ptr.deinit(std.testing.allocator);
}
vault1.deinit(std.testing.allocator);
}
And if I make the structs owned it would be very annoying for me. For example, for Vault I would have to make a put function that takes in a []const u8 and a Item, then duplicate both of them, and insert it into the hash map. I could make the put function just take ownership, with []u8 and *Item, but I’ll still have to create a wrapper for every function.