From 40337a755b4855e3ca83659a9f943bb4cb9008f8 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 23 Nov 2023 20:58:55 +0100 Subject: rewrite a helper to check if a type is a struct std.meta.trait doesn't exist anymore. --- sqlite.zig | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'sqlite.zig') diff --git a/sqlite.zig b/sqlite.zig index d521175..4fd06df 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -21,6 +21,7 @@ const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode; const getTestDb = @import("test.zig").getTestDb; pub const vtab = @import("vtab.zig"); + const helpers = @import("helpers.zig"); test { @@ -29,6 +30,11 @@ test { const logger = std.log.scoped(.sqlite); +fn isStruct(comptime typ: type) bool { + const type_info = @typeInfo(typ); + return type_info == .Struct; +} + /// Text is used to represent a SQLite TEXT value when binding a parameter or reading a column. pub const Text = struct { data: []const u8 }; @@ -1308,7 +1314,7 @@ pub fn Iterator(comptime Type: type) type { } fn readPointer(self: *Self, comptime PointerType: type, options: anytype, i: usize) !PointerType { - if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { + if (!comptime isStruct(@TypeOf(options))) { @compileError("options passed to readPointer must be a struct"); } if (!comptime std.meta.trait.hasField("allocator")(@TypeOf(options))) { @@ -1339,7 +1345,7 @@ pub fn Iterator(comptime Type: type) type { } fn readOptional(self: *Self, comptime OptionalType: type, options: anytype, _i: usize) !OptionalType { - if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { + if (!comptime isStruct(@TypeOf(options))) { @compileError("options passed to readOptional must be a struct"); } @@ -1384,7 +1390,7 @@ pub fn Iterator(comptime Type: type) type { // // TODO(vincent): add comptime checks for the fields/columns. fn readStruct(self: *Self, options: anytype) !Type { - if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { + if (!comptime isStruct(@TypeOf(options))) { @compileError("options passed to readStruct must be a struct"); } @@ -1402,7 +1408,7 @@ pub fn Iterator(comptime Type: type) type { } fn readField(self: *Self, comptime FieldType: type, options: anytype, i: usize) !FieldType { - if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { + if (!comptime isStruct(@TypeOf(options))) { @compileError("options passed to readField must be a struct"); } @@ -2013,7 +2019,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: anytype) type /// The types are checked at comptime. fn bind(self: *Self, options: anytype, values: anytype) !void { const StructType = @TypeOf(values); - if (!comptime std.meta.trait.is(.Struct)(@TypeOf(values))) { + if (!comptime isStruct(StructType)) { @compileError("options passed to Statement.bind must be a struct (DynamicStatement supports runtime slices)"); } -- cgit v1.2.3