ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.79k stars 2.54k forks source link

Proposal: Consider BufferReader and BufferWriter implementation in std.mem #20179

Open arkadiuszwojcik opened 5 months ago

arkadiuszwojcik commented 5 months ago

I'm quite new to the Zig community, so I may not have seen a lot, but recently, while submitting a PR to the MicroZig project, I had to implement BufferReader and BufferWriter types. This is quite common pattern (I think) and Zig also use it in one place where it is called FixedBufferReader. I think something like that is very useful and should be promoted to std.mem namespace, although FixedBufferReader itself have some specyfic functions (like: readUleb128) I would not expect in generic implementation. FixedBufferWriter would be useful in cases when we have to join together a lot os small byte chunks while serializing. So together FixedBufferReader and FixedBufferWriter would be very nice addition to std.mem namespace in my opinion.

nektro commented 5 months ago

are you perhaps looking for std.io.BufferedReader and std.io.BufferedWriter ?

arkadiuszwojcik commented 5 months ago

There is a lot of similarities but this is a bit different. FixedBufferReader don't relay on underlying Reader as all data are already in fixed length memory slice. So in some sense all BufferedReader implement FixedBufferReader interface + additional operations to work with underlying reader. So this is something in between concept of slice and BufferedReader/BufferedWriter or it can be thought of as advance wrapper around slice that tracks current positions. Most of functions in std.mem operate on slices but are stateless (no current position tracking), FixedBufferReader adds state around slices.

nektro commented 5 months ago

in that case it sounds like std.BoundedArray and std.fifo.LinearFifo ?

arkadiuszwojcik commented 5 months ago

Please take a look on FixedBufferReader it was made public in std 3 weeks ago. This is exactly what I am talking about. It should be promoted (with minor changes) to more generic namespace std.mem instead of being part of specialized std.dwarf namespace. Similar functionality is also impemented in Uri struct by SliceReader

tgschultz commented 3 weeks ago

Perhaps I'm missing something, but isn't FixedBufferStream in std.io what you're looking for? If not, can you describe why not? https://github.com/ziglang/zig/blob/master/lib/std/io/fixed_buffer_stream.zig