authorgravatar for motiejus@jakstys.ltMotiejus Jakštys <motiejus@jakstys.lt> 2023-05-19 10:28:01+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-19 09:13:49-07:00
logb61d0debd6d6018203945ae67dc54d474fbc74de
tree96afc4cf2fb78659012eaf00e36b9640e8661e5b
parentb873ce1e0e527a74fab9e55975ff1503e8f70b53

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 files changed, 13 insertions(+), 12 deletions(-)

lib/std/io/multi_writer.zig+13-12
...@@ -1,6 +1,5 @@...@@ -1,6 +1,5 @@
1const std = @import("../std.zig");1const std = @import("../std.zig");
2const io = std.io;2const io = std.io;
3const testing = std.testing;
43
5/// Takes a tuple of streams, and constructs a new stream that writes to all of them4/// Takes a tuple of streams, and constructs a new stream that writes to all of them
6pub fn MultiWriter(comptime Writers: type) type {5pub fn MultiWriter(comptime Writers: type) type {
...@@ -23,14 +22,8 @@ pub fn MultiWriter(comptime Writers: type) type {...@@ -23,14 +22,8 @@ pub fn MultiWriter(comptime Writers: type) type {
23 }22 }
2423
25 pub fn write(self: *Self, bytes: []const u8) Error!usize {24 pub fn write(self: *Self, bytes: []const u8) Error!usize {
26 var batch = std.event.Batch(Error!void, self.streams.len, .auto_async).init();25 inline for (self.streams) |stream|
27 comptime var i = 0;26 try stream.writeAll(bytes);
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();
34 return bytes.len;27 return bytes.len;
35 }28 }
36 };29 };
...@@ -40,13 +33,21 @@ pub fn multiWriter(streams: anytype) MultiWriter(@TypeOf(streams)) {...@@ -40,13 +33,21 @@ pub fn multiWriter(streams: anytype) MultiWriter(@TypeOf(streams)) {
40 return .{ .streams = streams };33 return .{ .streams = streams };
41}34}
4235
36const testing = std.testing;
37
43test "MultiWriter" {38test "MultiWriter" {
39 var tmp = testing.tmpDir(.{});
40 defer tmp.cleanup();
41 var f = try tmp.dir.createFile("t.txt", .{});
42
44 var buf1: [255]u8 = undefined;43 var buf1: [255]u8 = undefined;
45 var fbs1 = io.fixedBufferStream(&buf1);44 var fbs1 = io.fixedBufferStream(&buf1);
46 var buf2: [255]u8 = undefined;45 var buf2: [255]u8 = undefined;
47 var fbs2 = io.fixedBufferStream(&buf2);46 var stream = multiWriter(.{ fbs1.writer(), f.writer() });
48 var stream = multiWriter(.{ fbs1.writer(), fbs2.writer() });47
49 try stream.writer().print("HI", .{});48 try stream.writer().print("HI", .{});
49 f.close();
50
50 try testing.expectEqualSlices(u8, "HI", fbs1.getWritten());51 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));
52}53}