No `reader()` method of `std.boundedArray`?

I can do a work around with fixed buffer stream on the .slice() method. But does anyone else think it should have a .reader() method?

The std library is not fully fleshed out. It’s been built out as needed by the compiler and as people have contributed to it, so there are probably a lot of gaps like this. They plan to do a few audits of the std library as they get closer to a 1.0 release.

Sounds like a great first contribution to the zig language though.

3 Likes

ArrayList doesn’t have a reader method either. Insofar as there’s a reason for this, it’s that no custom logic is needed to read from an array (whether BoundedArray or ArrayList), you can just get the slice and make one of a few Reader types from that.

I think reaching for this pattern should come with asking yourself if it’s a good idea. Readers essentially take some source and read it into a []T, but you already have a []T: you can use functions in std.mem to seek in it, if you need to copy it, you can just copy it, and so on.

That isn’t to say it’s never the right thing to do: in particular, some function might expect a Reader, and you’ll need to give it one. But this is an ok argument for not having a .reader() helper method built in, that would encourage copying when it isn’t really necessary. Better to take two or three lines of setup, rather than end up with authors thinking they’re “supposed to” make a reader to work with data in some array type, even though that frequently isn’t necessary.

So I’m weakly against adding this to std for that reason: having just enough friction to prompt some thought about whether making a Reader is actually necessary.

2 Likes

I’ve found that I reach for a read() method in my own code, it’s usually to provide ergonomics for all of the bookkeeping that goes into maintaining a certain data structure (for example, a ring buffer can have different kinds of bookkeeping techniques to handle the wrap-around) or providing reasonable defaults for reducing the amount of system calls (such as a buffered_reader). If the read function is going to essentially just replace a @memcpy call and an index increment, then I don’t think it’s pulling its weight as an abstraction.

1 Like