From 0c8026170052ee3ecd6c8d3cada1fa7776b02638 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 23 Apr 2021 02:48:38 +1000 Subject: Allow reading into static size arrays as long as size matches --- sqlite.zig | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'sqlite.zig') diff --git a/sqlite.zig b/sqlite.zig index 15898e2..0571ef8 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -630,32 +630,33 @@ pub fn Iterator(comptime Type: type) type { // readArray reads a sqlite BLOB or TEXT column into an array of u8. // - // We also require the array to have a sentinel because otherwise we have no way - // of communicating the end of the data to the caller. + // We also require the array to be the exact size of the data, or have a sentinel; + // otherwise we have no way of communicating the end of the data to the caller. // // If the array is too small for the data an error will be returned. - fn readArray(self: *Self, comptime ArrayType: type, _i: usize) error{ArrayTooSmall}!ArrayType { + fn readArray(self: *Self, comptime ArrayType: type, _i: usize) !ArrayType { const i = @intCast(c_int, _i); const type_info = @typeInfo(ArrayType); var ret: ArrayType = undefined; switch (type_info) { .Array => |arr| { - comptime if (arr.sentinel == null) { - @compileError("cannot populate array of " ++ @typeName(arr.child) ++ ", arrays must have a sentinel"); - }; - switch (arr.child) { u8 => { - const data = c.sqlite3_column_blob(self.stmt, i); const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i)); + if (arr.sentinel == null) { + if (size != arr.len) return error.ArraySizeMisMatch; + } else if (size >= @as(usize, arr.len)) { + return error.ArrayTooSmall; + } - if (size >= @as(usize, arr.len)) return error.ArrayTooSmall; - + const data = c.sqlite3_column_blob(self.stmt, i); const ptr = @ptrCast([*c]const u8, data)[0..size]; mem.copy(u8, ret[0..], ptr); - ret[size] = arr.sentinel.?; + if (arr.sentinel) |s| { + ret[size] = s; + } }, else => @compileError("cannot populate field " ++ field.name ++ " of type array of " ++ @typeName(arr.child)), } -- cgit v1.2.3