@@ -7,70 +7,66 @@ const OutBuffer = types.OutBuffer;
77const ZstdError = @import ("error.zig" ).Error ;
88
99pub 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