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
BoundedArray: a simple way to represent small data whose max size is known #9134
Conversation
You can already use a |
New data is concatenated to existing data, In non-fifo contexts, the common practice is to include a buffer and a length for every variable-size member of a structure, and manually set the length and get slices out of these. This is not very convenient especially in public APIs. |
Yeah, using a While writing the CPU detection code I needed something like this What do you think of making this type an
I guess this refers to the |
The ability to add an element or a slice may indeed be useful and would be trivial to add. Are you suggesting that all the methods from |
All the methods that make sense given the fixed capacity, yes. |
Done. |
I'm writing a text processor in Zig and using similar data struct to FixedSlice or ArrayListFixed quite often to contains chars of words (<30), or words of phrases (<15) ... So +1 for it |
Used everywhere in https://github.com/jedisct1/zig-hpke too. |
One thought I have here is that this To make this distinction clear, maybe it would be better avoid the name EDIT: actually now that I think about it, an |
All these directly describe something we already have: a slice :) Damn. Naming things is hard! |
Maybe a Could also call it |
So, would anybody be strongly against |
I suggest It's interesting that the methods use errors rather than assertions. Everywhere I can think of a use for this type, I would end up using The use case for this type is a bit strange: you have an upper bound on the size needed for the array, but the upper bound is not correct; it can be exceeded depending on runtime conditions. |
This is a simple structure containing an array and a length, that can be viewed as a slice. It is useful to pass-by-copy small data whose exact size is known at runtime, but whose maximum size is known at comptime. This greatly simplifies code that otherwise would require an allocator, or reimplementing what this type does.
It's always nice to know some half-assed thing you had to roll yourself really was useful enough to belong in the standard library. From the PR discussion it sounds like a lot of people were independently having to invent that wheel: ziglang/zig#9134
This is a simple structure containing an array and a length, that can be viewed as a slice.
It is useful to pass-by-copy small data whose whose maximum size is known at comptime but whose exact size is only known at runtime. I found myself using this a lot as it greatly simplifies things over allocators/unions/the-same-thing-done-manually.
Introducing a new type may not be necessary, but it looks like people have been reimplementing something similar in different projects, or resorted to using an
Allocator
where not really required.FixedSlice
is probably not a good name. Suggestions are welcome :)