From 61f6c72bf8ee057541b5554d0553f3c76b6cfdb7 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Wed, 13 Nov 2024 16:49:38 +0100 Subject: [PATCH] mod(layout/Stacks): add comptime checks for correct `Element` union typing --- src/layout/HStack.zig | 7 ++++++- src/layout/VStack.zig | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/layout/HStack.zig b/src/layout/HStack.zig index 6c47c67..bb5dd7e 100644 --- a/src/layout/HStack.zig +++ b/src/layout/HStack.zig @@ -19,8 +19,13 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t @compileError("Provided type `Element` for `Layout(comptime Event: type, comptime Element: type, comptime Renderer: type)` is not of type `union(enum)`."); } const Elements = std.ArrayList(Element); - // TODO: expect name of field to be corresponding to the type + if (!std.mem.eql(u8, @typeInfo(Element).Union.fields[0].name, "layout")) { + @compileError("Expected `layout: Layout` to be the first union element, but has name: " ++ @typeInfo(Element).Union.fields[0].name); + } const LayoutType = @typeInfo(Element).Union.fields[0].type; + if (!std.mem.eql(u8, @typeInfo(Element).Union.fields[1].name, "widget")) { + @compileError("Expected `widget: Widget` to be the first union element, but has name: " ++ @typeInfo(Element).Union.fields[1].name); + } const WidgetType = @typeInfo(Element).Union.fields[1].type; const Events = std.ArrayList(Event); return struct { diff --git a/src/layout/VStack.zig b/src/layout/VStack.zig index 273fa9a..7b89062 100644 --- a/src/layout/VStack.zig +++ b/src/layout/VStack.zig @@ -19,8 +19,13 @@ pub fn Layout(comptime Event: type, comptime Element: type, comptime Renderer: t @compileError("Provided type `Element` for `Layout(comptime Event: type, comptime Element: type, comptime Renderer: type)` is not of type `union(enum)`."); } const Elements = std.ArrayList(Element); - // TODO: expect name of field to be corresponding to the type + if (!std.mem.eql(u8, @typeInfo(Element).Union.fields[0].name, "layout")) { + @compileError("Expected `layout: Layout` to be the first union element, but has name: " ++ @typeInfo(Element).Union.fields[0].name); + } const LayoutType = @typeInfo(Element).Union.fields[0].type; + if (!std.mem.eql(u8, @typeInfo(Element).Union.fields[1].name, "widget")) { + @compileError("Expected `widget: Widget` to be the first union element, but has name: " ++ @typeInfo(Element).Union.fields[1].name); + } const WidgetType = @typeInfo(Element).Union.fields[1].type; const Events = std.ArrayList(Event); return struct {