aboutsummaryrefslogtreecommitdiff
path: root/lib/std/meta.zig
diff options
context:
space:
mode:
authorMeghan Denny <meghan@bun.sh>2024-06-02 16:29:10 -0700
committerGitHub <noreply@github.com>2024-06-02 23:29:10 +0000
commitdb75a8781beb2643a26de2353232de99037b8241 (patch)
tree91dc8106af7b50a7af5b5c7df3e3fa20f0cc7162 /lib/std/meta.zig
parentd74180c373274bc545992c2a96a8e7618f8bd52c (diff)
downloadzig-db75a8781beb2643a26de2353232de99037b8241.tar.gz
zig-db75a8781beb2643a26de2353232de99037b8241.zip
std.meta.hasUniqueRepresentation: better support packed structs
Diffstat (limited to 'lib/std/meta.zig')
-rw-r--r--lib/std/meta.zig15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 13c7731c55..0fff0aed3c 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.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,