File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Expand file tree Collapse file tree 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 @@ -22,7 +22,8 @@ of type `T` where `N` is a power-of-two:
22
22
struct Vector <T , N >(T_0 , ... , T_ (N - 1 ));
23
23
```
24
24
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_ .
26
27
27
28
The size of ` Vector ` is ` N * size_of::<T>() ` and its alignment is an
28
29
_ implementation-defined_ function of ` T ` and ` N ` greater than or equal to
You can’t perform that action at this time.
0 commit comments