Video: Zig's new Reader and Writer interfaces (std.Io, 0.15.1)

Hello! I made a video about the new std.Io.Reader and std.Io.Writer interfaces. I explain how to use the new Reader and Writer interfaces, explain how/why they work. I also talk about concrete implementations: std.fs.File.Reader, std.fs.File.Writer, std.Io.Writer.Allocating, std.Io.Writer.Discarding…
I show how to read from the standard input (stdin) and from a file, and how to write to the standard output! I also talk about common mistakes to avoid!

I didn’t talk about:

  • splat
  • vectored IO
  • how to actually implement the interface
  • direct file-to-file transfer
  • and more…

I hope this can be helpful!

21 Likes

This is a good introduction to the new interfaces, a few of criticisms.

takeArray returns a pointer to an array, not a slice.

You also didn’t explain why you can’t take an array larger than the buffer, it isn’t obvious to everyone, especially those new to low level languages or programming in general, it also just might not occur to them.

You should have mentioned that Writer.Allocating is overkill, and that the API allows you to avoid allocations.

You did mention that Writer.Discarding was overkill, and you were showing how to use multiple writers, but you should have mentioned the discard function on the Reader interface being much more efficient.

1 Like

Thank you for the feedback!

My bad, I didn’t pay enough attention!
What do you mean by:

i mean, the interfaces api has a lot of functions to use the interfaces buffer, instead of providing your own or streaming to a writer. This means you don’t need to allocate, on the heap or stack, to use the interfaces (except the interface buffer ofc, it does need to be larger than 0 for these functions).

You used some in your video (7:50). But you didn’t explicitly touch on it, which I think you should have.

I also just noticed, you are not using [reader/writer]Streaming for stdio, they will detect that they need to be in streaming mode but that wastes a syscall and results in the first use reading/writing 0 data. If you know ahead of time that the file needs to use streaming mode, you should start with it, instead of relying on it changing modes.

It changes modes so it can still work with files you don’t know ahead of time and with APIs that you don’t control.