mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 14:25:16 +00:00
std.Io: delete MultiWriter
nah
This commit is contained in:
parent
abed0f5129
commit
d9a5a3e8c5
@ -456,9 +456,6 @@ pub const countingWriter = @import("Io/counting_writer.zig").countingWriter;
|
||||
pub const CountingReader = @import("Io/counting_reader.zig").CountingReader;
|
||||
pub const countingReader = @import("Io/counting_reader.zig").countingReader;
|
||||
|
||||
pub const MultiWriter = @import("Io/multi_writer.zig").MultiWriter;
|
||||
pub const multiWriter = @import("Io/multi_writer.zig").multiWriter;
|
||||
|
||||
pub const BitReader = @import("Io/bit_reader.zig").BitReader;
|
||||
pub const bitReader = @import("Io/bit_reader.zig").bitReader;
|
||||
|
||||
|
||||
@ -1,53 +0,0 @@
|
||||
const std = @import("../std.zig");
|
||||
const io = std.io;
|
||||
|
||||
/// Takes a tuple of streams, and constructs a new stream that writes to all of them
|
||||
pub fn MultiWriter(comptime Writers: type) type {
|
||||
comptime var ErrSet = error{};
|
||||
inline for (@typeInfo(Writers).@"struct".fields) |field| {
|
||||
const StreamType = field.type;
|
||||
ErrSet = ErrSet || StreamType.Error;
|
||||
}
|
||||
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
streams: Writers,
|
||||
|
||||
pub const Error = ErrSet;
|
||||
pub const Writer = io.GenericWriter(*Self, Error, write);
|
||||
|
||||
pub fn writer(self: *Self) Writer {
|
||||
return .{ .context = self };
|
||||
}
|
||||
|
||||
pub fn write(self: *Self, bytes: []const u8) Error!usize {
|
||||
inline for (self.streams) |stream|
|
||||
try stream.writeAll(bytes);
|
||||
return bytes.len;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn multiWriter(streams: anytype) MultiWriter(@TypeOf(streams)) {
|
||||
return .{ .streams = streams };
|
||||
}
|
||||
|
||||
const testing = std.testing;
|
||||
|
||||
test "MultiWriter" {
|
||||
var tmp = testing.tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
var f = try tmp.dir.createFile("t.txt", .{});
|
||||
|
||||
var buf1: [255]u8 = undefined;
|
||||
var fbs1 = io.fixedBufferStream(&buf1);
|
||||
var buf2: [255]u8 = undefined;
|
||||
var stream = multiWriter(.{ fbs1.writer(), f.writer() });
|
||||
|
||||
try stream.writer().print("HI", .{});
|
||||
f.close();
|
||||
|
||||
try testing.expectEqualSlices(u8, "HI", fbs1.getWritten());
|
||||
try testing.expectEqualSlices(u8, "HI", try tmp.dir.readFile("t.txt", &buf2));
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user