authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-02 16:29:10-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-06-02 23:29:10+00:00
logdb75a8781beb2643a26de2353232de99037b8241
tree91dc8106af7b50a7af5b5c7df3e3fa20f0cc7162
parentd74180c373274bc545992c2a96a8e7618f8bd52c
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.meta.hasUniqueRepresentation: better support packed structs


1 files changed, 15 insertions(+), 0 deletions(-)

lib/std/meta.zig+15
...@@ -1200,6 +1200,8 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {...@@ -1200,6 +1200,8 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
1200 .Array => |info| hasUniqueRepresentation(info.child),1200 .Array => |info| hasUniqueRepresentation(info.child),
12011201
1202 .Struct => |info| {1202 .Struct => |info| {
1203 if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T);
1204
1203 var sum_size = @as(usize, 0);1205 var sum_size = @as(usize, 0);
12041206
1205 inline for (info.fields) |field| {1207 inline for (info.fields) |field| {
...@@ -1245,6 +1247,19 @@ test hasUniqueRepresentation {...@@ -1245,6 +1247,19 @@ test hasUniqueRepresentation {
12451247
1246 try testing.expect(!hasUniqueRepresentation(TestStruct5));1248 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
1248 const TestUnion1 = packed union {1263 const TestUnion1 = packed union {
1249 a: u32,1264 a: u32,
1250 b: u16,1265 b: u16,