Skip to content

Commit dd98383

Browse files
author
Stephen Gutekanst
committed
make note of explicit backing integers for packed structs
Signed-off-by: Stephen Gutekanst <[email protected]>
1 parent 5a38c23 commit dd98383

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

content/2022/packed-structs-in-zig.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ Similarly we could check the `@bitSizeOf` both types if we like.
126126

127127
Note that [`@sizeOf`](https://ziglang.org/documentation/master/#sizeOf) may include the size of padding for more complex types, while [`@bitSizeOf`](https://ziglang.org/documentation/master/#bitSizeOf) returns the number of bits it takes to store `T` in memory _if the type were a field in a packed struct/union_. For flag sets like this, it doesn't matter and either will do. For more complex types, be sure to recall this.
128128

129+
## Explicit backing integers for packed structs
130+
131+
It's worth noting that in Zig 0.11 (shipping in Nov), the new self-hosted compiler has support for [explicit backing integers for packed structs](https://github.com/ziglang/zig/pull/12379) which will simplify this even further.
132+
133+
Instead of manually adding padding to make up 32 bits, one could simply write `packed struct(u32)`:
134+
135+
```zig
136+
pub const ColorWriteMaskFlags = packed struct(u32) {
137+
red: bool = false,
138+
green: bool = false,
139+
blue: bool = false,
140+
alpha: bool = false,
141+
}
142+
```
129143

130144
## Thanks for reading
131145

0 commit comments

Comments
 (0)