Skip to content

Commit 58558fe

Browse files
committed
Add examples
1 parent 97cbda4 commit 58558fe

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ compiler will not reorder it, to allow for the possibility of
122122
unsizing. E.g., `struct Foo { x: u16, y: u32 }` and `struct Foo<T> {
123123
x: u16, y: T }` where `T = u32` are not guaranteed to be identical.
124124

125+
#### Default layout of structs with a single non-zero-sized field
126+
127+
The default layout of structs with a single non-zero-sized field is the same as
128+
the layout of that field if the alignment requirement of all other fields is 1.
129+
130+
For example, the layout of:
131+
132+
```rust
133+
struct SomeStruct(i32, ());
134+
```
135+
136+
is the same as the layout of `i32`, but the layout of:
137+
138+
```rust
139+
#[repr(align(16))] struct Zst;
140+
struct SomeOtherStruct(i32, Zst);
141+
```
142+
143+
is **unspecified**, since there is a zero-sized field in `SomeOtherStruct` with
144+
alignment greater than 1.
145+
125146
#### Unresolved questions
126147

127148
During the course of the discussion in [#11] and [#12], various
@@ -140,15 +161,6 @@ single-field structs). An example of such a struct is
140161

141162
[#37]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/37
142163

143-
**Single-field structs ([#34]).** If you have a struct with single field
144-
(`struct Foo { x: T }`), should we guarantee that the memory layout of
145-
`Foo` is identical to the memory layout of `T` (note that ABI details
146-
around function calls may still draw a distinction, which is why
147-
`#[repr(transparent)]` is needed). What about zero-sized types like
148-
`PhantomData`?
149-
150-
[#34]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/34
151-
152164
**Homogeneous structs ([#36]).** If you have homogeneous structs, where all
153165
the `N` fields are of a single type `T`, can we guarantee a mapping to
154166
the memory layout of `[T; N]`? How do we map between the field names

0 commit comments

Comments
 (0)