diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index af30e11..79f419d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,16 +2,16 @@ name: CI on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ "**" ] + branches: ["**"] concurrency: group: ${{ github.workflow }}-${{ github.ref }}-ci cancel-in-progress: true env: - ZIG_VERSION: 0.14.1 + ZIG_VERSION: 0.16.0 jobs: build-test: @@ -20,7 +20,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] + os: [ubuntu-latest, macos-latest, windows-latest] steps: - uses: actions/checkout@v4 diff --git a/.gitignore b/.gitignore index 7c046b6..03cb27d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -.zig-cache -zig-out \ No newline at end of file +.zig-cache/ +zig-out/ +zig-pkg/ diff --git a/build.zig b/build.zig index 84905be..a083ca8 100644 --- a/build.zig +++ b/build.zig @@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void { }), }); - lib.addCSourceFiles(.{ + lib.root_module.addCSourceFiles(.{ .root = upstream.path("."), .files = &[_][]const u8{ "snappy-sinksource.cc", @@ -44,12 +44,12 @@ pub fn build(b: *std.Build) void { .PROJECT_VERSION_PATCH = @as(i64, @intCast(snappy_version.patch)), }); - lib.addIncludePath(upstream.path(".")); - lib.addConfigHeader(snappy_stubs_public_h); + lib.root_module.addIncludePath(upstream.path(".")); + lib.root_module.addConfigHeader(snappy_stubs_public_h); lib.installHeader(upstream.path("snappy.h"), "snappy.h"); lib.installHeader(upstream.path("snappy-c.h"), "snappy-c.h"); - lib.installHeader(snappy_stubs_public_h.getOutput(), "snappy-stubs-public.h"); + lib.installHeader(snappy_stubs_public_h.getOutputFile(), "snappy-stubs-public.h"); b.installArtifact(lib); const module = b.addModule("snappy", .{ diff --git a/build.zig.zon b/build.zig.zon index ad0c930..f97836e 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -2,7 +2,7 @@ .name = .snappy, .version = "0.1.0", .fingerprint = 0xcbfb5fb8aa1a809f, - .minimum_zig_version = "0.14.1", + .minimum_zig_version = "0.16.0", .dependencies = .{ .snappy = .{ .url = "git+https://github.com/google/snappy#6f99459b5b837fa18abb1be317d3ac868530f384", diff --git a/src/frame.zig b/src/frame.zig index 761f520..8c379f1 100644 --- a/src/frame.zig +++ b/src/frame.zig @@ -36,9 +36,9 @@ pub const CompressError = std.mem.Allocator.Error || snappy.Error; /// /// Caller owns the returned memory. pub fn compress(allocator: std.mem.Allocator, bytes: []const u8) CompressError![]u8 { - var out = std.ArrayList(u8).init(allocator); - errdefer out.deinit(); - try out.appendSlice(&IDENTIFIER_FRAME); + var out = std.ArrayList(u8).empty; + errdefer out.deinit(allocator); + try out.appendSlice(allocator, &IDENTIFIER_FRAME); const max_compressed_len = snappy.maxCompressedLength(UNCOMPRESSED_CHUNK_SIZE_LIMIT); var compressed_buf = try allocator.alloc(u8, max_compressed_len); @@ -59,15 +59,15 @@ pub fn compress(allocator: std.mem.Allocator, bytes: []const u8) CompressError![ var header: [4]u8 = .{ @intFromEnum(chunk_type), 0, 0, 0 }; std.mem.writeInt(u24, header[1..4], @intCast(frame_size), .little); - try out.appendSlice(&header); + try out.appendSlice(allocator, &header); var checksum: [4]u8 = undefined; std.mem.writeInt(u32, &checksum, crc(chunk), .little); - try out.appendSlice(&checksum); - try out.appendSlice(payload); + try out.appendSlice(allocator, &checksum); + try out.appendSlice(allocator, payload); } - return out.toOwnedSlice(); + return out.toOwnedSlice(allocator); } /// Parse framed Snappy data and return the uncompressed payload, @@ -83,8 +83,8 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr var slice = bytes[IDENTIFIER_FRAME.len..]; - var out = std.ArrayList(u8).init(allocator); - errdefer out.deinit(); + var out = std.ArrayList(u8).empty; + errdefer out.deinit(allocator); while (slice.len > 0) { if (slice.len < 4) break; @@ -102,7 +102,7 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr const uncompressed_len = try snappy.uncompress(compressed, uncompressed[0..]); if (crc(uncompressed[0..uncompressed_len]) != std.mem.bytesToValue(u32, checksum)) return UncompressError.BadChecksum; - try out.appendSlice(uncompressed[0..uncompressed_len]); + try out.appendSlice(allocator, uncompressed[0..uncompressed_len]); }, .uncompressed => { const checksum = frame[0..4]; @@ -112,7 +112,7 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr return UncompressError.IllegalChunkLength; } if (crc(uncompressed) != std.mem.bytesToValue(u32, checksum)) return UncompressError.BadChecksum; - try out.appendSlice(uncompressed); + try out.appendSlice(allocator, uncompressed); }, .padding, .skippable, @@ -126,7 +126,7 @@ pub fn uncompress(allocator: std.mem.Allocator, bytes: []const u8) UncompressErr if (out.items.len == 0) return null; - return try out.toOwnedSlice(); + return try out.toOwnedSlice(allocator); } /// Masked CRC32C hash used by the Snappy framing format. diff --git a/src/snappy.zig b/src/snappy.zig index 139ae75..86c6a4f 100644 --- a/src/snappy.zig +++ b/src/snappy.zig @@ -13,18 +13,16 @@ test { test "round trip - raw" { const allocator = std.testing.allocator; + const io = std.testing.io; - var dir = try std.fs.cwd().openDir("testdata", .{ .iterate = true }); - defer dir.close(); + var dir = try std.Io.Dir.cwd().openDir(io, "testdata", .{ .iterate = true }); + defer dir.close(io); var it = dir.iterate(); - while (try it.next()) |entry| { + while (try it.next(io)) |entry| { if (entry.kind != .file) continue; - var file = try dir.openFile(entry.name, .{}); - defer file.close(); - - const bytes = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); + const bytes = try dir.readFileAlloc(io, entry.name, allocator, .unlimited); defer allocator.free(bytes); const compressed = try allocator.alloc(u8, raw.maxCompressedLength(bytes.len)); @@ -41,19 +39,17 @@ test "round trip - raw" { test "bad data" { const allocator = std.testing.allocator; + const io = std.testing.io; - var dir = try std.fs.cwd().openDir("testdata", .{ .iterate = true }); - defer dir.close(); + var dir = try std.Io.Dir.cwd().openDir(io, "testdata", .{ .iterate = true }); + defer dir.close(io); var it = dir.iterate(); - while (try it.next()) |entry| { + while (try it.next(io)) |entry| { if (entry.kind != .file) continue; if (!std.mem.startsWith(u8, entry.name, "baddata")) continue; - var file = try dir.openFile(entry.name, .{}); - defer file.close(); - - const bytes = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); + const bytes = try dir.readFileAlloc(io, entry.name, allocator, .unlimited); defer allocator.free(bytes); const got = try allocator.alloc(u8, try raw.uncompressedLength(bytes)); defer allocator.free(got); @@ -63,18 +59,16 @@ test "bad data" { test "round trip - framed" { const allocator = std.testing.allocator; + const io = std.testing.io; - var dir = try std.fs.cwd().openDir("testdata", .{ .iterate = true }); - defer dir.close(); + var dir = try std.Io.Dir.cwd().openDir(io, "testdata", .{ .iterate = true }); + defer dir.close(io); var it = dir.iterate(); - while (try it.next()) |entry| { + while (try it.next(io)) |entry| { if (entry.kind != .file) continue; - var file = try dir.openFile(entry.name, .{}); - defer file.close(); - - const bytes = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); + const bytes = try dir.readFileAlloc(io, entry.name, allocator, .unlimited); defer allocator.free(bytes); const d = bytes[0..]; const compressed = try frame.compress(allocator, d[0..]);