This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Namespace
conststd = @import("std.zig");
constStringHashMap = std.StringHashMap;
constmem = std.mem;
constAllocator = mem.Allocator;
consttesting = std.testing;
/// BufMap copies keys and values before they go into the map and/// frees them when they get removed.pubconstBufMap = struct {
hash_map: BufMapHashMap,
constBufMapHashMap = StringHashMap([]constu8);
/// Create a BufMap backed by a specific allocator./// That allocator will be used for both backing allocations/// and string deduplication.pubfninit(allocator: Allocator) BufMap {
return .{ .hash_map = BufMapHashMap.init(allocator) };
}
/// Free the backing storage of the map, as well as all/// of the stored keys and values.pubfndeinit(self: *BufMap) void {
varit = self.hash_map.iterator();
while (it.next()) |entry| {
self.free(entry.key_ptr.*);
self.free(entry.value_ptr.*);
}
self.hash_map.deinit();
}
/// Same as `put` but the key and value become owned by the BufMap rather/// than being copied./// If `putMove` fails, the ownership of key and value does not transfer.pubfnputMove(self: *BufMap, key: []u8, value: []u8) !void {
constget_or_put = tryself.hash_map.getOrPut(key);
if (get_or_put.found_existing) {
self.free(get_or_put.key_ptr.*);
self.free(get_or_put.value_ptr.*);
get_or_put.key_ptr.* = key;
}
get_or_put.value_ptr.* = value;
}
/// `key` and `value` are copied into the BufMap.pubfnput(self: *BufMap, key: []constu8, value: []constu8) !void {
constvalue_copy = tryself.copy(value);
errdeferself.free(value_copy);
constget_or_put = tryself.hash_map.getOrPut(key);
if (get_or_put.found_existing) {
self.free(get_or_put.value_ptr.*);
} else {
get_or_put.key_ptr.* = self.copy(key) catch |err| {
_ = self.hash_map.remove(key);
returnerr;
};
}
get_or_put.value_ptr.* = value_copy;
}
/// Find the address of the value associated with a key./// The returned pointer is invalidated if the map resizes.pubfngetPtr(self: BufMap, key: []constu8) ?*[]constu8 {
returnself.hash_map.getPtr(key);
}
/// Return the map's copy of the value associated with/// a key. The returned string is invalidated if this/// key is removed from the map.pubfnget(self: BufMap, key: []constu8) ?[]constu8 {
returnself.hash_map.get(key);
}
/// Removes the item from the map and frees its value./// This invalidates the value returned by get() for this key.pubfnremove(self: *BufMap, key: []constu8) void {
constkv = self.hash_map.fetchRemove(key) orelsereturn;
self.free(kv.key);
self.free(kv.value);
}
/// Returns the number of KV pairs stored in the map.pubfncount(self: BufMap) BufMapHashMap.Size {
returnself.hash_map.count();
}
/// Returns an iterator over entries in the map.pubfniterator(self: *constBufMap) BufMapHashMap.Iterator {
returnself.hash_map.iterator();
}
fnfree(self: BufMap, value: []constu8) void {
self.hash_map.allocator.free(value);
}
fncopy(self: BufMap, value: []constu8) ![]u8 {
returnself.hash_map.allocator.dupe(u8, value);
}
};
test"BufMap" {
constallocator = std.testing.allocator;
varbufmap = BufMap.init(allocator);
deferbufmap.deinit();
trybufmap.put("x", "1");
trytesting.expect(mem.eql(u8, bufmap.get("x").?, "1"));
trytesting.expect(1 == bufmap.count());
trybufmap.put("x", "2");
trytesting.expect(mem.eql(u8, bufmap.get("x").?, "2"));
trytesting.expect(1 == bufmap.count());
trybufmap.put("x", "3");
trytesting.expect(mem.eql(u8, bufmap.get("x").?, "3"));
trytesting.expect(1 == bufmap.count());
bufmap.remove("x");
trytesting.expect(0 == bufmap.count());
trybufmap.putMove(tryallocator.dupe(u8, "k"), tryallocator.dupe(u8, "v1"));
trybufmap.putMove(tryallocator.dupe(u8, "k"), tryallocator.dupe(u8, "v2"));
}