Skip to content

Commit b7c749d

Browse files
committed
Layout of arrays
Closes rust-lang#91 .
1 parent 19f4223 commit b7c749d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

reference/src/layout/arrays.md

Lines changed: 33 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ of type `T` where `N` is a power-of-two:
2222
struct Vector<T, N>(T_0, ..., T_(N - 1));
2323
```
2424

25-
The set of supported values of `T` and `N` is _implementation-defined_.
25+
The size and alignment requirements of `T` are required to match, but the exact
26+
set of supported values of `T` and `N` is _implementation-defined_.
2627

2728
The size of `Vector` is `N * size_of::<T>()` and its alignment is an
2829
_implementation-defined_ function of `T` and `N` greater than or equal to

0 commit comments

Comments
 (0)