diff options
Diffstat (limited to 'vtab.zig')
| -rw-r--r-- | vtab.zig | 29 |
1 files changed, 19 insertions, 10 deletions
| @@ -16,6 +16,15 @@ const helpers = @import("helpers.zig"); | |||
| 16 | 16 | ||
| 17 | const logger = std.log.scoped(.vtab); | 17 | const logger = std.log.scoped(.vtab); |
| 18 | 18 | ||
| 19 | fn hasDecls(comptime T: type, comptime names: anytype) bool { | ||
| 20 | inline for (names) |name| { | ||
| 21 | if (!@hasDecl(T, name)) { | ||
| 22 | return false; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | return true; | ||
| 26 | } | ||
| 27 | |||
| 19 | /// ModuleContext contains state that is needed by all implementations of virtual tables. | 28 | /// ModuleContext contains state that is needed by all implementations of virtual tables. |
| 20 | /// | 29 | /// |
| 21 | /// Currently there's only an allocator. | 30 | /// Currently there's only an allocator. |
| @@ -301,7 +310,7 @@ fn validateCursorType(comptime Table: type) void { | |||
| 301 | 310 | ||
| 302 | // Validate the `init` function | 311 | // Validate the `init` function |
| 303 | { | 312 | { |
| 304 | if (!meta.trait.hasDecls(Cursor, .{"InitError"})) { | 313 | if (!comptime hasDecls(Cursor, .{"InitError"})) { |
| 305 | @compileError("the Cursor type must declare a InitError error set for the init function"); | 314 | @compileError("the Cursor type must declare a InitError error set for the init function"); |
| 306 | } | 315 | } |
| 307 | 316 | ||
| @@ -340,7 +349,7 @@ fn validateCursorType(comptime Table: type) void { | |||
| 340 | 349 | ||
| 341 | // Validate the `next` function | 350 | // Validate the `next` function |
| 342 | { | 351 | { |
| 343 | if (!meta.trait.hasDecls(Cursor, .{"NextError"})) { | 352 | if (!comptime hasDecls(Cursor, .{"NextError"})) { |
| 344 | @compileError("the Cursor type must declare a NextError error set for the next function"); | 353 | @compileError("the Cursor type must declare a NextError error set for the next function"); |
| 345 | } | 354 | } |
| 346 | 355 | ||
| @@ -362,7 +371,7 @@ fn validateCursorType(comptime Table: type) void { | |||
| 362 | 371 | ||
| 363 | // Validate the `hasNext` function | 372 | // Validate the `hasNext` function |
| 364 | { | 373 | { |
| 365 | if (!meta.trait.hasDecls(Cursor, .{"HasNextError"})) { | 374 | if (!comptime hasDecls(Cursor, .{"HasNextError"})) { |
| 366 | @compileError("the Cursor type must declare a HasNextError error set for the hasNext function"); | 375 | @compileError("the Cursor type must declare a HasNextError error set for the hasNext function"); |
| 367 | } | 376 | } |
| 368 | 377 | ||
| @@ -384,7 +393,7 @@ fn validateCursorType(comptime Table: type) void { | |||
| 384 | 393 | ||
| 385 | // Validate the `filter` function | 394 | // Validate the `filter` function |
| 386 | { | 395 | { |
| 387 | if (!meta.trait.hasDecls(Cursor, .{"FilterError"})) { | 396 | if (!comptime hasDecls(Cursor, .{"FilterError"})) { |
| 388 | @compileError("the Cursor type must declare a FilterError error set for the filter function"); | 397 | @compileError("the Cursor type must declare a FilterError error set for the filter function"); |
| 389 | } | 398 | } |
| 390 | 399 | ||
| @@ -408,10 +417,10 @@ fn validateCursorType(comptime Table: type) void { | |||
| 408 | 417 | ||
| 409 | // Validate the `column` function | 418 | // Validate the `column` function |
| 410 | { | 419 | { |
| 411 | if (!meta.trait.hasDecls(Cursor, .{"ColumnError"})) { | 420 | if (!comptime hasDecls(Cursor, .{"ColumnError"})) { |
| 412 | @compileError("the Cursor type must declare a ColumnError error set for the column function"); | 421 | @compileError("the Cursor type must declare a ColumnError error set for the column function"); |
| 413 | } | 422 | } |
| 414 | if (!meta.trait.hasDecls(Cursor, .{"Column"})) { | 423 | if (!comptime hasDecls(Cursor, .{"Column"})) { |
| 415 | @compileError("the Cursor type must declare a Column type for the return type of the column function"); | 424 | @compileError("the Cursor type must declare a Column type for the return type of the column function"); |
| 416 | } | 425 | } |
| 417 | 426 | ||
| @@ -434,7 +443,7 @@ fn validateCursorType(comptime Table: type) void { | |||
| 434 | 443 | ||
| 435 | // Validate the `rowId` function | 444 | // Validate the `rowId` function |
| 436 | { | 445 | { |
| 437 | if (!meta.trait.hasDecls(Cursor, .{"RowIDError"})) { | 446 | if (!comptime hasDecls(Cursor, .{"RowIDError"})) { |
| 438 | @compileError("the Cursor type must declare a RowIDError error set for the rowId function"); | 447 | @compileError("the Cursor type must declare a RowIDError error set for the rowId function"); |
| 439 | } | 448 | } |
| 440 | 449 | ||
| @@ -459,7 +468,7 @@ fn validateCursorType(comptime Table: type) void { | |||
| 459 | fn validateTableType(comptime Table: type) void { | 468 | fn validateTableType(comptime Table: type) void { |
| 460 | // Validate the `init` function | 469 | // Validate the `init` function |
| 461 | { | 470 | { |
| 462 | if (!meta.trait.hasDecls(Table, .{"InitError"})) { | 471 | if (!comptime hasDecls(Table, .{"InitError"})) { |
| 463 | @compileError("the Table type must declare a InitError error set for the init function"); | 472 | @compileError("the Table type must declare a InitError error set for the init function"); |
| 464 | } | 473 | } |
| 465 | 474 | ||
| @@ -501,7 +510,7 @@ fn validateTableType(comptime Table: type) void { | |||
| 501 | 510 | ||
| 502 | // Validate the `buildBestIndex` function | 511 | // Validate the `buildBestIndex` function |
| 503 | { | 512 | { |
| 504 | if (!meta.trait.hasDecls(Table, .{"BuildBestIndexError"})) { | 513 | if (!comptime hasDecls(Table, .{"BuildBestIndexError"})) { |
| 505 | @compileError("the Cursor type must declare a BuildBestIndexError error set for the buildBestIndex function"); | 514 | @compileError("the Cursor type must declare a BuildBestIndexError error set for the buildBestIndex function"); |
| 506 | } | 515 | } |
| 507 | 516 | ||
| @@ -522,7 +531,7 @@ fn validateTableType(comptime Table: type) void { | |||
| 522 | if (info.return_type.? != Table.BuildBestIndexError!void) @compileError(error_message); | 531 | if (info.return_type.? != Table.BuildBestIndexError!void) @compileError(error_message); |
| 523 | } | 532 | } |
| 524 | 533 | ||
| 525 | if (!meta.trait.hasDecls(Table, .{"Cursor"})) { | 534 | if (!comptime hasDecls(Table, .{"Cursor"})) { |
| 526 | @compileError("the Table type must declare a Cursor type"); | 535 | @compileError("the Table type must declare a Cursor type"); |
| 527 | } | 536 | } |
| 528 | } | 537 | } |