-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Make slices (optionally) packed #2201
Comments
So in my PR there is an error because one function is fed with a pointer and a length as separate parameters, coming from a slice. The function expects default alignment, and the slice fields are accessed as It's easy to make it work, just add Is this a temporary state of things, or a decision needed/set in stone ? I feel like it could be avoided in cases where the fields actually do not need to be Also note that in this particular case, it's probably only an issue when interfacing with C code, because idiomatic zig code would use slices directly and not decompose them. @andrewrk I'm interested on your thoughts about this. |
Related to #2652? |
Unless the |
That wasn't what I was thinking of, but actually it could: the
I meant the same thing as in the packed pointer proposal: taking off the bits unused due to pointer alignment. |
That's just it though - if they don't have debug safety, then slices would not be able to participate in, for example, #2414. I think there may be a better way to solve the use case outlined here. After #1512 I think we can relax the restriction about what types can go in packed structs. A packed struct would have well-defined memory layout if and only if all its fields did. So your above example would work, but I'd like to solve #1512 as a prerequisite for this proposal, so I'm going to postpone this proposal for next release cycle. |
Fun fact, test "" {
const Foo = packed union {
bar: []u32,
};
} is currently allowed |
It's not planned to have optionally packed slices, however it is planned that packed structs will be allowed to contain types which do not have well defined memory layouts. |
Is there an open (and accepted) issue for that? |
They currently are not, forbidding this usecase:
As long as no debug safety is embedded within them, I see no reason that they couldn't be packed.
The text was updated successfully, but these errors were encountered: