22
33## Layout of Rust array types
44
5- Array types, ` [T; N] ` , store ` N ` values of type ` T ` with a constant _ stride_ .
6- Here, _ stride_ is the distance between each pair of consecutive values within
7- the array.
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.
88
99The _ offset_ of the first array element is ` 0 ` , that is, a pointer to the array
1010and a pointer to its first element both point to the same memory address.
@@ -21,33 +21,11 @@ guaranteed to be the same as the layout of a C array with the same element type.
2121> }` . Pointers to arrays are fine: ` extern { fn foo(x: * const [ T; N] ) -> * const
2222> [ U; M] ; }` , and ` struct` s and ` union`s containing arrays are also fine.
2323
24- The _ stride_ of the array is constant for all element pairs and it is computed
25- as the _ size_ of the element type rounded up to the next multiple of the
26- _ alignment_ of the element type.
27-
2824### Arrays of zero-size
2925
3026Arrays ` [T; N] ` have zero size if and only if their count ` N ` is zero or their
3127element type ` T ` is zero-sized.
3228
33- ### Special case ` stride == size `
34-
35- When the element _ size_ is a multiple of the element's _ alignment_ , then `stride
36- == size` , and the elements are laid out contiguously in memory, e.g., ` [ u8; 4] `.
37- In this case, the _ size_ of the array can be computed as ` size_of::<T>() * N ` ,
38- and a pointer to the ` i ` -th element of the array can be obtained by offsetting a
39- pointer to the first element of the array by ` i ` [ ^ 1 ] .
40-
41- > ** Note:** In the current Rust implementation, _ size_ is always a multiple of
42- > the element's _ alignment_ , and therefore ` stride == size ` always holds. This
43- > is, however, not guaranteed by the [ layout of structs and tuples] .
44-
45- [ ^ 1 ] : When ` stride > size ` the pointer needs to be advanced by the array
46- _ stride_ instead of by the element _ size_ .
47-
48- [ layout of structs and tuples ] : ./structs-and-tuples.md
49-
50-
5129### Layout compatibility with packed SIMD vectors
5230
5331The [ layout of packed SIMD vector types] [ Vector ] [ ^ 2 ] requires the _ size_ and
@@ -63,29 +41,3 @@ type and the same number of elements as the vector.
6341## Layout of Rust slices
6442
6543The layout of a slice ` [T] ` of length ` N ` is the same as that of a ` [T; N] ` array.
66-
67- ## Unresolved questions
68-
69- ### Guaranteeing ` stride == size ` ?
70-
71- Currently, the [ layout of structs and tuples] does not guarantee that the
72- element _ size_ is a multiple of its _ alignment_ . For example, consider:
73-
74- ``` rust,ignore
75- struct A(u16, u8);
76- type B = [A; 4];
77- ```
78-
79- In the current Rust implementation, ` A ` has an alignment of ` 2 ` and a size of ` 4 ` ,
80- and ` B ` has a size of ` 16 ` , such that ` B ` contains four ` A ` s that are contiguously
81- laid in memory.
82-
83- However, a future Rust implementation could implement a layout optimization that
84- reduces the size of ` A ` to ` 3 ` . For the elements of ` B ` to be properly aligned,
85- ` B ` would need to choose a ` stride == 4 ` , resulting in a ` stride > size ` .
86-
87- Guaranteeing ` stride >= size ` is forward-compatible with such
88- layout-optimization proposals:
89-
90- * [ rust-lang/rfcs/1397: Spearate size and stride for types] ( https://github.com/rust-lang/rfcs/issues/1397 )
91- * [ rust-lang/rust/17027: Collapse trailing padding] ( https://github.com/rust-lang/rust/issues/17027 )
0 commit comments