-
Notifications
You must be signed in to change notification settings - Fork 60
Layout of arrays #94
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
Merged
Merged
Layout of arrays #94
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6358eef
Layout of arrays
gnzlbg 3a7fed0
Document that in current Rust stride == size
gnzlbg afc53ad
Hyperlink Vector
gnzlbg 56e7f28
There are no repr(C) arrays
gnzlbg b8e108b
Further clarify Vector layout
gnzlbg 324e98d
Expand definition of stride
gnzlbg 6e1036b
Expand consequence of offset 0
gnzlbg 43a21a4
Reword the proposal
gnzlbg 8c0d1ff
More rewording
gnzlbg d5221e7
Clarify why the array alignment is unspecified
gnzlbg 0f358b2
Fix Vector links
gnzlbg 70ed080
Language
gnzlbg 0f8a4f5
Fix rendering
gnzlbg 7511825
Guarantees about array alignment
gnzlbg e3df96c
Document array types in C FFI
gnzlbg 472f226
Language
gnzlbg 9b45f31
Language
gnzlbg 40e88dd
Add layout of slices; clarify packed SIMD vectors
gnzlbg 516847e
Fix links
gnzlbg 64f6ea6
Add footnote clarifying what packed means
gnzlbg e983592
Add link to RFC2366 text
gnzlbg 292039e
Structs and unions containing array are fine in C FFI
gnzlbg 9ffe806
Avoid repetition
gnzlbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Layout of Rust array types | ||
|
||
Array types, `[T; N]`, store `N` values of type `T` contiguously with a constant | ||
_stride_ (the distance between each two consecutive values). | ||
|
||
The offset of the first array element is `0`. | ||
|
||
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. That is, the _stride_ | ||
of an array can be larger than the element size iff the alignment requirements | ||
of the element are larger than its size. | ||
|
||
Having a stride larger than the element size allows: | ||
|
||
```rust,ignore | ||
struct A(u16, u8); // size_of == 3, align_of == 4 | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type B = [A; 4]; // => stride == 4 > 3 | ||
``` | ||
|
||
The size and alignment of `Vector` element types match, such that `Vector` types | ||
and arrays are layout compatible. | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
`repr(C)` arrays have the same layout as C arrays and are passed by pointer in C | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FFI according to the C ABI. | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Unresolved questions | ||
|
||
### Stride > size | ||
|
||
The current layout guarantees for `repr(Rust)` structs guarantee that Rust is forward-compatible with proposals allowing `stride > size`, like: | ||
|
||
* [rust-lang/rfcs/1397: Spearate size and stride for types](https://github.com/rust-lang/rfcs/issues/1397) | ||
* [rust-lang/rust/17027: Collapse trailing padding](https://github.com/rust-lang/rust/issues/17027) | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.