File tree 2 files changed +35
-1
lines changed
2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -15,7 +15,8 @@ currently different for each architecture.
15
15
## Vector types
16
16
17
17
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:
19
20
20
21
``` rust
21
22
#[repr(simd)]
You can’t perform that action at this time.
0 commit comments