Skip to content

Commit db75a87

Browse files
authored
std.meta.hasUniqueRepresentation: better support packed structs
1 parent d74180c commit db75a87

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/std/meta.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,8 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
12001200
.Array => |info| hasUniqueRepresentation(info.child),
12011201

12021202
.Struct => |info| {
1203+
if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T);
1204+
12031205
var sum_size = @as(usize, 0);
12041206

12051207
inline for (info.fields) |field| {
@@ -1245,6 +1247,19 @@ test hasUniqueRepresentation {
12451247

12461248
try testing.expect(!hasUniqueRepresentation(TestStruct5));
12471249

1250+
const TestStruct6 = packed struct(u8) {
1251+
@"0": bool,
1252+
@"1": bool,
1253+
@"2": bool,
1254+
@"3": bool,
1255+
@"4": bool,
1256+
@"5": bool,
1257+
@"6": bool,
1258+
@"7": bool,
1259+
};
1260+
1261+
try testing.expect(hasUniqueRepresentation(TestStruct6));
1262+
12481263
const TestUnion1 = packed union {
12491264
a: u32,
12501265
b: u16,

0 commit comments

Comments
 (0)