Skip to content

Commit c906418

Browse files
committed
bandaid over 0.15 writer upgrade
1 parent bdd03f4 commit c906418

File tree

3 files changed

+52
-55
lines changed

3 files changed

+52
-55
lines changed

src/reader.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub const Reader = struct {
2222
self.decompressor.deinit();
2323
}
2424

25-
pub const R = std.io.Reader(*Reader, Error, read);
25+
pub const R = std.io.GenericReader(*Reader, Error, read);
2626
pub fn reader(self: *Reader) R {
2727
return .{ .context = self };
2828
}

src/tests.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ comptime {
77

88
test "writing & reading" {
99
std.testing.log_level = std.log.Level.err;
10+
const allocator = std.testing.allocator;
1011

1112
const test_data = blk: {
1213
var bytes: [32]u8 = undefined;
@@ -18,21 +19,21 @@ test "writing & reading" {
1819
break :blk bytes ++ "Foo Bar Baz".* ++ bytes;
1920
};
2021

21-
var compressed_data = std.ArrayList(u8).init(std.testing.allocator);
22+
var compressed_data: std.io.Writer.Allocating = .init(allocator);
2223
defer compressed_data.deinit();
2324

2425
const compressor = try zstd.Compressor.init(.{});
2526
defer compressor.deinit();
2627
var buffer: [128]u8 = undefined;
27-
const writer_ctx = zstd.writerCtx(compressed_data.writer(), &compressor, &buffer);
28+
const writer_ctx = zstd.writerCtx(&compressed_data.writer, &compressor, &buffer);
2829
const writer = writer_ctx.writer();
2930

3031
try writer.writeAll(&test_data);
3132
try writer_ctx.finish();
3233

33-
try std.testing.expect(compressed_data.items.len <= test_data.len);
34+
try std.testing.expect(compressed_data.written().len <= test_data.len);
3435

35-
var reader_state = try zstd.Reader.init(compressed_data.items);
36+
var reader_state = try zstd.Reader.init(compressed_data.written());
3637
defer reader_state.deinit();
3738
const reader = reader_state.reader();
3839

src/writer.zig

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,66 @@ const OutBuffer = types.OutBuffer;
77
const ZstdError = @import("error.zig").Error;
88

99
pub inline fn writerCtx(
10-
inner_writer: anytype,
10+
inner_writer: *std.io.Writer,
1111
compressor: *const Compressor,
1212
out_buffer: []u8,
13-
) WriterCtx(@TypeOf(inner_writer)) {
13+
) Writer {
1414
return .{
1515
.inner = inner_writer,
1616
.compressor = compressor,
1717
.out_buffer = out_buffer,
1818
};
1919
}
2020

21-
pub fn WriterCtx(comptime InnerWriter: type) type {
22-
return struct {
23-
inner: InnerWriter,
24-
compressor: *const Compressor,
25-
out_buffer: []u8,
26-
const Self = @This();
21+
pub const Writer = struct {
22+
inner: *std.io.Writer,
23+
compressor: *const Compressor,
24+
out_buffer: []u8,
2725

28-
const Error = ZstdError || InnerWriter.Error;
29-
pub const Writer = std.io.Writer(Self, Error, zstdWrite);
26+
const Error = ZstdError || std.io.Writer.Error;
3027

31-
pub inline fn writer(ctx: Self) Writer {
32-
return .{ .context = ctx };
33-
}
28+
pub inline fn writer(ctx: Writer) std.io.GenericWriter(Writer, Error, zstdWrite) {
29+
return .{ .context = ctx };
30+
}
3431

35-
/// This must be called after writing all the data that has to be compressed.
36-
/// After doing so, all of the internal buffers of the compressor will have been
37-
/// flushed to the destination writer, allowing re-use and re-configuration of
38-
/// the referenced Compressor.
39-
pub fn finish(ctx: Self) Error!void {
40-
while (true) {
41-
var out_buffer: OutBuffer = .{
42-
.dst = ctx.out_buffer.ptr,
43-
.size = ctx.out_buffer.len,
44-
.pos = 0,
45-
};
46-
var in_buffer: InBuffer = .{
47-
.src = "",
48-
.size = 0,
49-
.pos = 0,
50-
};
51-
const remaining = try ctx.compressor.compressStream(&in_buffer, &out_buffer, .end);
52-
try ctx.inner.writeAll(out_buffer.dst[0..out_buffer.pos]);
53-
if (remaining == 0) break;
54-
}
32+
/// This must be called after writing all the data that has to be compressed.
33+
/// After doing so, all of the internal buffers of the compressor will have been
34+
/// flushed to the destination writer, allowing re-use and re-configuration of
35+
/// the referenced Compressor.
36+
pub fn finish(ctx: Writer) Error!void {
37+
while (true) {
38+
var out_buffer: OutBuffer = .{
39+
.dst = ctx.out_buffer.ptr,
40+
.size = ctx.out_buffer.len,
41+
.pos = 0,
42+
};
43+
var in_buffer: InBuffer = .{
44+
.src = "",
45+
.size = 0,
46+
.pos = 0,
47+
};
48+
const remaining = try ctx.compressor.compressStream(&in_buffer, &out_buffer, .end);
49+
try ctx.inner.writeAll(out_buffer.dst[0..out_buffer.pos]);
50+
if (remaining == 0) break;
5551
}
52+
}
5653

57-
fn zstdWrite(ctx: Self, bytes: []const u8) Error!usize {
58-
var in_buffer: InBuffer = .{
59-
.src = bytes.ptr,
60-
.size = bytes.len,
54+
fn zstdWrite(ctx: Writer, bytes: []const u8) Error!usize {
55+
var in_buffer: InBuffer = .{
56+
.src = bytes.ptr,
57+
.size = bytes.len,
58+
.pos = 0,
59+
};
60+
while (true) {
61+
var out_buffer: OutBuffer = .{
62+
.dst = ctx.out_buffer.ptr,
63+
.size = ctx.out_buffer.len,
6164
.pos = 0,
6265
};
63-
while (true) {
64-
var out_buffer: OutBuffer = .{
65-
.dst = ctx.out_buffer.ptr,
66-
.size = ctx.out_buffer.len,
67-
.pos = 0,
68-
};
69-
const remaining = try ctx.compressor.compressStream(&in_buffer, &out_buffer, .continue_);
70-
try ctx.inner.writeAll(out_buffer.dst[0..out_buffer.pos]);
71-
if (remaining == 0 or in_buffer.pos == in_buffer.size) break;
72-
}
73-
return bytes.len;
66+
const remaining = try ctx.compressor.compressStream(&in_buffer, &out_buffer, .continue_);
67+
try ctx.inner.writeAll(out_buffer.dst[0..out_buffer.pos]);
68+
if (remaining == 0 or in_buffer.pos == in_buffer.size) break;
7469
}
75-
};
76-
}
70+
return bytes.len;
71+
}
72+
};

0 commit comments

Comments
 (0)