Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
arkadiuszwojcik opened this issue Jun 3, 2024 · 7 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@arkadiuszwojcik
Copy link

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
Copy link
Contributor

nektro commented Jun 3, 2024

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

@arkadiuszwojcik
Copy link
Author

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
Copy link
Contributor

nektro commented Jun 3, 2024

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

@arkadiuszwojcik
Copy link
Author

arkadiuszwojcik commented Jun 4, 2024

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

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library. labels Jul 17, 2024
@andrewrk andrewrk added this to the 1.0.0 milestone Jul 17, 2024
@tgschultz
Copy link
Contributor

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

@arkadiuszwojcik
Copy link
Author

@tgschultz in comparison to SliceReader it is missing peek operation. I wonder is such operation should by dedicated or be emulated by using SeekableStream so then FixedBufferStream can be used.

@arkadiuszwojcik
Copy link
Author

With #22125 merged I this issue can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

4 participants