From 3da288bffe6042fdd6c9cf103dda3fc943ce80f6 Mon Sep 17 00:00:00 2001 From: Paul Montag Date: Sat, 10 Oct 2026 23:23:33 -0500 Subject: [PATCH] Added a thing --- src/packed_struct.zig | 72 ++++++++++++++++++++++++++++++++++++ src/root.zig | 5 +++ src/switching_on_strings.zig | 11 +++--- 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 src/packed_struct.zig diff --git a/src/packed_struct.zig b/src/packed_struct.zig new file mode 100644 index 0000000..dabd603 --- /dev/null +++ b/src/packed_struct.zig @@ -0,0 +1,72 @@ +const std = @import("std"); + +// LLVM doesn't give a shit how you laid things out in your source code +// because of this you can't assume the first field in your struct is the +// first one in memory, making bitcasting between types hard. +// +// Enter the packed struct. According to the documentation: +// > Packed structs have well-defined memory layout - exactly the same ABI +// > as their backing integer. + +/// Animal is the lower level type. +const Animal = packed struct { + name: usize, + age: u16, + + fn Name(self: @This()) []const u8 { + const str: *[]const u8 = @ptrFromInt(self.name); + return str.*; + } +}; + +/// Dog has the same initial fields as an Animal, That means the initial +/// byes should be able to be turned into the lower level type if we want +/// to. +const Dog = packed struct { + name: usize, + age: u16, + breed: Breed, + + /// Breed specifies the different kinds of dogs + const Breed = enum(u4) { + FrenchBulldog, + LabradorRetriever, + GoldenRetriever, + GermanShepherd, + Poodle, + }; +}; + +test "packed struct" { + const DOG_NAME: []const u8 = "Teddy"; + + var dog: Dog = .{ + .name = @intFromPtr(&DOG_NAME), + .age = 20, + .breed = .FrenchBulldog, + }; + + const animal: *Animal = @ptrCast(&dog); + try std.testing.expectEqualStrings(animal.Name(), DOG_NAME); +} + +// Honestly this value is a bit contrived since you wouldn't create some kind +// of downgrading interface this way. A packed struct does have it's usefulness though: +// +// - Bit-level layouts: When fields need specific bit widths, such as hardware +// registers or protocol headers. +// - Memory efficiency: When storing many values with small fields, such as flags or +// enums, where reducing memory usage matters. +// - Binary formats: When matching an external specification that defines an exact +// bit-level data layout. +// - C interoperability: When interfacing with C APIs or hardware that require +// specific memory layouts, provided the layout requirements are compatible with +// Zig's guarantees. +// - Hardware access: When manipulating memory-mapped registers or device fields at +// specific bit positions. +// +// LLVM not giving a shit about your order has it's reasons. It can make things more +// efficient and whatnot, so don't use this unless you really care about the memory +// layout of the struct. +// +// Also, zig does offer some helpful things like the `@fieldParentPtr`. diff --git a/src/root.zig b/src/root.zig index f8ef2de..a2a4740 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,4 +1,9 @@ test "Everything" { + _ = @import("interface.zig"); _ = @import("intrusive_interface.zig"); + _ = @import("anytype_polymorphism.zig"); + _ = @import("switching_on_strings.zig"); + _ = @import("packed_struct.zig"); + @import("std").testing.refAllDecls(@This()); } diff --git a/src/switching_on_strings.zig b/src/switching_on_strings.zig index 072e419..9409186 100644 --- a/src/switching_on_strings.zig +++ b/src/switching_on_strings.zig @@ -30,9 +30,10 @@ test "Switch StaticStringMap" { // Now when we actually do the switch, we parse it into an enum, and then // can do an exhaustive switch on that. Since we could be passed something // that doesn't exist, we have to also support null. - std.testing.expectEqual(switch (Number.parse("two")) { - null => "", - .One, .Two => "fizz", - .Three, .Four => "buzz", - }, "fizz"); + if (Number.parse("two")) |number| { + try std.testing.expectEqual(switch (number) { + .One, .Two => "fizz", + .Three, .Four => "buzz", + }, "fizz"); + } }