Skip to content

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 23 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Integers and Floating Points](./layout/integers-floatingpoint.md)
- [Enums](./layout/enums.md)
- [Unions](./layout/unions.md)
- [Arrays](./layout/arrays.md)
- [Vectors](./layout/vectors.md)
- [Optimizations](./optimizations.md)
- [Optimizing immutable memory](./optimizations/immutable_memory.md)
Expand Down
33 changes: 33 additions & 0 deletions reference/src/layout/arrays.md
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
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.

`repr(C)` arrays have the same layout as C arrays and are passed by pointer in C
FFI according to the C ABI.

## 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)
3 changes: 2 additions & 1 deletion reference/src/layout/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ currently different for each architecture.
## Vector types

Vector types are `repr(simd)` homogeneous tuple-structs containing `N` elements
of type `T` where `N` is a power-of-two:
of type `T` where `N` is a power-of-two and the size and alignment requirements
of `T` are equal:

```rust
#[repr(simd)]
Expand Down