Skip to content

Commit b61d0de

Browse files
motiejusandrewrk
authored andcommitted
multi-writer: test with a non-comptime stream
Since #14819 this test failed with: $ ../../../build/stage3/bin/zig test multi_writer.zig multi_writer.zig:26:57: error: unable to evaluate comptime expression var batch = std.event.Batch(Error!void, self.streams.len, .auto_async).init(); ~~~~^~~~~~~~ referenced by: Writer: multi_writer.zig:19:52 writer: multi_writer.zig:21:36 remaining reference traces hidden; use '-freference-trace' to see all reference traces Thanks @jacobly for hints how to fix this on IRC.
1 parent b873ce1 commit b61d0de

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

lib/std/io/multi_writer.zig

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const std = @import("../std.zig");
22
const io = std.io;
3-
const testing = std.testing;
43

54
/// Takes a tuple of streams, and constructs a new stream that writes to all of them
65
pub fn MultiWriter(comptime Writers: type) type {
@@ -23,14 +22,8 @@ pub fn MultiWriter(comptime Writers: type) type {
2322
}
2423

2524
pub fn write(self: *Self, bytes: []const u8) Error!usize {
26-
var batch = std.event.Batch(Error!void, self.streams.len, .auto_async).init();
27-
comptime var i = 0;
28-
inline while (i < self.streams.len) : (i += 1) {
29-
const stream = self.streams[i];
30-
// TODO: remove ptrCast: https://github.com/ziglang/zig/issues/5258
31-
batch.add(@ptrCast(anyframe->Error!void, &async stream.writeAll(bytes)));
32-
}
33-
try batch.wait();
25+
inline for (self.streams) |stream|
26+
try stream.writeAll(bytes);
3427
return bytes.len;
3528
}
3629
};
@@ -40,13 +33,21 @@ pub fn multiWriter(streams: anytype) MultiWriter(@TypeOf(streams)) {
4033
return .{ .streams = streams };
4134
}
4235

36+
const testing = std.testing;
37+
4338
test "MultiWriter" {
39+
var tmp = testing.tmpDir(.{});
40+
defer tmp.cleanup();
41+
var f = try tmp.dir.createFile("t.txt", .{});
42+
4443
var buf1: [255]u8 = undefined;
4544
var fbs1 = io.fixedBufferStream(&buf1);
4645
var buf2: [255]u8 = undefined;
47-
var fbs2 = io.fixedBufferStream(&buf2);
48-
var stream = multiWriter(.{ fbs1.writer(), fbs2.writer() });
46+
var stream = multiWriter(.{ fbs1.writer(), f.writer() });
47+
4948
try stream.writer().print("HI", .{});
49+
f.close();
50+
5051
try testing.expectEqualSlices(u8, "HI", fbs1.getWritten());
51-
try testing.expectEqualSlices(u8, "HI", fbs2.getWritten());
52+
try testing.expectEqualSlices(u8, "HI", try tmp.dir.readFile("t.txt", &buf2));
5253
}

0 commit comments

Comments
 (0)