Understanding common std.Io.Reader/Writer mistakes (0.15.1)

Also:

I think the main thing here is to understand the semantics of @fieldParentPtr and what it’s doing, and given that what would be known known at the callsite when @fieldParentPtr is called. Here, you’re correctly taking the address of the interface field instead of assigning a new value. So when the method is called, the receiver field will be already correctly set to the pointer value of the std.Io.Reader, for which @fieldParentPtr would be able to correctly look up off of. Given that the amount of info that takeDelimiterExclusive has will be the same regardless of whether or not it’s called your way or via reader.interface, there should be no harm in doing it this way. Just make sure that reader does not go out of scope.

PS: Don’t forget you can always read the stdlib docs and follow the code which should be pretty helpful in understanding what you can and can’t do. :slight_smile: If you go down far enough you’ll get to std.fs.File’s implementation of stream where you can see how @fieldParentPtr is used, as an example.

PPS: @kj4tmp has a great writeup that goes more into detail about this stuff: https://ziggit.dev/t/zig-0-15-1-reader-writer-dont-make-copies-of-fieldparentptr-based-interfaces/

1 Like