Skip to content

Commit e88dc3d

Browse files
committed
Simplify zero-sized fields of unions
1 parent e5af280 commit e88dc3d

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

Diff for: reference/src/layout/unions.md

+5-21
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,12 @@ assert_eq!(size_of::<U>(), 2);
6363
6464
#### Zero-sized fields
6565

66-
If a `#[repr(C)]` union contains a field of zero-size, that field does not
67-
occupy space in Rust unions. For example:
66+
`repr(C)` union fields of zero-size are handled in the same way as in struct
67+
fields, matching the behavior of GCC and Clang for unions in C when zero-sized
68+
types are allowed via their language extensions.
6869

69-
```rust
70-
# use std::mem::{size_of, align_of};
71-
#[repr(C)]
72-
union U {
73-
x: u8,
74-
y: (),
75-
}
76-
# fn main() {
77-
assert_eq!(size_of::<U>(), 1);
78-
# }
79-
```
80-
81-
The field does, however, participate in the layout computation of the union, and
82-
can raise its alignment requirement, which in turn can introduce trailing
83-
padding. For example:
70+
That is, these fields occupy zero-size and participate in the layout computation
71+
of the union as usual:
8472

8573
```rust
8674
# use std::mem::{size_of, align_of};
@@ -97,10 +85,6 @@ assert_eq!(size_of::<U>(), 2);
9785
# }
9886
```
9987

100-
This handling of zero-sized types is equivalent to the handling of zero-sized
101-
types in struct fields, and matches the behavior of GCC and Clang for unions in
102-
C when zero-sized types are allowed via their language extensions.
103-
10488
**C++ compatibility hazard**: C++ does, in general, give a size of 1 to empty
10589
structs. If an empty struct in C++ is used as an union field, a "naive"
10690
translation of that code into Rust will not produce a compatible result.

0 commit comments

Comments
 (0)