Skip to content

Commit 949aebb

Browse files
committed
Guarantee layout of homogeneous tuples
Closes rust-lang#36 .
1 parent 57f2d0a commit 949aebb

File tree

2 files changed

+6
-35
lines changed

2 files changed

+6
-35
lines changed

reference/src/layout/packed-simd-vectors.md

+2-28
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ That is, two distinct `repr(simd)` vector types that have the same `T` and the
4545
same `N` have the same size and alignment.
4646

4747
Vector elements are laid out in source field order, enabling random access to
48-
vector elements by reinterpreting the vector as an array:
48+
vector elements by reinterpreting the vector as an array or as an homogeneous tuple:
4949

5050
```rust,ignore
5151
union U {
5252
vec: Vector<T, N>,
53-
arr: [T; N]
53+
arr: [T; N],
5454
}
5555
5656
assert_eq!(size_of::<Vector<T, N>>(), size_of::<[T; N]>());
@@ -66,32 +66,6 @@ unsafe {
6666
```
6767

6868
### Unresolved questions
69-
70-
* **Blocked**: Should the layout of packed SIMD vectors be the same as that of
71-
homogeneous tuples ? Such that:
72-
73-
```rust,ignore
74-
union U {
75-
vec: Vector<T, N>,
76-
tup: (T_0, ..., T_(N-1)),
77-
}
78-
79-
assert_eq!(size_of::<Vector<T, N>>(), size_of::<(T_0, ..., T_(N-1))>());
80-
assert!(align_of::<Vector<T, N>>() >= align_of::<(T_0, ..., T_(N-1))>());
81-
82-
unsafe {
83-
let u = U { vec: Vector(t_0, ..., t_(N - 1)) };
84-
85-
assert_eq!(u.vec.0, u.tup.0);
86-
// ...
87-
assert_eq!(u.vec.(N - 1), u.tup.(N - 1));
88-
}
89-
```
90-
91-
This is blocked on the resolution of issue [#36] about the layout of
92-
homogeneous structs and tuples.
93-
94-
[#36]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/36
9569

9670
* `MaybeUninit<T>` does not have the same `repr` as `T`, so
9771
`MaybeUninit<Vector<T, N>>` are not `repr(simd)`, which has performance

reference/src/layout/structs-and-tuples.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ guaranteed layout from a tuple, you are generally advised to create a
2424
named struct with a `#[repr(C)]` annotation (see [the section on
2525
structs for more details](#structs)).
2626

27+
Homogeneous tuples have the same layout as an array `[T; N]` where `N` is the
28+
number of fields in the tuple and the tuple field `i` is located at the same
29+
offset as the `i`-th array element.
30+
2731
Note that the final element of a tuple (`Pn`) is marked as `?Sized` to
2832
permit unsized tuple coercion -- this is implemented on nightly but is
2933
currently unstable ([tracking issue][#42877]). In the future, we may
@@ -207,13 +211,6 @@ issue has been opened for further discussion on the repository. This
207211
section documents the questions and gives a few light details, but the
208212
reader is referred to the issues for further discussion.
209213

210-
**Homogeneous structs ([#36]).** If you have homogeneous structs, where all
211-
the `N` fields are of a single type `T`, can we guarantee a mapping to
212-
the memory layout of `[T; N]`? How do we map between the field names
213-
and the indices? What about zero-sized types?
214-
215-
[#36]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/36
216-
217214
**Deterministic layout ([#35]).** Can we say that layout is some deterministic
218215
function of a certain, fixed set of inputs? This would allow you to be
219216
sure that if you do not alter those inputs, your struct layout would

0 commit comments

Comments
 (0)