-
Notifications
You must be signed in to change notification settings - Fork 59
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
Layout of arrays #91
Comments
I expect there is little controversy here: The first element is at offset 0, the stride is the element type size. It'd be nice to eventually have types with stride > size (avoiding padding at the end), but I am not sure if that is realistic? |
I'm not sure I follow, do you mean #[repr(align(4))] struct A([u8; 3]); // size_of == 4
type B = [A; 4]; // size_of == 12 instead of 16 ? |
No, that makes no sense. Then elements would overlap. I mean struct A(u16, u8); // size == 3
type B = [A; 4]; // size == 16 because all elements need to be aligned |
So to be clear, I think we should keep open the option of eventually having types where the size is not a multiple of the alignment, which implies that arrays of such types will have to have To that end, we should declare arrays as something like: The first element is at offset 0, and the stride of the array is computed as the size of the element type rounded up to the next multiple of the alignment of the element type. |
See rust-lang/rfcs#1397. |
@gnzlbg remarks that we should also state explicitly that since |
@Amanieu thanks! The plan here is not to specify that this actually happens. but to word things in a way that it can happen in the future. That RFC points out that this interacts with |
One of the many options that were proposed in #36 were that |
For now I think we should just document how these decisions affect each other. |
Does anybody have an example of such a type? |
Closes rust-lang#91 .
Closes rust-lang#91 .
Closes rust-lang#91 .
Closes rust-lang#91 .
struct A(u16, u8); // size = 3
struct Homogeneous(A, A, A); // size = 4+4+3 = 11 because we avoid padding at the end
type Array = [A; 3]; // naively, size = 3*4 = 12 |
That would make sense if we made a distinction between "size" vs "stride" in the language, but we don't. As is, the size has to be a multiple of the alignment and trailing padding of fields can't be repurposed because anyone with a |
@rkruppe see above for the prior discussion where I basically argue we should be forwards-compatible with a world where size and stride can be different. I don't think this is fundamentally incompatible with mutable references. There are some hard questions around using |
Ah, I see. I disagree, but I'll take that over to the PR. |
We are missing the layout of arrays (I think we just forgot about this). Tangentially related to the layout of homogeneous structs: #36
The text was updated successfully, but these errors were encountered: