-
-
Save tadeokondrak/ec5bc3e4ea722c8c93b8a6df96b267b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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