Added a thing

This commit is contained in:
2026-10-10 23:23:33 -05:00
parent d029d3c3cd
commit 3da288bffe
3 changed files with 83 additions and 5 deletions
+72
View File
@@ -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`.
+5
View File
@@ -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());
}
+6 -5
View File
@@ -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");
}
}