-
Notifications
You must be signed in to change notification settings - Fork 82
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
Fixed length arrays #181
Comments
That does seem like a potentially good idea, and could be defined as another "specialization" (just like how |
A similar issue that came up earlier was IPv6 addresses, which would want to use |
Oh right, that was it, thanks. I suppose mostly this is just a question of prioritization and whether we think there are enough use cases to motivate adding this in the MVP time frame. |
Just stumbled across this - I'd be very interested in these types for cryptographic hashes, in case there's a need for more motivating examples |
Seconding @cameron1024 use case. I've been trying to understand wit, components, and wit-bindgen using the md5 algorithm. Being able to create |
This actually happened to be my use-case too. I have ids for certain resources that are referenced by host and guest that are the output of cryptographic hashes so always have a fixed length. It's a bit of a PITA having to check the length and copy the bytes into a fixed length array on both sides. |
It does seem like there are some good use cases for this and it's not a big addition if this is just a specialization of |
For reference: WebAssembly/interface-types#146. I think that issue was raised before the existence of tuples, though. |
I’m working on the Go bindings for WASI Preview 2 and the Component Model. The type ipv4-address = tuple<u8, u8, u8, u8>;
type ipv6-address = tuple<u16, u16, u16, u16, u16, u16, u16, u16>; Could be: type ipv4-address = [8]u8;
type ipv6-address = [8]u8; (Go-style syntax) It seems like a fixed-width array could just be syntactic sugar for |
Is there any update on supporting this feature? I have several similar use-cases to those pointed out above as well. |
Do you have a specific use-case that tuples can't handle? |
Tuples handle these use-cases but are rather messy to specify especially for larger fixed sizes. Something similar to @ydnar's solution would be useful. |
Monotypic |
Now that Preview 2 has shipped, I think it makes sense to add this as an emoji-gated feature (to be ungated once widely implemented in Preview 2 runtimes and included in a subgroup-approved 0.2.* release). I can write a PR for this in a bit if noone does it first. Syntactically in WIT and the C-M, I'm leaning toward |
I think the form of Other possible benefits of implementing fixed-length arrays:
|
We already have the Canonical ABI varying significantly for specializations (e.g. |
That's not what you want. Tuples are accessed with a static index and no runtime overhead, easily verifiable, whereas lists/arrays are accessed with a dynamic index and (usually) a runtime bounds check, at least in typed languages that distinguish the two. Different trade-offs that API designers supposedly pick consciously and an ABI should not mix up. |
I think the gist of the OP (which I agree with) is that a fixed-length array is ABI identical to a tuple with a single type. Which isn’t the same as a list—as you correctly pointed out has a header, dynamic index, overhead, etc. |
No, a list/array with fixed length is still indexed with a dynamic index, like any other list/array, which requires a check, like for any other list/array. The only difference is that you statically know one side of that check. A tuple is a different concept with no dynamic indexing nor checking. |
Ah, I think I understand the disconnect: @ydnar is primarily concerned with the Canonical ABI representation here, specifically that, whatever we call it, our fixed-length-array is given the same "inline" representation as the N-ary tuples that it is replacing (and not a (ptr, length) pair pointing to separately-allocated memory). I didn't mention it above, but I agree this is the representation we want and this is totally doable even if we call it |
PR up in #304, PTAL |
I would approciate fix length list (like mac address, tuple<u8, u8, u8, u8, u8, u8> is just messy), however I believe fixed length array and dynamic length array would be very different from the ABI perspective? like std::array is N consecutive Ts lives on stack, while std::vector is a pointer point to a heap allocated buffer(along with a size & capacity member). |
Yes, the ABIs (as defined in #304) are totally different. |
Rust has fixed length arrays whose type is written as
[u8;32]
for a 32-byte array. You can achieve this in the current wit specification by doingtuple<u8,u8 ... u8>
(with 32u8
s). Couldlist<u8,32>
or something be a first class type to avoid abusing tuples in this way even if it desugars to something similar?The text was updated successfully, but these errors were encountered: