std.io.Writer.Allocating: rename interface to writer

This commit is contained in:
Andrew Kelley 2025-07-01 09:41:41 -07:00
parent fc310ee7bc
commit fac5fe57be
8 changed files with 37 additions and 37 deletions

View File

@ -1380,8 +1380,8 @@ const MachODumper = struct {
},
macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => {
name_buf.clearRetainingCapacity();
try br.streamDelimiterLimit(&name_buf, 0, .limited(std.math.maxInt(u32)));
try name_buf.writeByte(0);
_ = try br.streamDelimiterLimit(&name_buf.writer, 0, .limited(std.math.maxInt(u32)));
try name_buf.writer.writeByte(0);
},
macho.BIND_OPCODE_SET_ADDEND_SLEB => {
addend = try br.takeLeb128(i64);
@ -1588,7 +1588,7 @@ const MachODumper = struct {
var aw: std.io.Writer.Allocating = .init(gpa);
defer aw.deinit();
const bw = &aw.interface;
const bw = &aw.writer;
switch (check.kind) {
.headers => {
@ -1741,7 +1741,7 @@ const ElfDumper = struct {
var aw: std.io.Writer.Allocating = .init(gpa);
defer aw.deinit();
const bw = &aw.interface;
const bw = &aw.writer;
switch (check.kind) {
.archive_symtab => if (ctx.symtab.len > 0) {
@ -1888,7 +1888,7 @@ const ElfDumper = struct {
var aw: std.io.Writer.Allocating = .init(gpa);
defer aw.deinit();
const bw = &aw.interface;
const bw = &aw.writer;
switch (check.kind) {
.headers => {
@ -2338,7 +2338,7 @@ const WasmDumper = struct {
var aw: std.io.Writer.Allocating = .init(gpa);
defer aw.deinit();
const bw = &aw.interface;
const bw = &aw.writer;
parseAndDumpInner(step, check, &br, bw) catch |err| switch (err) {
error.EndOfStream => try bw.writeAll("\n<UnexpectedEndOfStream>"),

View File

@ -198,7 +198,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
var aw: std.io.Writer.Allocating = .init(gpa);
defer aw.deinit();
const bw = &aw.interface;
const bw = &aw.writer;
const header_text = "This file was generated by ConfigHeader using the Zig Build System.";
const c_generated_line = "/* " ++ header_text ++ " */\n";
@ -335,7 +335,7 @@ fn render_autoconf_at(
) !void {
const build = step.owner;
const allocator = build.allocator;
const bw = &aw.interface;
const bw = &aw.writer;
const used = allocator.alloc(bool, values.count()) catch @panic("OOM");
for (used) |*u| u.* = false;
@ -753,17 +753,17 @@ fn testReplaceVariablesAutoconfAt(
expected: []const u8,
values: std.StringArrayHashMap(Value),
) !void {
var output: std.io.Writer.Allocating = .init(allocator);
defer output.deinit();
var aw: std.io.Writer.Allocating = .init(allocator);
defer aw.deinit();
const used = try allocator.alloc(bool, values.count());
for (used) |*u| u.* = false;
defer allocator.free(used);
try expand_variables_autoconf_at(&output.interface, contents, values, used);
try expand_variables_autoconf_at(&aw.writer, contents, values, used);
for (used) |u| if (!u) return error.UnusedValue;
try std.testing.expectEqualStrings(expected, output.getWritten());
try std.testing.expectEqualStrings(expected, aw.getWritten());
}
fn testReplaceVariablesCMake(

View File

@ -941,7 +941,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig
try self.ensureUnusedCapacity(gpa, fmt.len);
var aw: std.io.Writer.Allocating = .fromArrayList(gpa, self);
defer self.* = aw.toArrayList();
return aw.interface.print(fmt, args) catch |err| switch (err) {
return aw.writer.print(fmt, args) catch |err| switch (err) {
error.WriteFailed => return error.OutOfMemory,
};
}

View File

@ -307,7 +307,7 @@ test dumpHexFallible {
var aw: std.io.Writer.Allocating = .init(std.testing.allocator);
defer aw.deinit();
try dumpHexFallible(&aw.interface, .no_color, bytes);
try dumpHexFallible(&aw.writer, .no_color, bytes);
const expected = try std.fmt.allocPrint(std.testing.allocator,
\\{x:0>[2]} 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........
\\{x:0>[2]} 01 12 13 ...
@ -1230,7 +1230,7 @@ fn printLineFromFileAnyOs(writer: *Writer, source_location: SourceLocation) !voi
test printLineFromFileAnyOs {
var aw: Writer.Allocating = .init(std.testing.allocator);
defer aw.deinit();
const output_stream = &aw.interface;
const output_stream = &aw.writer;
const allocator = std.testing.allocator;
const join = std.fs.path.join;

View File

@ -759,7 +759,7 @@ pub fn count(comptime fmt: []const u8, args: anytype) usize {
pub fn allocPrint(gpa: Allocator, comptime fmt: []const u8, args: anytype) Allocator.Error![]u8 {
var aw = try Writer.Allocating.initCapacity(gpa, fmt.len);
defer aw.deinit();
aw.interface.print(fmt, args) catch |err| switch (err) {
aw.writer.print(fmt, args) catch |err| switch (err) {
error.WriteFailed => return error.OutOfMemory,
};
return aw.toOwnedSlice();
@ -773,7 +773,7 @@ pub fn allocPrintSentinel(
) Allocator.Error![:sentinel]u8 {
var aw = try Writer.Allocating.initCapacity(gpa, fmt.len);
defer aw.deinit();
aw.interface.print(fmt, args) catch |err| switch (err) {
aw.writer.print(fmt, args) catch |err| switch (err) {
error.WriteFailed => return error.OutOfMemory,
};
return aw.toOwnedSliceSentinel(sentinel);

View File

@ -2056,12 +2056,12 @@ pub fn Hashed(comptime Hasher: type) type {
/// When using this API, it is not necessary to call `flush`.
pub const Allocating = struct {
allocator: Allocator,
interface: Writer,
writer: Writer,
pub fn init(allocator: Allocator) Allocating {
return .{
.allocator = allocator,
.interface = .{
.writer = .{
.buffer = &.{},
.vtable = &vtable,
},
@ -2071,7 +2071,7 @@ pub const Allocating = struct {
pub fn initCapacity(allocator: Allocator, capacity: usize) error{OutOfMemory}!Allocating {
return .{
.allocator = allocator,
.interface = .{
.writer = .{
.buffer = try allocator.alloc(u8, capacity),
.vtable = &vtable,
},
@ -2081,7 +2081,7 @@ pub const Allocating = struct {
pub fn initOwnedSlice(allocator: Allocator, slice: []u8) Allocating {
return .{
.allocator = allocator,
.interface = .{
.writer = .{
.buffer = slice,
.vtable = &vtable,
},
@ -2093,7 +2093,7 @@ pub const Allocating = struct {
defer array_list.* = .empty;
return .{
.allocator = allocator,
.interface = .{
.writer = .{
.vtable = &vtable,
.buffer = array_list.allocatedSlice(),
.end = array_list.items.len,
@ -2108,14 +2108,14 @@ pub const Allocating = struct {
};
pub fn deinit(a: *Allocating) void {
a.allocator.free(a.interface.buffer);
a.allocator.free(a.writer.buffer);
a.* = undefined;
}
/// Returns an array list that takes ownership of the allocated memory.
/// Resets the `Allocating` to an empty state.
pub fn toArrayList(a: *Allocating) std.ArrayListUnmanaged(u8) {
const w = &a.interface;
const w = &a.writer;
const result: std.ArrayListUnmanaged(u8) = .{
.items = w.buffer[0..w.end],
.capacity = w.buffer.len,
@ -2137,13 +2137,13 @@ pub const Allocating = struct {
}
pub fn getWritten(a: *Allocating) []u8 {
return a.interface.buffered();
return a.writer.buffered();
}
pub fn shrinkRetainingCapacity(a: *Allocating, new_len: usize) void {
const shrink_by = a.interface.end - new_len;
a.interface.end = new_len;
a.interface.count -= shrink_by;
const shrink_by = a.writer.end - new_len;
a.writer.end = new_len;
a.writer.count -= shrink_by;
}
pub fn clearRetainingCapacity(a: *Allocating) void {
@ -2151,7 +2151,7 @@ pub const Allocating = struct {
}
fn drain(w: *Writer, data: []const []const u8, splat: usize) Error!usize {
const a: *Allocating = @fieldParentPtr("interface", w);
const a: *Allocating = @fieldParentPtr("writer", w);
const gpa = a.allocator;
const pattern = data[data.len - 1];
const splat_len = pattern.len * splat;
@ -2177,7 +2177,7 @@ pub const Allocating = struct {
fn sendFile(w: *Writer, file_reader: *File.Reader, limit: std.io.Limit) FileError!usize {
if (File.Handle == void) return error.Unimplemented;
const a: *Allocating = @fieldParentPtr("interface", w);
const a: *Allocating = @fieldParentPtr("writer", w);
const gpa = a.allocator;
var list = a.toArrayList();
defer setArrayList(a, list);
@ -2194,14 +2194,14 @@ pub const Allocating = struct {
}
fn setArrayList(a: *Allocating, list: std.ArrayListUnmanaged(u8)) void {
a.interface.buffer = list.allocatedSlice();
a.interface.end = list.items.len;
a.writer.buffer = list.allocatedSlice();
a.writer.end = list.items.len;
}
test Allocating {
var a: Allocating = .init(std.testing.allocator);
defer a.deinit();
const w = &a.interface;
const w = &a.writer;
const x: i32 = 42;
const y: i32 = 1234;

View File

@ -8962,7 +8962,7 @@ pub fn getIntrinsic(
const name = name: {
{
var aw: Writer.Allocating = .fromArrayList(self.gpa, &self.strtab_string_bytes);
const w = &aw.interface;
const w = &aw.writer;
defer self.strtab_string_bytes = aw.toArrayList();
w.print("llvm.{s}", .{@tagName(id)}) catch return error.OutOfMemory;
for (overload) |ty| w.print(".{fm}", .{ty.fmt(self)}) catch return error.OutOfMemory;

View File

@ -3026,7 +3026,7 @@ pub fn createTypeName(
var aw: std.io.Writer.Allocating = .init(gpa);
defer aw.deinit();
const bw = &aw.interface;
const bw = &aw.writer;
bw.print("{f}(", .{block.type_name_ctx.fmt(ip)}) catch return error.OutOfMemory;
var arg_i: usize = 0;
@ -5908,7 +5908,7 @@ fn zirCompileLog(
var aw: std.io.Writer.Allocating = .init(gpa);
defer aw.deinit();
const bw = &aw.interface;
const bw = &aw.writer;
const extra = sema.code.extraData(Zir.Inst.NodeMultiOp, extended.operand);
const src_node = extra.data.src_node;
@ -37224,7 +37224,7 @@ fn notePathToComptimeAllocPtr(sema: *Sema, msg: *Zcu.ErrorMsg, src: LazySrcLoc,
const inter_name = try std.fmt.allocPrint(arena, "v{d}", .{intermediate_value_count});
const deriv_start = @import("print_value.zig").printPtrDerivation(
derivation,
&second_path_aw.interface,
&second_path_aw.writer,
pt,
.lvalue,
.{ .str = inter_name },