Skip to content

Instantly share code, notes, and snippets.

@abradley2
Created April 6, 2025 14:47
Show Gist options
  • Save abradley2/91d9aa67978505376f062d3d223bc2f9 to your computer and use it in GitHub Desktop.
Save abradley2/91d9aa67978505376f062d3d223bc2f9 to your computer and use it in GitHub Desktop.
Zig Writer Example
pub const Foo: type = struct {
alloc: std.mem.Allocator,
buff: std.ArrayListUnmanaged(u8),
pub fn init(alloc: std.mem.Allocator) Foo {
return Foo{
.alloc = alloc,
.buff = std.ArrayListUnmanaged(u8){},
};
}
pub const Writer = std.io.GenericWriter(*Foo, Foo.Error, Foo.writeFn);
pub fn writer(self: *Foo) Writer {
return Foo.Writer{ .context = self };
}
pub fn writeFn(self: *Foo, b: []const u8) Foo.Error!usize {
try self.buff.appendSlice(self.alloc, b);
return b.len;
}
pub const Error: type = error{
OutOfMemory,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment