Tech News
← Back to articles

Zig's New Writer

read original related products more articles

Zig's new Writer

As you might have heard, Zig's Io namespace is being reworked. Eventually, this will mean the re-introduction of async. As a first step though, the Writer and Reader interfaces and some of the related code have been revamped.

This post is written based on a mid-July 2025 development release of Zig. It doesn't apply to Zig 0.14.x (or any previous version) and is likely to be outdated as more of the Io namespace is reworked.

Not long ago, I wrote a blog post which tried to explain Zig's Writers. At best, I'd describe the current state as "confusing" with two writer interfaces while often dealing with anytype . And while anytype is convenient, it lacks developer ergonomics. Furthermore, the current design has significant performance issues for some common cases.

The new Writer interface is std.Io.Writer . At a minimum, implementations have to provide a drain function. Its signature looks like:

fn drain ( w : * Writer , data : [ ] const [ ] const u8 , splat : usize ) Error ! usize

You might be surprised that this is the method a custom writer needs to implemented. Not only does it take an array of strings, but what's that splat parameter? Like me, you might have expected a simpler write method:

fn write ( w : * Writer , data : [ ] const u8 ) Error ! usize

It turns out that std.Io.Writer has buffering built-in. For example, if we want a Writer for an std.fs.File , we need to provide the buffer:

var buffer : [ 1024 ] u8 = undefined ; var writer = my_file . writer ( & buffer ) ;

... continue reading