Skip to content

Commit ea3dbdf

Browse files
authored
Merge pull request #163 from gnzlbg/zst_tuples
Guarantee that some structs are zero sized
2 parents d86d39c + ac0cc9c commit ea3dbdf

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

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

+19-9
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ 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+
#### Zero-sized structs
126+
127+
For `repr(Rust)`, `repr(packed(N))`, `repr(align(N))`, and `repr(C)`
128+
structs: if all fields of a struct have size 0, then the struct has size 0.
129+
130+
For example, all these types are zero-sized:
131+
132+
```rust
133+
# use std::mem::size_of;
134+
#[repr(align(32))] struct Zst0;
135+
#[repr(C)] struct Zst1(Zst0);
136+
struct Zst2(Zst1, Zst0);
137+
# fn main() {
138+
# assert_eq!(size_of::<Zst0>(), 0);
139+
# assert_eq!(size_of::<Zst1>(), 0);
140+
# assert_eq!(size_of::<Zst2>(), 0);
141+
# }
142+
```
143+
125144
#### Unresolved questions
126145

127146
During the course of the discussion in [#11] and [#12], various
@@ -131,15 +150,6 @@ issue has been opened for further discussion on the repository. This
131150
section documents the questions and gives a few light details, but the
132151
reader is referred to the issues for further discussion.
133152

134-
**Zero-sized structs ([#37]).** If you have a struct which --
135-
transitively -- contains no data of non-zero size, then the size of
136-
that struct will be zero as well. These zero-sized structs appear
137-
frequently as exceptions in other layout considerations (e.g.,
138-
single-field structs). An example of such a struct is
139-
`std::marker::PhantomData`.
140-
141-
[#37]: https://github.com/rust-rfcs/unsafe-code-guidelines/issues/37
142-
143153
**Single-field structs ([#34]).** If you have a struct with single field
144154
(`struct Foo { x: T }`), should we guarantee that the memory layout of
145155
`Foo` is identical to the memory layout of `T` (note that ABI details

0 commit comments

Comments
 (0)