From f523438c4b1acf3682f369f8c7a71cd2b1de5150 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 23 Nov 2023 21:10:59 +0100 Subject: use our helper to check a typ has a specific declaration --- vtab.zig | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'vtab.zig') diff --git a/vtab.zig b/vtab.zig index 6fbd865..7c2dafe 100644 --- a/vtab.zig +++ b/vtab.zig @@ -16,6 +16,15 @@ const helpers = @import("helpers.zig"); const logger = std.log.scoped(.vtab); +fn hasDecls(comptime T: type, comptime names: anytype) bool { + inline for (names) |name| { + if (!@hasDecl(T, name)) { + return false; + } + } + return true; +} + /// ModuleContext contains state that is needed by all implementations of virtual tables. /// /// Currently there's only an allocator. @@ -301,7 +310,7 @@ fn validateCursorType(comptime Table: type) void { // Validate the `init` function { - if (!meta.trait.hasDecls(Cursor, .{"InitError"})) { + if (!comptime hasDecls(Cursor, .{"InitError"})) { @compileError("the Cursor type must declare a InitError error set for the init function"); } @@ -340,7 +349,7 @@ fn validateCursorType(comptime Table: type) void { // Validate the `next` function { - if (!meta.trait.hasDecls(Cursor, .{"NextError"})) { + if (!comptime hasDecls(Cursor, .{"NextError"})) { @compileError("the Cursor type must declare a NextError error set for the next function"); } @@ -362,7 +371,7 @@ fn validateCursorType(comptime Table: type) void { // Validate the `hasNext` function { - if (!meta.trait.hasDecls(Cursor, .{"HasNextError"})) { + if (!comptime hasDecls(Cursor, .{"HasNextError"})) { @compileError("the Cursor type must declare a HasNextError error set for the hasNext function"); } @@ -384,7 +393,7 @@ fn validateCursorType(comptime Table: type) void { // Validate the `filter` function { - if (!meta.trait.hasDecls(Cursor, .{"FilterError"})) { + if (!comptime hasDecls(Cursor, .{"FilterError"})) { @compileError("the Cursor type must declare a FilterError error set for the filter function"); } @@ -408,10 +417,10 @@ fn validateCursorType(comptime Table: type) void { // Validate the `column` function { - if (!meta.trait.hasDecls(Cursor, .{"ColumnError"})) { + if (!comptime hasDecls(Cursor, .{"ColumnError"})) { @compileError("the Cursor type must declare a ColumnError error set for the column function"); } - if (!meta.trait.hasDecls(Cursor, .{"Column"})) { + if (!comptime hasDecls(Cursor, .{"Column"})) { @compileError("the Cursor type must declare a Column type for the return type of the column function"); } @@ -434,7 +443,7 @@ fn validateCursorType(comptime Table: type) void { // Validate the `rowId` function { - if (!meta.trait.hasDecls(Cursor, .{"RowIDError"})) { + if (!comptime hasDecls(Cursor, .{"RowIDError"})) { @compileError("the Cursor type must declare a RowIDError error set for the rowId function"); } @@ -459,7 +468,7 @@ fn validateCursorType(comptime Table: type) void { fn validateTableType(comptime Table: type) void { // Validate the `init` function { - if (!meta.trait.hasDecls(Table, .{"InitError"})) { + if (!comptime hasDecls(Table, .{"InitError"})) { @compileError("the Table type must declare a InitError error set for the init function"); } @@ -501,7 +510,7 @@ fn validateTableType(comptime Table: type) void { // Validate the `buildBestIndex` function { - if (!meta.trait.hasDecls(Table, .{"BuildBestIndexError"})) { + if (!comptime hasDecls(Table, .{"BuildBestIndexError"})) { @compileError("the Cursor type must declare a BuildBestIndexError error set for the buildBestIndex function"); } @@ -522,7 +531,7 @@ fn validateTableType(comptime Table: type) void { if (info.return_type.? != Table.BuildBestIndexError!void) @compileError(error_message); } - if (!meta.trait.hasDecls(Table, .{"Cursor"})) { + if (!comptime hasDecls(Table, .{"Cursor"})) { @compileError("the Table type must declare a Cursor type"); } } -- cgit v1.2.3