Compile-Time Configuration For Zig Libraries
More from openmymind.net
Zig doesn't have an interface keyword or some simple way to create interfaces. Nonetheless, types like std.mem.Allocator and std.Random are often cited as examples that, with a bit of elbow grease, Zig offers every thing needed to create them. We've looked at Zig Interfaces in the past. If you want to understand how they work and how to write...
If you look at my httpz library, you'll notice that httpz.Server(T) is a generic. The type passed to Server serves two purposes. The first is to support an application-specific context - whatever instance of T passed into Server(T).init gets passed back to your custom HTTP handlers. But the other purpose of T is to act as a configuration object....
In an older post, we explored Zig's @ptrCast and then looked at a concrete usage in the shape of std.heap.MemoryPool. @ptrCast As a brief recap, when we use @ptrCast, we're telling the compiler to treat a pointer as a given type. For example, this code runs fine: const std = @import("std"); pub fn main() !void { var user = User{.power = 9001,...
Software developers are often evaluated based on how well they understand specific ideas and tools. While mastery is important, there's another type of knowledge I find myself relying on: vague awareness. Unlike mastery, awareness is merely knowing that something exists along with a basic understanding of what it is and what problem it can solve....
First, the code: std.mem.sort([]const u8, values, {}, stringLessThan); fn stringLessThan(_: void, lhs: []const u8, rhs: []const u8) bool { return std.mem.order(u8, lhs, rhs) == .lt; } std.mem.sort takes 4 arguments: the type of value we're sorting, the list of values to sort, an arbitrary context, and a function. The last argument, the...