Commit db75a8781b

Meghan Denny <meghan@bun.sh>
2024-06-03 01:29:10
std.meta.hasUniqueRepresentation: better support packed structs
1 parent d74180c
Changed files (1)
lib
lib/std/meta.zig
@@ -1200,6 +1200,8 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
         .Array => |info| hasUniqueRepresentation(info.child),
 
         .Struct => |info| {
+            if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T);
+
             var sum_size = @as(usize, 0);
 
             inline for (info.fields) |field| {
@@ -1245,6 +1247,19 @@ test hasUniqueRepresentation {
 
     try testing.expect(!hasUniqueRepresentation(TestStruct5));
 
+    const TestStruct6 = packed struct(u8) {
+        @"0": bool,
+        @"1": bool,
+        @"2": bool,
+        @"3": bool,
+        @"4": bool,
+        @"5": bool,
+        @"6": bool,
+        @"7": bool,
+    };
+
+    try testing.expect(hasUniqueRepresentation(TestStruct6));
+
     const TestUnion1 = packed union {
         a: u32,
         b: u16,