From 572b7f731897a1a18f3896766548c6f1a7429198 Mon Sep 17 00:00:00 2001 From: Nir Lahad Date: Sun, 13 Jul 2025 03:17:19 +0300 Subject: [PATCH] Add packed struct encoding Implicitly encode packed struct as their backing integer. --- src/encoding.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/encoding.zig b/src/encoding.zig index aaef1a8..7fdafac 100644 --- a/src/encoding.zig +++ b/src/encoding.zig @@ -86,7 +86,10 @@ pub const Encoding = union(enum) { .@"opaque" => .void, .@"enum" => |m| .init(m.tag_type), .array => |arr| .{ .array = .{ .len = arr.len, .arr_type = arr.child } }, - .@"struct" => .{ .structure = .{ .struct_type = T, .show_type_spec = true } }, + .@"struct" => |m| switch (m.layout) { + .@"packed" => .init(m.backing_integer.?), + else => .{ .structure = .{ .struct_type = T, .show_type_spec = true } }, + }, .@"union" => .{ .@"union" = .{ .union_type = T, .show_type_spec = true, @@ -323,6 +326,13 @@ test "Enum(c_uint) to Encoding.uint encoding" { try encodingMatchesType(TestEnum, "I"); } +test "TestPackedStruct to Encoding.uint encoding" { + const TestPackedStruct = packed struct(u32) { + _: u32, + }; + try encodingMatchesType(TestPackedStruct, "I"); +} + test "*TestStruct to Encoding.pointer encoding" { const TestStruct = extern struct { float: f32,