|
1 | 1 | # Layout of Rust array types and slices |
2 | 2 |
|
3 | | -## Layout of Rust array types |
| 3 | +**This page has been archived** |
4 | 4 |
|
5 | | -Array types, `[T; N]`, store `N` values of type `T` with a _stride_ that is |
6 | | -equal to the size of `T`. Here, _stride_ is the distance between each pair of |
7 | | -consecutive values within the array. |
| 5 | +It did not actually reflect current layout guarantees and caused frequent confusion. |
8 | 6 |
|
9 | | -The _offset_ of the first array element is `0`, that is, a pointer to the array |
10 | | -and a pointer to its first element both point to the same memory address. |
11 | | - |
12 | | -The _alignment_ of array types is greater or equal to the alignment of its |
13 | | -element type. If the element type is `repr(C)` the layout of the array is |
14 | | -guaranteed to be the same as the layout of a C array with the same element type. |
15 | | - |
16 | | -> **Note**: the type of array arguments in C function signatures, e.g., `void |
17 | | -> foo(T x[N])`, decays to a pointer. That is, these functions do not take arrays |
18 | | -> as an arguments, they take a pointer to the first element of the array |
19 | | -> instead. Array types are therefore _improper C types_ (not C FFI safe) in Rust |
20 | | -> foreign function declarations, e.g., `extern { fn foo(x: [T; N]) -> [U; M]; |
21 | | -> }`. Pointers to arrays are fine: `extern { fn foo(x: *const [T; N]) -> *const |
22 | | -> [U; M]; }`, and `struct`s and `union`s containing arrays are also fine. |
23 | | -
|
24 | | -### Arrays of zero-size |
25 | | - |
26 | | -Arrays `[T; N]` have zero size if and only if their count `N` is zero or their |
27 | | -element type `T` is zero-sized. |
28 | | - |
29 | | -### Layout compatibility with packed SIMD vectors |
30 | | - |
31 | | -The [layout of packed SIMD vector types][Vector] [^2] requires the _size_ and |
32 | | -_alignment_ of the vector elements to match. That is, types with [packed SIMD |
33 | | -vector][Vector] layout are layout compatible with arrays having the same element |
34 | | -type and the same number of elements as the vector. |
35 | | - |
36 | | -[^2]: The [packed SIMD vector][Vector] layout is the layout of `repr(simd)` types like [`__m128`]. |
37 | | - |
38 | | -[Vector]: packed-simd-vectors.md |
39 | | -[`__m128`]: https://doc.rust-lang.org/core/arch/x86_64/struct.__m128.html |
40 | | - |
41 | | -## Layout of Rust slices |
42 | | - |
43 | | -The layout of a slice `[T]` of length `N` is the same as that of a `[T; N]` array. |
| 7 | +The old content can be accessed [on GitHub](https://github.com/rust-lang/unsafe-code-guidelines/blob/c138499c1de03b908dfe719a41193c84f8146883/reference/src/layout/arrays-and-slices.md). |
0 commit comments