Skip to content

Commit 3056194

Browse files
committed
Add examples
1 parent 003eee9 commit 3056194

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

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

+30-7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ 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+
<<<<<<< HEAD
125126
#### Zero-sized structs
126127

127128
For `repr(Rust)`, `repr(packed(N))`, `repr(align(N))`, and `repr(C)`
@@ -141,6 +142,28 @@ struct Zst2(Zst1, Zst0);
141142
# }
142143
```
143144

145+
=======
146+
#### Default layout of structs with a single non-zero-sized field
147+
148+
The default layout of structs with a single non-zero-sized field is the same as
149+
the layout of that field if the alignment requirement of all other fields is 1.
150+
151+
For example, the layout of:
152+
153+
```rust
154+
struct SomeStruct(i32, ());
155+
```
156+
157+
is the same as the layout of `i32`, but the layout of:
158+
159+
```rust
160+
#[repr(align(16))] struct Zst;
161+
struct SomeOtherStruct(i32, Zst);
162+
```
163+
164+
is **unspecified**, since there is a zero-sized field in `SomeOtherStruct` with
165+
alignment greater than 1.
166+
144167
#### Unresolved questions
145168

146169
During the course of the discussion in [#11] and [#12], various
@@ -150,14 +173,14 @@ issue has been opened for further discussion on the repository. This
150173
section documents the questions and gives a few light details, but the
151174
reader is referred to the issues for further discussion.
152175

153-
**Single-field structs ([#34]).** If you have a struct with single field
154-
(`struct Foo { x: T }`), should we guarantee that the memory layout of
155-
`Foo` is identical to the memory layout of `T` (note that ABI details
156-
around function calls may still draw a distinction, which is why
157-
`#[repr(transparent)]` is needed). What about zero-sized types like
158-
`PhantomData`?
176+
**Zero-sized structs ([#37]).** If you have a struct which --
177+
transitively -- contains no data of non-zero size, then the size of
178+
that struct will be zero as well. These zero-sized structs appear
179+
frequently as exceptions in other layout considerations (e.g.,
180+
single-field structs). An example of such a struct is
181+
`std::marker::PhantomData`.
159182

160-
[#34]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/34
183+
[#37]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/37
161184

162185
**Homogeneous structs ([#36]).** If you have homogeneous structs, where all
163186
the `N` fields are of a single type `T`, can we guarantee a mapping to

0 commit comments

Comments
 (0)