ziglang / zig

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

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

Open arkadiuszwojcik opened 1 month ago

arkadiuszwojcik commented 1 month 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 1 month ago

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

arkadiuszwojcik commented 1 month 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 1 month ago

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

arkadiuszwojcik commented 1 month 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.