Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guarantee that some structs are zero sized #163

Merged
merged 5 commits into from
Jul 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions reference/src/layout/structs-and-tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ compiler will not reorder it, to allow for the possibility of
unsizing. E.g., `struct Foo { x: u16, y: u32 }` and `struct Foo<T> {
x: u16, y: T }` where `T = u32` are not guaranteed to be identical.

#### Zero-sized structs

For `repr(Rust)`, `repr(packed(N))`, `repr(align(N))`, and `repr(C)`
structs: if all fields of a struct have size 0, then the struct has size 0.

For example, all these types are zero-sized:

```rust
# use std::mem::size_of;
#[repr(align(32))] struct Zst0;
#[repr(C)] struct Zst1(Zst0);
struct Zst2(Zst1, Zst0);
# fn main() {
# assert_eq!(size_of::<Zst0>(), 0);
# assert_eq!(size_of::<Zst1>(), 0);
# assert_eq!(size_of::<Zst2>(), 0);
# }
```
gnzlbg marked this conversation as resolved.
Show resolved Hide resolved

#### Unresolved questions

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

**Zero-sized structs ([#37]).** If you have a struct which --
transitively -- contains no data of non-zero size, then the size of
that struct will be zero as well. These zero-sized structs appear
frequently as exceptions in other layout considerations (e.g.,
single-field structs). An example of such a struct is
`std::marker::PhantomData`.

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

**Single-field structs ([#34]).** If you have a struct with single field
(`struct Foo { x: T }`), should we guarantee that the memory layout of
`Foo` is identical to the memory layout of `T` (note that ABI details
Expand Down