Skip to content

Commit 0b4e710

Browse files
committed
Layout of arrays
Closes #91 .
1 parent 19f4223 commit 0b4e710

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

reference/src/layout/arrays.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Layout of Rust array types
2+
3+
Array types, `[T; N]`, store `N` values of type `T` contiguously with a constant
4+
_stride_ (the distance between each two consecutive values).
5+
6+
The offset of the first array element is `0`.
7+
8+
The stride of the array is computed as the size of the element type rounded up
9+
to the next multiple of the alignment of the element type. That is, the _stride_
10+
of an array can be larger than the element size iff the alignment requirements
11+
of the element are larger than its size.
12+
13+
Having a stride larger than the element size allows:
14+
15+
```rust,ignore
16+
struct A(u16, u8); // size_of == 3, align_of == 4
17+
type B = [A; 4]; // => stride == 4 > 3
18+
```
19+
20+
The size and alignment of `Vector` element types match, such that `Vector` types
21+
and arrays are layout compatible.
22+
23+
`repr(C)` arrays have the same layout as C arrays and are passed by pointer in C
24+
FFI according to the C ABI.
25+
26+
## Unresolved questions
27+
28+
### Stride > size
29+
30+
The current layout guarantees for `repr(Rust)` structs guarantee that Rust is forward-compatible with proposals allowing `stride > size`, like:
31+
32+
* [rust-lang/rfcs/1397: Spearate size and stride for types](https://github.com/rust-lang/rfcs/issues/1397)
33+
* [rust-lang/rust/17027: Collapse trailing padding](https://github.com/rust-lang/rust/issues/17027)

reference/src/layout/vectors.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ currently different for each architecture.
1515
## Vector types
1616

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

2021
```rust
2122
#[repr(simd)]

0 commit comments

Comments
 (0)