How to manage memory with light wrappers?

I am making a library with these structs:

  • Vault is just a light wrapper around StringHashMapUnmanaged(Item).
  • Item is light wrapper around StringHashMapUnmanaged(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.

  1. a video worth watching on that topic Programming without pointers
  2. perhaps worth looking at the data design differently. what if you make a composite key instead? like a struct{key1: []const u8, key2; []const u8}

in the end it could be:

key = struct{
  key1: []const u8,
  key2: []const u8,
};

vault = struct{
    keys: Strings,
    keys: std.ArrayList(key),
    values: std.ArrayList(Value),
   
   fn deinit(self: *vault, alloc: Allocator) void {
        alloc.free(self.keys.buf);
        self.keys.deinit(alloc)
        self.values.deinit(alloc)
    }
}

an come up with your own hashing/context or whatever, I’ve never done a custom keys for a hashmap