Skip to content

Instantly share code, notes, and snippets.

@tadeokondrak
Created March 17, 2021 04:23
Show Gist options
  • Save tadeokondrak/ec5bc3e4ea722c8c93b8a6df96b267b5 to your computer and use it in GitHub Desktop.
Save tadeokondrak/ec5bc3e4ea722c8c93b8a6df96b267b5 to your computer and use it in GitHub Desktop.
const std = @import("std");
const VoidList = std.SinglyLinkedList(void);
const Command = struct {
tag: Tag,
node: VoidList.Node = .{ .data = {} },
const Tag = enum {
fill,
text,
};
const Fill = struct {
base: Command = .{ .tag = .fill },
color: u32,
};
const Text = struct {
base: Command = .{ .tag = .text },
text: []const u8,
};
};
pub fn main() void {
var list = VoidList{};
var fill = Command.Fill{ .color = 0 };
var text = Command.Text{ .text = "hello" };
list.prepend(&fill.base.node);
list.prepend(&text.base.node);
{
var it = list.first;
while (it) |node| : (it = node.next) {
const command = @fieldParentPtr(Command, "node", node);
switch (command.tag) {
.fill => std.debug.print("fill: {}\n", .{@fieldParentPtr(Command.Fill, "base", command).color}),
.text => std.debug.print("text: {s}\n", .{@fieldParentPtr(Command.Text, "base", command).text}),
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment