diff --git a/src/DB.zig b/src/DB.zig index 53d3413..aaac6e4 100644 --- a/src/DB.zig +++ b/src/DB.zig @@ -456,9 +456,7 @@ fn usersSection( while (i < corpus.users.len) : (i += 1) { // TODO: this is inefficient by calling `.slice()` on every iteration const user = corpus.users.get(i); - const user_offset = math.cast(u35, blob.items.len) catch |err| switch (err) { - error.Overflow => return error.TooMany, - }; + const user_offset = math.cast(u35, blob.items.len) orelse return error.TooMany; assert(user_offset & 7 == 0); idx2offset[i] = @truncate(u32, user_offset >> 3); try PackedUser.packTo( diff --git a/src/File.zig b/src/File.zig index a3ac21d..15813b4 100644 --- a/src/File.zig +++ b/src/File.zig @@ -51,17 +51,23 @@ pub const TestFile = struct { var tmp = testing.tmpDir(.{}); errdefer tmp.cleanup(); + const base_path = blk: { + const relative_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] }); + const real_path = try fs.realpathAlloc(allocator, relative_path); + allocator.free(relative_path); + break :blk real_path; + }; + + const full_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "db.turbo\x00" }); + allocator.free(base_path); + const mode = os.O.RDWR | os.O.CREAT | os.O.EXCL; const fd = try os.openat(tmp.dir.fd, "db.turbo", mode, 0o666); defer os.close(fd); _ = try os.writev(fd, db.iov().constSlice()); - const dir_path = try tmp.getFullPath(allocator); - defer allocator.free(dir_path); - const full = &[_][]const u8{ dir_path, "db.turbo\x00" }; - var result = try fs.path.join(allocator, full); - return TestFile{ .dir = tmp, .path = result[0 .. result.len - 1 :0] }; + return TestFile{ .dir = tmp, .path = full_path[0 .. full_path.len - 1 :0] }; } pub fn deinit(self: *TestFile, allocator: Allocator) void { diff --git a/src/bdz.zig b/src/bdz.zig index acff6e7..2701b88 100644 --- a/src/bdz.zig +++ b/src/bdz.zig @@ -3,7 +3,7 @@ const std = @import("std"); extern fn bdz_search_packed(packed_mphf: [*]const u8, key: [*]const u8, len: c_uint) u32; pub fn search(packed_mphf: []const u8, key: []const u8) u32 { - const len = std.math.cast(c_uint, key.len) catch unreachable; + const len = std.math.cast(c_uint, key.len).?; return @as(u32, bdz_search_packed(packed_mphf.ptr, key.ptr, len)); } diff --git a/src/validate.zig b/src/validate.zig index 657c526..ccb4755 100644 --- a/src/validate.zig +++ b/src/validate.zig @@ -1,11 +1,7 @@ const std = @import("std"); pub fn downCast(comptime T: type, n: u64) error{InvalidRecord}!T { - return std.math.cast(T, n) catch |err| switch (err) { - error.Overflow => { - return error.InvalidRecord; - }, - }; + return std.math.cast(T, n) orelse return error.InvalidRecord; } pub fn utf8(s: []const u8) error{InvalidRecord}!void {