Skip to content

Commit d2f7792

Browse files
authored
Merge pull request ziglang#24742 from ziglang/CountingWriter
std.Io: delete CountingWriter
2 parents 3ea015d + 9a538e0 commit d2f7792

File tree

11 files changed

+76
-119
lines changed

11 files changed

+76
-119
lines changed

lib/compiler/resinator/cvtres.zig

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -665,19 +665,16 @@ const ResourceTree = struct {
665665
pub fn writeCoff(
666666
self: *const ResourceTree,
667667
allocator: Allocator,
668-
writer: anytype,
668+
w: anytype,
669669
resources_in_data_order: []const Resource,
670670
lengths: Lengths,
671671
coff_string_table: *StringTable,
672672
) ![]const std.coff.Symbol {
673673
if (self.type_to_name_map.count() == 0) {
674-
try writer.writeByteNTimes(0, 16);
674+
try w.writeByteNTimes(0, 16);
675675
return &.{};
676676
}
677677

678-
var counting_writer = std.io.countingWriter(writer);
679-
const w = counting_writer.writer();
680-
681678
var level2_list: std.ArrayListUnmanaged(*const NameToLanguageMap) = .empty;
682679
defer level2_list.deinit(allocator);
683680

@@ -735,7 +732,6 @@ const ResourceTree = struct {
735732
try level2_list.append(allocator, name_to_lang_map);
736733
}
737734
}
738-
std.debug.assert(counting_writer.bytes_written == level2_start);
739735

740736
const level3_start = level2_start + lengths.level2;
741737
var level3_address = level3_start;
@@ -771,7 +767,6 @@ const ResourceTree = struct {
771767
try level3_list.append(allocator, lang_to_resources_map);
772768
}
773769
}
774-
std.debug.assert(counting_writer.bytes_written == level3_start);
775770

776771
var reloc_addresses = try allocator.alloc(u32, resources_in_data_order.len);
777772
defer allocator.free(reloc_addresses);
@@ -813,7 +808,6 @@ const ResourceTree = struct {
813808
try resources_list.append(allocator, reloc_resource);
814809
}
815810
}
816-
std.debug.assert(counting_writer.bytes_written == data_entries_start);
817811

818812
for (resources_list.items, 0..) |reloc_resource, i| {
819813
// TODO: This logic works but is convoluted, would be good to clean this up
@@ -827,7 +821,6 @@ const ResourceTree = struct {
827821
};
828822
try w.writeStructEndian(data_entry, .little);
829823
}
830-
std.debug.assert(counting_writer.bytes_written == strings_start);
831824

832825
for (self.rsrc_string_table.keys()) |v| {
833826
const str = v.name;

lib/std/Io.zig

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,6 @@ pub const FixedBufferStream = @import("Io/fixed_buffer_stream.zig").FixedBufferS
432432
/// Deprecated in favor of `Reader`.
433433
pub const fixedBufferStream = @import("Io/fixed_buffer_stream.zig").fixedBufferStream;
434434
/// Deprecated with no replacement; inefficient pattern
435-
pub const CountingWriter = @import("Io/counting_writer.zig").CountingWriter;
436-
/// Deprecated with no replacement; inefficient pattern
437-
pub const countingWriter = @import("Io/counting_writer.zig").countingWriter;
438-
/// Deprecated with no replacement; inefficient pattern
439435
pub const CountingReader = @import("Io/counting_reader.zig").CountingReader;
440436
/// Deprecated with no replacement; inefficient pattern
441437
pub const countingReader = @import("Io/counting_reader.zig").countingReader;
@@ -917,7 +913,6 @@ test {
917913
_ = Reader;
918914
_ = Writer;
919915
_ = BufferedWriter;
920-
_ = CountingWriter;
921916
_ = CountingReader;
922917
_ = FixedBufferStream;
923918
_ = tty;

lib/std/Io/Writer.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,11 @@ pub const Discarding = struct {
22392239
};
22402240
}
22412241

2242+
/// Includes buffered data (no need to flush).
2243+
pub fn fullCount(d: *const Discarding) u64 {
2244+
return d.count + d.writer.end;
2245+
}
2246+
22422247
pub fn drain(w: *Writer, data: []const []const u8, splat: usize) Error!usize {
22432248
const d: *Discarding = @alignCast(@fieldParentPtr("writer", w));
22442249
const slice = data[0 .. data.len - 1];

lib/std/Io/counting_writer.zig

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/std/crypto/phc_encoding.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fmt = std.fmt;
55
const io = std.io;
66
const mem = std.mem;
77
const meta = std.meta;
8+
const Writer = std.Io.Writer;
89

910
const fields_delimiter = "$";
1011
const fields_delimiter_scalar = '$';
@@ -188,19 +189,20 @@ pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult
188189
///
189190
/// `params` can also include any additional parameters.
190191
pub fn serialize(params: anytype, str: []u8) Error![]const u8 {
191-
var buf = io.fixedBufferStream(str);
192-
try serializeTo(params, buf.writer());
193-
return buf.getWritten();
192+
var w: Writer = .fixed(str);
193+
serializeTo(params, &w) catch return error.NoSpaceLeft;
194+
return w.buffered();
194195
}
195196

196197
/// Compute the number of bytes required to serialize `params`
197198
pub fn calcSize(params: anytype) usize {
198-
var buf = io.countingWriter(io.null_writer);
199-
serializeTo(params, buf.writer()) catch unreachable;
200-
return @as(usize, @intCast(buf.bytes_written));
199+
var trash: [128]u8 = undefined;
200+
var d: Writer.Discarding = .init(&trash);
201+
serializeTo(params, &d.writer) catch unreachable;
202+
return @intCast(d.fullCount());
201203
}
202204

203-
fn serializeTo(params: anytype, out: anytype) !void {
205+
fn serializeTo(params: anytype, out: *std.Io.Writer) !void {
204206
const HashResult = @TypeOf(params);
205207

206208
if (@hasField(HashResult, version_param_name)) {

lib/std/crypto/scrypt.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ const crypt_format = struct {
311311

312312
/// Compute the number of bytes required to serialize `params`
313313
pub fn calcSize(params: anytype) usize {
314-
var buf = io.countingWriter(io.null_writer);
315-
serializeTo(params, buf.writer()) catch unreachable;
316-
return @as(usize, @intCast(buf.bytes_written));
314+
var trash: [128]u8 = undefined;
315+
var d: std.Io.Writer.Discarding = .init(&trash);
316+
serializeTo(params, &d) catch unreachable;
317+
return @intCast(d.fullCount());
317318
}
318319

319320
fn serializeTo(params: anytype, out: anytype) !void {

src/arch/x86_64/encoder.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,11 +1204,10 @@ const TestEncode = struct {
12041204
mnemonic: Instruction.Mnemonic,
12051205
ops: []const Instruction.Operand,
12061206
) !void {
1207-
var stream = std.io.fixedBufferStream(&enc.buffer);
1208-
var count_writer = std.io.countingWriter(stream.writer());
1207+
var writer: std.Io.Writer = .fixed(&enc.buffer);
12091208
const inst: Instruction = try .new(.none, mnemonic, ops);
1210-
try inst.encode(count_writer.writer(), .{});
1211-
enc.index = count_writer.bytes_written;
1209+
try inst.encode(&writer, .{});
1210+
enc.index = writer.bufferedLen();
12121211
}
12131212

12141213
fn code(enc: TestEncode) []const u8 {

0 commit comments

Comments
 (0)