From bb63c43f4473dbe6e06d7bfe33e026b9497a3959 Mon Sep 17 00:00:00 2001 From: Ben Sinclair Date: Mon, 18 Mar 2024 09:06:52 +1100 Subject: [PATCH] Type checking can be much simpler --- zbench.zig | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/zbench.zig b/zbench.zig index f12146c..5f7e83c 100644 --- a/zbench.zig +++ b/zbench.zig @@ -115,40 +115,8 @@ pub const Benchmark = struct { ), }; - // Check the type of the benchmark parameter has a run function. - comptime switch (@typeInfo(T)) { - .Struct => |str| { - var run_found: bool = false; - for (str.decls) |decl| if (std.mem.eql(u8, decl.name, "run")) { - run_found = true; - }; - if (!run_found) @compileError( - "benchmark must be a const ptr to a struct with a 'run' method", - ); - }, - else => @compileError( - "benchmark must be a const ptr to a struct with a 'run' method", - ), - }; - - // Check the run function has the proper type - comptime switch (@typeInfo(@TypeOf(@field(T, "run")))) { - .Fn => |info| { - if (info.params.len != 2) @compileError( - "benchmark run function must have two parameters", - ); - if (info.params[0].type != T) @compileError( - "benchmark run function's first parameter must be self", - ); - if (info.params[1].type != std.mem.Allocator) @compileError( - "benchmark run function's second parameter must be an Allocator", - ); - if (info.return_type != void) @compileError( - "benchmark run function must return void", - ); - }, - else => unreachable, - }; + // Check the benchmark parameter has a well typed run function. + _ = @as(fn (T, std.mem.Allocator) void, T.run); try self.benchmarks.append(self.allocator, Definition{ .name = name,