Skip to content

Commit 94dd28b

Browse files
committed
std.Io: delete CountingWriter
1 parent 3fb8684 commit 94dd28b

File tree

9 files changed

+39
-91
lines changed

9 files changed

+39
-91
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/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 {

src/link/Elf.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,10 +3152,11 @@ fn writeSyntheticSections(self: *Elf) !void {
31523152

31533153
if (self.section_indexes.gnu_hash) |shndx| {
31543154
const shdr = slice.items(.shdr)[shndx];
3155-
var buffer = try std.ArrayList(u8).initCapacity(gpa, self.gnu_hash.size());
3156-
defer buffer.deinit();
3157-
try self.gnu_hash.write(self, buffer.writer());
3158-
try self.pwriteAll(buffer.items, shdr.sh_offset);
3155+
var aw: std.Io.Writer.Allocating = .init(gpa);
3156+
try aw.ensureUnusedCapacity(self.gnu_hash.size());
3157+
defer aw.deinit();
3158+
try self.gnu_hash.write(self, &aw.writer);
3159+
try self.pwriteAll(aw.getWritten(), shdr.sh_offset);
31593160
}
31603161

31613162
if (self.section_indexes.versym) |shndx| {

src/link/Elf/synthetic_sections.zig

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,17 +1237,14 @@ pub const GnuHashSection = struct {
12371237
return header_size + hash.num_bloom * 8 + hash.num_buckets * 4 + hash.num_exports * 4;
12381238
}
12391239

1240-
pub fn write(hash: GnuHashSection, elf_file: *Elf, writer: anytype) !void {
1240+
pub fn write(hash: GnuHashSection, elf_file: *Elf, writer: *std.Io.Writer) !void {
12411241
const exports = getExports(elf_file);
12421242
const export_off = elf_file.dynsym.count() - hash.num_exports;
12431243

1244-
var counting = std.io.countingWriter(writer);
1245-
const cwriter = counting.writer();
1246-
1247-
try cwriter.writeInt(u32, hash.num_buckets, .little);
1248-
try cwriter.writeInt(u32, export_off, .little);
1249-
try cwriter.writeInt(u32, hash.num_bloom, .little);
1250-
try cwriter.writeInt(u32, bloom_shift, .little);
1244+
try writer.writeInt(u32, hash.num_buckets, .little);
1245+
try writer.writeInt(u32, export_off, .little);
1246+
try writer.writeInt(u32, hash.num_bloom, .little);
1247+
try writer.writeInt(u32, bloom_shift, .little);
12511248

12521249
const comp = elf_file.base.comp;
12531250
const gpa = comp.gpa;
@@ -1271,7 +1268,7 @@ pub const GnuHashSection = struct {
12711268
bloom[idx] |= @as(u64, 1) << @as(u6, @intCast((h >> bloom_shift) % 64));
12721269
}
12731270

1274-
try cwriter.writeAll(mem.sliceAsBytes(bloom));
1271+
try writer.writeAll(mem.sliceAsBytes(bloom));
12751272

12761273
// Fill in the hash bucket indices
12771274
const buckets = try gpa.alloc(u32, hash.num_buckets);
@@ -1284,7 +1281,7 @@ pub const GnuHashSection = struct {
12841281
}
12851282
}
12861283

1287-
try cwriter.writeAll(mem.sliceAsBytes(buckets));
1284+
try writer.writeAll(mem.sliceAsBytes(buckets));
12881285

12891286
// Finally, write the hash table
12901287
const table = try gpa.alloc(u32, hash.num_exports);
@@ -1300,9 +1297,7 @@ pub const GnuHashSection = struct {
13001297
}
13011298
}
13021299

1303-
try cwriter.writeAll(mem.sliceAsBytes(table));
1304-
1305-
assert(counting.bytes_written == hash.size());
1300+
try writer.writeAll(mem.sliceAsBytes(table));
13061301
}
13071302

13081303
pub fn hasher(name: [:0]const u8) u32 {

src/link/MachO/dyld_info/Trie.zig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,18 @@ const FinalizeNodeResult = struct {
186186

187187
/// Updates offset of this node in the output byte stream.
188188
fn finalizeNode(self: *Trie, node_index: Node.Index, offset_in_trie: u32) !FinalizeNodeResult {
189-
var stream = std.io.countingWriter(std.io.null_writer);
190-
const writer = stream.writer();
189+
var trash_buffer: [64]u8 = undefined;
190+
var stream: std.Io.Writer.Discarding = .init(&trash_buffer);
191+
const writer = &stream.writer;
191192
const slice = self.nodes.slice();
192193

193194
var node_size: u32 = 0;
194195
if (slice.items(.is_terminal)[node_index]) {
195196
const export_flags = slice.items(.export_flags)[node_index];
196197
const vmaddr_offset = slice.items(.vmaddr_offset)[node_index];
197-
try leb.writeUleb128(writer, export_flags);
198-
try leb.writeUleb128(writer, vmaddr_offset);
199-
try leb.writeUleb128(writer, stream.bytes_written);
198+
try writer.writeUleb128(export_flags);
199+
try writer.writeUleb128(vmaddr_offset);
200+
try writer.writeUleb128(stream.fullCount());
200201
} else {
201202
node_size += 1; // 0x0 for non-terminal nodes
202203
}
@@ -206,13 +207,13 @@ fn finalizeNode(self: *Trie, node_index: Node.Index, offset_in_trie: u32) !Final
206207
const edge = &self.edges.items[edge_index];
207208
const next_node_offset = slice.items(.trie_offset)[edge.node];
208209
node_size += @intCast(edge.label.len + 1);
209-
try leb.writeUleb128(writer, next_node_offset);
210+
try writer.writeUleb128(next_node_offset);
210211
}
211212

212213
const trie_offset = slice.items(.trie_offset)[node_index];
213214
const updated = offset_in_trie != trie_offset;
214215
slice.items(.trie_offset)[node_index] = offset_in_trie;
215-
node_size += @intCast(stream.bytes_written);
216+
node_size += @intCast(stream.fullCount());
216217

217218
return .{ .node_size = node_size, .updated = updated };
218219
}

0 commit comments

Comments
 (0)