@@ -63,24 +63,12 @@ assert_eq!(size_of::<U>(), 2);
63
63
64
64
#### Zero-sized fields
65
65
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.
68
69
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:
84
72
85
73
``` rust
86
74
# use std :: mem :: {size_of, align_of};
@@ -97,10 +85,6 @@ assert_eq!(size_of::<U>(), 2);
97
85
# }
98
86
```
99
87
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
-
104
88
** C++ compatibility hazard** : C++ does, in general, give a size of 1 to empty
105
89
structs. If an empty struct in C++ is used as an union field, a "naive"
106
90
translation of that code into Rust will not produce a compatible result.
0 commit comments