From d979d2acadf479f58152a456c2573044f7626cc7 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 31 May 2024 17:07:48 -0700 Subject: [PATCH 1/3] std.meta.hasUniqueRepresentation: better support packed structs --- lib/std/meta.zig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 13c7731c5514..24d6e61a80dc 100644 --- a/lib/std/meta.zig +++ b/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 == .@"packed" and @sizeOf(T) == @bitSizeOf(T) / 8) return true; + 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, From b2d41696dd10eca92f3694e485c6fd24809e6329 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 31 May 2024 17:13:30 -0700 Subject: [PATCH 2/3] fix web editor typo --- lib/std/meta.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 24d6e61a80dc..e1425a73c5c5 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1200,7 +1200,7 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool { .Array => |info| hasUniqueRepresentation(info.child), .Struct => |info| { - if (info == .@"packed" and @sizeOf(T) == @bitSizeOf(T) / 8) return true; + if (info.layout == .@"packed" and @sizeOf(T) == @bitSizeOf(T) / 8) return true; var sum_size = @as(usize, 0); From 78f2408e51ca2bf22e470fecff9822374f26621c Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 2 Jun 2024 12:18:05 -0700 Subject: [PATCH 3/3] more robust bitsize comparison check Co-authored-by: Matthew Lugg --- lib/std/meta.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index e1425a73c5c5..0fff0aed3c65 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1200,7 +1200,7 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool { .Array => |info| hasUniqueRepresentation(info.child), .Struct => |info| { - if (info.layout == .@"packed" and @sizeOf(T) == @bitSizeOf(T) / 8) return true; + if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T); var sum_size = @as(usize, 0);