From 06e0b857ad20d28cd343d8ca012701e14f3b2818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gr=C3=A9goire?= Date: Fri, 26 Jun 2026 17:46:05 -0400 Subject: [PATCH] rtl: add assert_interface --- src/rtl/root.zig | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/rtl/root.zig b/src/rtl/root.zig index 6048d97..9bdc192 100644 --- a/src/rtl/root.zig +++ b/src/rtl/root.zig @@ -14,6 +14,63 @@ pub const AVLTree = @import("avl.zig").AVLTree; pub const BitMap = @import("bitmap.zig").BitMap; pub const AtomicBitMap = @import("bitmap.zig").AtomicBitMap; +/// Asserts that a given type `T` matches the schema declared by `I`. +/// This includes public methods and fields. +pub fn assert_interface(T: type, I: type) void { + const tinfo = @typeInfo(I); + + inline for (tinfo.@"struct".fields) |f| { + if (!@hasField(T, f.name)) { + @compileError(std.fmt.comptimePrint("Expected field '{s}' of type '{s}' in type '{s}' required by '{s}'", .{ f.name, @typeName(f.type), @typeName(T), @typeName(I) })); + } else { + if (@FieldType(T, f.name) != f.type) { + @compileError(std.fmt.comptimePrint("Expected field '{s}' of type '{s}' in type '{s}' required by '{s}', got '{s}'", .{ f.name, @typeName(f.type), @typeName(T), @typeName(I), @typeName(@FieldType(T, f.name)) })); + } + } + } + + inline for (tinfo.@"struct".decls) |decl| { + const member = @field(I, decl.name); + + if (@typeInfo(@TypeOf(member)) == .@"fn") { + if (!@hasDecl(T, decl.name)) { + @compileError(std.fmt.comptimePrint("Expected method '{s}' in type '{s}' required by '{s}'", .{ decl.name, @typeName(T), @typeName(I) })); + } + + const impl_member = @field(T, decl.name); + const IfaceFnType = @TypeOf(member); + const ImplFnType = @TypeOf(impl_member); + + if (IfaceFnType != ImplFnType) { + const iface_fn = @typeInfo(IfaceFnType).@"fn"; + const impl_fn = @typeInfo(ImplFnType).@"fn"; + + if (iface_fn.params.len != impl_fn.params.len) { + @compileError(std.fmt.comptimePrint("Parameter count mismatch in method '{s}' in type '{s}' required by '{s}'", .{ decl.name, @typeName(T), @typeName(I) })); + } + if (iface_fn.return_type != impl_fn.return_type) { + @compileError(std.fmt.comptimePrint("Return type mismatch in method '{s}' in type '{s}' required by '{s}'", .{ decl.name, @typeName(T), @typeName(I) })); + } + + for (0..iface_fn.params.len) |i| { + if (iface_fn.params[i].type != impl_fn.params[i].type) { + @compileError(std.fmt.comptimePrint("Parameter type mismatch in method '{s}' in type '{s}' required by '{s}'", .{ decl.name, @typeName(T), @typeName(I) })); + } + } + } + } else if (@TypeOf(member) == type) { + if (!@hasDecl(T, decl.name)) { + @compileError(std.fmt.comptimePrint("Expected type declaration '{s}' in type '{s}' required by '{s}'", .{ decl.name, @typeName(T), @typeName(I) })); + } + + const impl_member = @field(T, decl.name); + if (@TypeOf(impl_member) != type) { + @compileError(std.fmt.comptimePrint("Declaration '{s}' in type '{s}' is required to be a type by '{s}', but got '{s}'", .{ decl.name, @typeName(T), @typeName(I), @typeName(@TypeOf(impl_member)) })); + } + } + } +} + test { std.testing.refAllDecls(@This()); }