std.Io: delete GenericWriter

This commit is contained in:
Andrew Kelley 2025-08-07 23:09:40 -07:00
parent 5cb8cdef10
commit 57dbc9e74a
17 changed files with 35 additions and 644 deletions

View File

@ -257,7 +257,7 @@ const Check = struct {
fn dumpSection(allocator: Allocator, name: [:0]const u8) Check { fn dumpSection(allocator: Allocator, name: [:0]const u8) Check {
var check = Check.create(allocator, .dump_section); var check = Check.create(allocator, .dump_section);
const off: u32 = @intCast(check.data.items.len); const off: u32 = @intCast(check.data.items.len);
check.data.writer().print("{s}\x00", .{name}) catch @panic("OOM"); check.data.print("{s}\x00", .{name}) catch @panic("OOM");
check.payload = .{ .dump_section = off }; check.payload = .{ .dump_section = off };
return check; return check;
} }
@ -1320,7 +1320,8 @@ const MachODumper = struct {
} }
bindings.deinit(); bindings.deinit();
} }
try ctx.parseBindInfo(data, &bindings); var data_reader: std.Io.Reader = .fixed(data);
try ctx.parseBindInfo(&data_reader, &bindings);
mem.sort(Binding, bindings.items, {}, Binding.lessThan); mem.sort(Binding, bindings.items, {}, Binding.lessThan);
for (bindings.items) |binding| { for (bindings.items) |binding| {
try writer.print("0x{x} [addend: {d}]", .{ binding.address, binding.addend }); try writer.print("0x{x} [addend: {d}]", .{ binding.address, binding.addend });
@ -1335,11 +1336,7 @@ const MachODumper = struct {
} }
} }
fn parseBindInfo(ctx: ObjectContext, data: []const u8, bindings: *std.array_list.Managed(Binding)) !void { fn parseBindInfo(ctx: ObjectContext, reader: *std.Io.Reader, bindings: *std.array_list.Managed(Binding)) !void {
var stream = std.io.fixedBufferStream(data);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
var seg_id: ?u8 = null; var seg_id: ?u8 = null;
var tag: Binding.Tag = .self; var tag: Binding.Tag = .self;
var ordinal: u16 = 0; var ordinal: u16 = 0;
@ -1350,7 +1347,7 @@ const MachODumper = struct {
defer name_buf.deinit(); defer name_buf.deinit();
while (true) { while (true) {
const byte = reader.readByte() catch break; const byte = reader.takeByte() catch break;
const opc = byte & macho.BIND_OPCODE_MASK; const opc = byte & macho.BIND_OPCODE_MASK;
const imm = byte & macho.BIND_IMMEDIATE_MASK; const imm = byte & macho.BIND_IMMEDIATE_MASK;
switch (opc) { switch (opc) {
@ -1371,18 +1368,17 @@ const MachODumper = struct {
}, },
macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => { macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => {
seg_id = imm; seg_id = imm;
offset = try std.leb.readUleb128(u64, reader); offset = try reader.takeLeb128(u64);
}, },
macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => { macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => {
name_buf.clearRetainingCapacity(); name_buf.clearRetainingCapacity();
try reader.readUntilDelimiterArrayList(&name_buf, 0, std.math.maxInt(u32)); try name_buf.appendSlice(try reader.takeDelimiterInclusive(0));
try name_buf.append(0);
}, },
macho.BIND_OPCODE_SET_ADDEND_SLEB => { macho.BIND_OPCODE_SET_ADDEND_SLEB => {
addend = try std.leb.readIleb128(i64, reader); addend = try reader.takeLeb128(i64);
}, },
macho.BIND_OPCODE_ADD_ADDR_ULEB => { macho.BIND_OPCODE_ADD_ADDR_ULEB => {
const x = try std.leb.readUleb128(u64, reader); const x = try reader.takeLeb128(u64);
offset = @intCast(@as(i64, @intCast(offset)) + @as(i64, @bitCast(x))); offset = @intCast(@as(i64, @intCast(offset)) + @as(i64, @bitCast(x)));
}, },
macho.BIND_OPCODE_DO_BIND, macho.BIND_OPCODE_DO_BIND,
@ -1397,14 +1393,14 @@ const MachODumper = struct {
switch (opc) { switch (opc) {
macho.BIND_OPCODE_DO_BIND => {}, macho.BIND_OPCODE_DO_BIND => {},
macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB => { macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB => {
add_addr = try std.leb.readUleb128(u64, reader); add_addr = try reader.takeLeb128(u64);
}, },
macho.BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED => { macho.BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED => {
add_addr = imm * @sizeOf(u64); add_addr = imm * @sizeOf(u64);
}, },
macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB => { macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB => {
count = try std.leb.readUleb128(u64, reader); count = try reader.takeLeb128(u64);
skip = try std.leb.readUleb128(u64, reader); skip = try reader.takeLeb128(u64);
}, },
else => unreachable, else => unreachable,
} }
@ -1621,8 +1617,9 @@ const MachODumper = struct {
var ctx = ObjectContext{ .gpa = gpa, .data = bytes, .header = hdr }; var ctx = ObjectContext{ .gpa = gpa, .data = bytes, .header = hdr };
try ctx.parse(); try ctx.parse();
var output = std.array_list.Managed(u8).init(gpa); var output: std.Io.Writer.Allocating = .init(gpa);
const writer = output.writer(); defer output.deinit();
const writer = &output.writer;
switch (check.kind) { switch (check.kind) {
.headers => { .headers => {
@ -1787,8 +1784,9 @@ const ElfDumper = struct {
try ctx.objects.append(gpa, .{ .name = name, .off = stream.pos, .len = size }); try ctx.objects.append(gpa, .{ .name = name, .off = stream.pos, .len = size });
} }
var output = std.array_list.Managed(u8).init(gpa); var output: std.Io.Writer.Allocating = .init(gpa);
const writer = output.writer(); defer output.deinit();
const writer = &output.writer;
switch (check.kind) { switch (check.kind) {
.archive_symtab => if (ctx.symtab.items.len > 0) { .archive_symtab => if (ctx.symtab.items.len > 0) {
@ -1944,8 +1942,9 @@ const ElfDumper = struct {
else => {}, else => {},
}; };
var output = std.array_list.Managed(u8).init(gpa); var output: std.Io.Writer.Allocating = .init(gpa);
const writer = output.writer(); defer output.deinit();
const writer = &output.writer;
switch (check.kind) { switch (check.kind) {
.headers => { .headers => {
@ -2398,10 +2397,10 @@ const WasmDumper = struct {
return error.UnsupportedWasmVersion; return error.UnsupportedWasmVersion;
} }
var output = std.array_list.Managed(u8).init(gpa); var output: std.Io.Writer.Allocating = .init(gpa);
defer output.deinit(); defer output.deinit();
parseAndDumpInner(step, check, bytes, &fbs, &output) catch |err| switch (err) { parseAndDumpInner(step, check, bytes, &fbs, &output.writer) catch |err| switch (err) {
error.EndOfStream => try output.appendSlice("\n<UnexpectedEndOfStream>"), error.EndOfStream => try output.writer.writeAll("\n<UnexpectedEndOfStream>"),
else => |e| return e, else => |e| return e,
}; };
return output.toOwnedSlice(); return output.toOwnedSlice();
@ -2412,10 +2411,9 @@ const WasmDumper = struct {
check: Check, check: Check,
bytes: []const u8, bytes: []const u8,
fbs: *std.io.FixedBufferStream([]const u8), fbs: *std.io.FixedBufferStream([]const u8),
output: *std.array_list.Managed(u8), writer: *std.Io.Writer,
) !void { ) !void {
const reader = fbs.reader(); const reader = fbs.reader();
const writer = output.writer();
switch (check.kind) { switch (check.kind) {
.headers => { .headers => {

View File

@ -144,19 +144,6 @@ pub fn GenericReader(
return @errorCast(self.any().readAllAlloc(allocator, max_size)); return @errorCast(self.any().readAllAlloc(allocator, max_size));
} }
pub inline fn readUntilDelimiterArrayList(
self: Self,
array_list: *std.array_list.Managed(u8),
delimiter: u8,
max_size: usize,
) (NoEofError || Allocator.Error || error{StreamTooLong})!void {
return @errorCast(self.any().readUntilDelimiterArrayList(
array_list,
delimiter,
max_size,
));
}
pub inline fn readUntilDelimiterAlloc( pub inline fn readUntilDelimiterAlloc(
self: Self, self: Self,
allocator: Allocator, allocator: Allocator,
@ -326,103 +313,8 @@ pub fn GenericReader(
}; };
} }
/// Deprecated in favor of `Writer`.
pub fn GenericWriter(
comptime Context: type,
comptime WriteError: type,
comptime writeFn: fn (context: Context, bytes: []const u8) WriteError!usize,
) type {
return struct {
context: Context,
const Self = @This();
pub const Error = WriteError;
pub inline fn write(self: Self, bytes: []const u8) Error!usize {
return writeFn(self.context, bytes);
}
pub inline fn writeAll(self: Self, bytes: []const u8) Error!void {
return @errorCast(self.any().writeAll(bytes));
}
pub inline fn print(self: Self, comptime format: []const u8, args: anytype) Error!void {
return @errorCast(self.any().print(format, args));
}
pub inline fn writeByte(self: Self, byte: u8) Error!void {
return @errorCast(self.any().writeByte(byte));
}
pub inline fn writeByteNTimes(self: Self, byte: u8, n: usize) Error!void {
return @errorCast(self.any().writeByteNTimes(byte, n));
}
pub inline fn writeBytesNTimes(self: Self, bytes: []const u8, n: usize) Error!void {
return @errorCast(self.any().writeBytesNTimes(bytes, n));
}
pub inline fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void {
return @errorCast(self.any().writeInt(T, value, endian));
}
pub inline fn writeStruct(self: Self, value: anytype) Error!void {
return @errorCast(self.any().writeStruct(value));
}
pub inline fn writeStructEndian(self: Self, value: anytype, endian: std.builtin.Endian) Error!void {
return @errorCast(self.any().writeStructEndian(value, endian));
}
pub inline fn any(self: *const Self) AnyWriter {
return .{
.context = @ptrCast(&self.context),
.writeFn = typeErasedWriteFn,
};
}
fn typeErasedWriteFn(context: *const anyopaque, bytes: []const u8) anyerror!usize {
const ptr: *const Context = @ptrCast(@alignCast(context));
return writeFn(ptr.*, bytes);
}
/// Helper for bridging to the new `Writer` API while upgrading.
pub fn adaptToNewApi(self: *const Self, buffer: []u8) Adapter {
return .{
.derp_writer = self.*,
.new_interface = .{
.buffer = buffer,
.vtable = &.{ .drain = Adapter.drain },
},
};
}
pub const Adapter = struct {
derp_writer: Self,
new_interface: Writer,
err: ?Error = null,
fn drain(w: *std.io.Writer, data: []const []const u8, splat: usize) std.io.Writer.Error!usize {
_ = splat;
const a: *@This() = @alignCast(@fieldParentPtr("new_interface", w));
const buffered = w.buffered();
if (buffered.len != 0) return w.consume(a.derp_writer.write(buffered) catch |err| {
a.err = err;
return error.WriteFailed;
});
return a.derp_writer.write(data[0]) catch |err| {
a.err = err;
return error.WriteFailed;
};
}
};
};
}
/// Deprecated in favor of `Reader`. /// Deprecated in favor of `Reader`.
pub const AnyReader = @import("Io/DeprecatedReader.zig"); pub const AnyReader = @import("Io/DeprecatedReader.zig");
/// Deprecated in favor of `Writer`.
pub const AnyWriter = @import("Io/DeprecatedWriter.zig");
/// Deprecated in favor of `Reader`. /// Deprecated in favor of `Reader`.
pub const FixedBufferStream = @import("Io/fixed_buffer_stream.zig").FixedBufferStream; pub const FixedBufferStream = @import("Io/fixed_buffer_stream.zig").FixedBufferStream;
/// Deprecated in favor of `Reader`. /// Deprecated in favor of `Reader`.
@ -434,19 +326,6 @@ pub const countingReader = @import("Io/counting_reader.zig").countingReader;
pub const tty = @import("Io/tty.zig"); pub const tty = @import("Io/tty.zig");
/// Deprecated in favor of `Writer.Discarding`.
pub const null_writer: NullWriter = .{ .context = {} };
/// Deprecated in favor of `Writer.Discarding`.
pub const NullWriter = GenericWriter(void, error{}, dummyWrite);
fn dummyWrite(context: void, data: []const u8) error{}!usize {
_ = context;
return data.len;
}
test null_writer {
null_writer.writeAll("yay" ** 10) catch |err| switch (err) {};
}
pub fn poll( pub fn poll(
gpa: Allocator, gpa: Allocator,
comptime StreamEnum: type, comptime StreamEnum: type,

View File

@ -93,100 +93,6 @@ pub fn readAllAlloc(self: Self, allocator: mem.Allocator, max_size: usize) anyer
return try array_list.toOwnedSlice(); return try array_list.toOwnedSlice();
} }
/// Deprecated: use `streamUntilDelimiter` with ArrayList's writer instead.
/// Replaces the `std.array_list.Managed` contents by reading from the stream until `delimiter` is found.
/// Does not include the delimiter in the result.
/// If the `std.array_list.Managed` length would exceed `max_size`, `error.StreamTooLong` is returned and the
/// `std.array_list.Managed` is populated with `max_size` bytes from the stream.
pub fn readUntilDelimiterArrayList(
self: Self,
array_list: *std.array_list.Managed(u8),
delimiter: u8,
max_size: usize,
) anyerror!void {
array_list.shrinkRetainingCapacity(0);
try self.streamUntilDelimiter(array_list.writer(), delimiter, max_size);
}
/// Deprecated: use `streamUntilDelimiter` with ArrayList's writer instead.
/// Allocates enough memory to read until `delimiter`. If the allocated
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
/// Caller owns returned memory.
/// If this function returns an error, the contents from the stream read so far are lost.
pub fn readUntilDelimiterAlloc(
self: Self,
allocator: mem.Allocator,
delimiter: u8,
max_size: usize,
) anyerror![]u8 {
var array_list = std.array_list.Managed(u8).init(allocator);
defer array_list.deinit();
try self.streamUntilDelimiter(array_list.writer(), delimiter, max_size);
return try array_list.toOwnedSlice();
}
/// Deprecated: use `streamUntilDelimiter` with FixedBufferStream's writer instead.
/// Reads from the stream until specified byte is found. If the buffer is not
/// large enough to hold the entire contents, `error.StreamTooLong` is returned.
/// If end-of-stream is found, `error.EndOfStream` is returned.
/// Returns a slice of the stream data, with ptr equal to `buf.ptr`. The
/// delimiter byte is written to the output buffer but is not included
/// in the returned slice.
pub fn readUntilDelimiter(self: Self, buf: []u8, delimiter: u8) anyerror![]u8 {
var fbs = std.io.fixedBufferStream(buf);
try self.streamUntilDelimiter(fbs.writer(), delimiter, fbs.buffer.len);
const output = fbs.getWritten();
buf[output.len] = delimiter; // emulating old behaviour
return output;
}
/// Deprecated: use `streamUntilDelimiter` with ArrayList's (or any other's) writer instead.
/// Allocates enough memory to read until `delimiter` or end-of-stream.
/// If the allocated memory would be greater than `max_size`, returns
/// `error.StreamTooLong`. If end-of-stream is found, returns the rest
/// of the stream. If this function is called again after that, returns
/// null.
/// Caller owns returned memory.
/// If this function returns an error, the contents from the stream read so far are lost.
pub fn readUntilDelimiterOrEofAlloc(
self: Self,
allocator: mem.Allocator,
delimiter: u8,
max_size: usize,
) anyerror!?[]u8 {
var array_list = std.array_list.Managed(u8).init(allocator);
defer array_list.deinit();
self.streamUntilDelimiter(array_list.writer(), delimiter, max_size) catch |err| switch (err) {
error.EndOfStream => if (array_list.items.len == 0) {
return null;
},
else => |e| return e,
};
return try array_list.toOwnedSlice();
}
/// Deprecated: use `streamUntilDelimiter` with FixedBufferStream's writer instead.
/// Reads from the stream until specified byte is found. If the buffer is not
/// large enough to hold the entire contents, `error.StreamTooLong` is returned.
/// If end-of-stream is found, returns the rest of the stream. If this
/// function is called again after that, returns null.
/// Returns a slice of the stream data, with ptr equal to `buf.ptr`. The
/// delimiter byte is written to the output buffer but is not included
/// in the returned slice.
pub fn readUntilDelimiterOrEof(self: Self, buf: []u8, delimiter: u8) anyerror!?[]u8 {
var fbs = std.io.fixedBufferStream(buf);
self.streamUntilDelimiter(fbs.writer(), delimiter, fbs.buffer.len) catch |err| switch (err) {
error.EndOfStream => if (fbs.getWritten().len == 0) {
return null;
},
else => |e| return e,
};
const output = fbs.getWritten();
buf[output.len] = delimiter; // emulating old behaviour
return output;
}
/// Appends to the `writer` contents by reading from the stream until `delimiter` is found. /// Appends to the `writer` contents by reading from the stream until `delimiter` is found.
/// Does not write the delimiter itself. /// Does not write the delimiter itself.
/// If `optional_max_size` is not null and amount of written bytes exceeds `optional_max_size`, /// If `optional_max_size` is not null and amount of written bytes exceeds `optional_max_size`,

View File

@ -1,114 +0,0 @@
const std = @import("../std.zig");
const assert = std.debug.assert;
const mem = std.mem;
const native_endian = @import("builtin").target.cpu.arch.endian();
context: *const anyopaque,
writeFn: *const fn (context: *const anyopaque, bytes: []const u8) anyerror!usize,
const Self = @This();
pub const Error = anyerror;
pub fn write(self: Self, bytes: []const u8) anyerror!usize {
return self.writeFn(self.context, bytes);
}
pub fn writeAll(self: Self, bytes: []const u8) anyerror!void {
var index: usize = 0;
while (index != bytes.len) {
index += try self.write(bytes[index..]);
}
}
pub fn print(self: Self, comptime format: []const u8, args: anytype) anyerror!void {
return std.fmt.format(self, format, args);
}
pub fn writeByte(self: Self, byte: u8) anyerror!void {
const array = [1]u8{byte};
return self.writeAll(&array);
}
pub fn writeByteNTimes(self: Self, byte: u8, n: usize) anyerror!void {
var bytes: [256]u8 = undefined;
@memset(bytes[0..], byte);
var remaining: usize = n;
while (remaining > 0) {
const to_write = @min(remaining, bytes.len);
try self.writeAll(bytes[0..to_write]);
remaining -= to_write;
}
}
pub fn writeBytesNTimes(self: Self, bytes: []const u8, n: usize) anyerror!void {
var i: usize = 0;
while (i < n) : (i += 1) {
try self.writeAll(bytes);
}
}
pub inline fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) anyerror!void {
var bytes: [@divExact(@typeInfo(T).int.bits, 8)]u8 = undefined;
mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian);
return self.writeAll(&bytes);
}
pub fn writeStruct(self: Self, value: anytype) anyerror!void {
// Only extern and packed structs have defined in-memory layout.
comptime assert(@typeInfo(@TypeOf(value)).@"struct".layout != .auto);
return self.writeAll(mem.asBytes(&value));
}
pub fn writeStructEndian(self: Self, value: anytype, endian: std.builtin.Endian) anyerror!void {
// TODO: make sure this value is not a reference type
if (native_endian == endian) {
return self.writeStruct(value);
} else {
var copy = value;
mem.byteSwapAllFields(@TypeOf(value), &copy);
return self.writeStruct(copy);
}
}
pub fn writeFile(self: Self, file: std.fs.File) anyerror!void {
// TODO: figure out how to adjust std lib abstractions so that this ends up
// doing sendfile or maybe even copy_file_range under the right conditions.
var buf: [4000]u8 = undefined;
while (true) {
const n = try file.readAll(&buf);
try self.writeAll(buf[0..n]);
if (n < buf.len) return;
}
}
/// Helper for bridging to the new `Writer` API while upgrading.
pub fn adaptToNewApi(self: *const Self, buffer: []u8) Adapter {
return .{
.derp_writer = self.*,
.new_interface = .{
.buffer = buffer,
.vtable = &.{ .drain = Adapter.drain },
},
};
}
pub const Adapter = struct {
derp_writer: Self,
new_interface: std.io.Writer,
err: ?Error = null,
fn drain(w: *std.io.Writer, data: []const []const u8, splat: usize) std.io.Writer.Error!usize {
_ = splat;
const a: *@This() = @alignCast(@fieldParentPtr("new_interface", w));
const buffered = w.buffered();
if (buffered.len != 0) return w.consume(a.derp_writer.write(buffered) catch |err| {
a.err = err;
return error.WriteFailed;
});
return a.derp_writer.write(data[0]) catch |err| {
a.err = err;
return error.WriteFailed;
};
}
};

View File

@ -17,7 +17,6 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
pub const GetSeekPosError = error{}; pub const GetSeekPosError = error{};
pub const Reader = io.GenericReader(*Self, ReadError, read); pub const Reader = io.GenericReader(*Self, ReadError, read);
pub const Writer = io.GenericWriter(*Self, WriteError, write);
const Self = @This(); const Self = @This();
@ -25,10 +24,6 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
return .{ .context = self }; return .{ .context = self };
} }
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
pub fn read(self: *Self, dest: []u8) ReadError!usize { pub fn read(self: *Self, dest: []u8) ReadError!usize {
const size = @min(dest.len, self.buffer.len - self.pos); const size = @min(dest.len, self.buffer.len - self.pos);
const end = self.pos + size; const end = self.pos + size;
@ -39,23 +34,6 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
return size; return size;
} }
/// If the returned number of bytes written is less than requested, the
/// buffer is full. Returns `error.NoSpaceLeft` when no bytes would be written.
/// Note: `error.NoSpaceLeft` matches the corresponding error from
/// `std.fs.File.WriteError`.
pub fn write(self: *Self, bytes: []const u8) WriteError!usize {
if (bytes.len == 0) return 0;
if (self.pos >= self.buffer.len) return error.NoSpaceLeft;
const n = @min(self.buffer.len - self.pos, bytes.len);
@memcpy(self.buffer[self.pos..][0..n], bytes[0..n]);
self.pos += n;
if (n == 0) return error.NoSpaceLeft;
return n;
}
pub fn seekTo(self: *Self, pos: u64) SeekError!void { pub fn seekTo(self: *Self, pos: u64) SeekError!void {
self.pos = @min(std.math.lossyCast(usize, pos), self.buffer.len); self.pos = @min(std.math.lossyCast(usize, pos), self.buffer.len);
} }
@ -84,10 +62,6 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
return self.pos; return self.pos;
} }
pub fn getWritten(self: Self) Buffer {
return self.buffer[0..self.pos];
}
pub fn reset(self: *Self) void { pub fn reset(self: *Self) void {
self.pos = 0; self.pos = 0;
} }
@ -117,49 +91,6 @@ fn Slice(comptime T: type) type {
} }
} }
test "output" {
var buf: [255]u8 = undefined;
var fbs = fixedBufferStream(&buf);
const stream = fbs.writer();
try stream.print("{s}{s}!", .{ "Hello", "World" });
try testing.expectEqualSlices(u8, "HelloWorld!", fbs.getWritten());
}
test "output at comptime" {
comptime {
var buf: [255]u8 = undefined;
var fbs = fixedBufferStream(&buf);
const stream = fbs.writer();
try stream.print("{s}{s}!", .{ "Hello", "World" });
try testing.expectEqualSlices(u8, "HelloWorld!", fbs.getWritten());
}
}
test "output 2" {
var buffer: [10]u8 = undefined;
var fbs = fixedBufferStream(&buffer);
try fbs.writer().writeAll("Hello");
try testing.expect(mem.eql(u8, fbs.getWritten(), "Hello"));
try fbs.writer().writeAll("world");
try testing.expect(mem.eql(u8, fbs.getWritten(), "Helloworld"));
try testing.expectError(error.NoSpaceLeft, fbs.writer().writeAll("!"));
try testing.expect(mem.eql(u8, fbs.getWritten(), "Helloworld"));
fbs.reset();
try testing.expect(fbs.getWritten().len == 0);
try testing.expectError(error.NoSpaceLeft, fbs.writer().writeAll("Hello world!"));
try testing.expect(mem.eql(u8, fbs.getWritten(), "Hello worl"));
try fbs.seekTo((try fbs.getEndPos()) + 1);
try testing.expectError(error.NoSpaceLeft, fbs.writer().writeAll("H"));
}
test "input" { test "input" {
const bytes = [_]u8{ 1, 2, 3, 4, 5, 6, 7 }; const bytes = [_]u8{ 1, 2, 3, 4, 5, 6, 7 };
var fbs = fixedBufferStream(&bytes); var fbs = fixedBufferStream(&bytes);

View File

@ -336,39 +336,6 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
try unmanaged.print(gpa, fmt, args); try unmanaged.print(gpa, fmt, args);
} }
pub const Writer = if (T != u8) void else std.io.GenericWriter(*Self, Allocator.Error, appendWrite);
/// Initializes a Writer which will append to the list.
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
/// Same as `append` except it returns the number of bytes written, which is always the same
/// as `m.len`. The purpose of this function existing is to match `std.io.GenericWriter` API.
/// Invalidates element pointers if additional memory is needed.
fn appendWrite(self: *Self, m: []const u8) Allocator.Error!usize {
try self.appendSlice(m);
return m.len;
}
pub const FixedWriter = std.io.GenericWriter(*Self, Allocator.Error, appendWriteFixed);
/// Initializes a Writer which will append to the list but will return
/// `error.OutOfMemory` rather than increasing capacity.
pub fn fixedWriter(self: *Self) FixedWriter {
return .{ .context = self };
}
/// The purpose of this function existing is to match `std.io.GenericWriter` API.
fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize {
const available_capacity = self.capacity - self.items.len;
if (m.len > available_capacity)
return error.OutOfMemory;
self.appendSliceAssumeCapacity(m);
return m.len;
}
/// Append a value to the list `n` times. /// Append a value to the list `n` times.
/// Allocates more memory as necessary. /// Allocates more memory as necessary.
/// Invalidates element pointers if additional memory is needed. /// Invalidates element pointers if additional memory is needed.
@ -1083,48 +1050,6 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
self.items.len += w.end; self.items.len += w.end;
} }
/// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub const WriterContext = struct {
self: *Self,
allocator: Allocator,
};
/// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub const Writer = if (T != u8)
@compileError("The Writer interface is only defined for ArrayList(u8) " ++
"but the given type is ArrayList(" ++ @typeName(T) ++ ")")
else
std.io.GenericWriter(WriterContext, Allocator.Error, appendWrite);
/// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub fn writer(self: *Self, gpa: Allocator) Writer {
return .{ .context = .{ .self = self, .allocator = gpa } };
}
/// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
fn appendWrite(context: WriterContext, m: []const u8) Allocator.Error!usize {
try context.self.appendSlice(context.allocator, m);
return m.len;
}
/// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub const FixedWriter = std.io.GenericWriter(*Self, Allocator.Error, appendWriteFixed);
/// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub fn fixedWriter(self: *Self) FixedWriter {
return .{ .context = self };
}
/// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize {
const available_capacity = self.capacity - self.items.len;
if (m.len > available_capacity)
return error.OutOfMemory;
self.appendSliceAssumeCapacity(m);
return m.len;
}
/// Append a value to the list `n` times. /// Append a value to the list `n` times.
/// Allocates more memory as necessary. /// Allocates more memory as necessary.
/// Invalidates element pointers if additional memory is needed. /// Invalidates element pointers if additional memory is needed.

View File

@ -108,8 +108,7 @@ pub const Base64Encoder = struct {
} }
} }
// dest must be compatible with std.io.GenericWriter's writeAll interface pub fn encodeWriter(encoder: *const Base64Encoder, dest: *std.Io.Writer, source: []const u8) !void {
pub fn encodeWriter(encoder: *const Base64Encoder, dest: anytype, source: []const u8) !void {
var chunker = window(u8, source, 3, 3); var chunker = window(u8, source, 3, 3);
while (chunker.next()) |chunk| { while (chunker.next()) |chunk| {
var temp: [5]u8 = undefined; var temp: [5]u8 = undefined;

View File

@ -801,18 +801,6 @@ fn AegisMac(comptime T: type) type {
ctx.update(msg); ctx.update(msg);
ctx.final(out); ctx.final(out);
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Mac, Error, write);
fn write(self: *Mac, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Mac) Writer {
return .{ .context = self };
}
}; };
} }

View File

@ -185,18 +185,6 @@ pub fn Blake2s(comptime out_bits: usize) type {
r.* ^= v[i] ^ v[i + 8]; r.* ^= v[i] ^ v[i + 8];
} }
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }

View File

@ -474,18 +474,6 @@ pub const Blake3 = struct {
} }
output.rootOutputBytes(out_slice); output.rootOutputBytes(out_slice);
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Blake3, Error, write);
fn write(self: *Blake3, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Blake3) Writer {
return .{ .context = self };
}
}; };
// Use named type declarations to workaround crash with anonymous structs (issue #4373). // Use named type declarations to workaround crash with anonymous structs (issue #4373).

View File

@ -4,6 +4,12 @@
//! Laid out in memory like: //! Laid out in memory like:
//! capacity |--------------------------| //! capacity |--------------------------|
//! data |-------------| //! data |-------------|
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const testing = std.testing;
data: []u8, data: []u8,
capacity: usize, capacity: usize,
allocator: Allocator, allocator: Allocator,
@ -45,12 +51,6 @@ pub fn prependSlice(self: *ArrayListReverse, data: []const u8) Error!void {
self.data.ptr = begin; self.data.ptr = begin;
} }
pub const Writer = std.io.GenericWriter(*ArrayListReverse, Error, prependSliceSize);
/// Warning: This writer writes backwards. `fn print` will NOT work as expected.
pub fn writer(self: *ArrayListReverse) Writer {
return .{ .context = self };
}
fn prependSliceSize(self: *ArrayListReverse, data: []const u8) Error!usize { fn prependSliceSize(self: *ArrayListReverse, data: []const u8) Error!usize {
try self.prependSlice(data); try self.prependSlice(data);
return data.len; return data.len;
@ -77,11 +77,6 @@ pub fn toOwnedSlice(self: *ArrayListReverse) Error![]u8 {
return new_memory; return new_memory;
} }
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const testing = std.testing;
test ArrayListReverse { test ArrayListReverse {
var b = ArrayListReverse.init(testing.allocator); var b = ArrayListReverse.init(testing.allocator);
defer b.deinit(); defer b.deinit();

View File

@ -373,18 +373,6 @@ fn Sha2x32(comptime iv: Iv32, digest_bits: comptime_int) type {
for (&d.s, v) |*dv, vv| dv.* +%= vv; for (&d.s, v) |*dv, vv| dv.* +%= vv;
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }

View File

@ -80,18 +80,6 @@ pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime default_delim
self.st.pad(); self.st.pad();
self.st.squeeze(out[0..]); self.st.squeeze(out[0..]);
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }
@ -191,18 +179,6 @@ fn ShakeLike(comptime security_level: u11, comptime default_delim: u8, comptime
pub fn fillBlock(self: *Self) void { pub fn fillBlock(self: *Self) void {
self.st.fillBlock(); self.st.fillBlock();
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }
@ -284,18 +260,6 @@ fn CShakeLike(comptime security_level: u11, comptime default_delim: u8, comptime
pub fn fillBlock(self: *Self) void { pub fn fillBlock(self: *Self) void {
self.shaker.fillBlock(); self.shaker.fillBlock();
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }
@ -390,18 +354,6 @@ fn KMacLike(comptime security_level: u11, comptime default_delim: u8, comptime r
ctx.update(msg); ctx.update(msg);
ctx.final(out); ctx.final(out);
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }
@ -482,18 +434,6 @@ fn TupleHashLike(comptime security_level: u11, comptime default_delim: u8, compt
} }
self.cshaker.squeeze(out); self.cshaker.squeeze(out);
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }

View File

@ -238,18 +238,6 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
pub fn toInt(msg: []const u8, key: *const [key_length]u8) T { pub fn toInt(msg: []const u8, key: *const [key_length]u8) T {
return State.hash(msg, key); return State.hash(msg, key);
} }
pub const Error = error{};
pub const Writer = std.io.GenericWriter(*Self, Error, write);
fn write(self: *Self, bytes: []const u8) Error!usize {
self.update(bytes);
return bytes.len;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
}; };
} }

View File

@ -1097,14 +1097,6 @@ pub fn deprecatedReader(file: File) DeprecatedReader {
return .{ .context = file }; return .{ .context = file };
} }
/// Deprecated in favor of `Writer`.
pub const DeprecatedWriter = io.GenericWriter(File, WriteError, write);
/// Deprecated in favor of `Writer`.
pub fn deprecatedWriter(file: File) DeprecatedWriter {
return .{ .context = file };
}
/// Memoizes key information about a file handle such as: /// Memoizes key information about a file handle such as:
/// * The size from calling stat, or the error that occurred therein. /// * The size from calling stat, or the error that occurred therein.
/// * The current seek position. /// * The current seek position.

View File

@ -6,7 +6,7 @@
//! The high-level `parseFromSlice` and `parseFromTokenSource` deserialize a JSON document into a Zig type. //! The high-level `parseFromSlice` and `parseFromTokenSource` deserialize a JSON document into a Zig type.
//! Parse into a dynamically-typed `Value` to load any JSON value for runtime inspection. //! Parse into a dynamically-typed `Value` to load any JSON value for runtime inspection.
//! //!
//! The low-level `writeStream` emits syntax-conformant JSON tokens to a `std.io.GenericWriter`. //! The low-level `writeStream` emits syntax-conformant JSON tokens to a `std.Io.Writer`.
//! The high-level `stringify` serializes a Zig or `Value` type into JSON. //! The high-level `stringify` serializes a Zig or `Value` type into JSON.
const builtin = @import("builtin"); const builtin = @import("builtin");

View File

@ -4230,7 +4230,7 @@ fn serveUpdateResults(s: *Server, comp: *Compilation) !void {
const decl_name = zir.nullTerminatedString(zir.getDeclaration(resolved.inst).name); const decl_name = zir.nullTerminatedString(zir.getDeclaration(resolved.inst).name);
const gop = try files.getOrPut(gpa, resolved.file); const gop = try files.getOrPut(gpa, resolved.file);
if (!gop.found_existing) try file_name_bytes.writer(gpa).print("{f}\x00", .{file.path.fmt(comp)}); if (!gop.found_existing) try file_name_bytes.print(gpa, "{f}\x00", .{file.path.fmt(comp)});
const codegen_ns = tr.decl_codegen_ns.get(tracked_inst) orelse 0; const codegen_ns = tr.decl_codegen_ns.get(tracked_inst) orelse 0;
const link_ns = tr.decl_link_ns.get(tracked_inst) orelse 0; const link_ns = tr.decl_link_ns.get(tracked_inst) orelse 0;
@ -7451,7 +7451,7 @@ const Templates = struct {
i += "_NAME".len; i += "_NAME".len;
continue; continue;
} else if (std.mem.startsWith(u8, contents[i + 1 ..], "FINGERPRINT")) { } else if (std.mem.startsWith(u8, contents[i + 1 ..], "FINGERPRINT")) {
try templates.buffer.writer().print("0x{x}", .{fingerprint.int()}); try templates.buffer.print("0x{x}", .{fingerprint.int()});
i += "_FINGERPRINT".len; i += "_FINGERPRINT".len;
continue; continue;
} else if (std.mem.startsWith(u8, contents[i + 1 ..], "ZIGVER")) { } else if (std.mem.startsWith(u8, contents[i + 1 ..], "ZIGVER")) {