From 2a031c8825832fa70299c3fb772d8230de723c22 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 2 Apr 2020 20:55:59 +1100 Subject: [PATCH 1/2] std: LinearFifo matches ArrayList in always having outStream method --- lib/std/fifo.zig | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 6866cc3d7a..c97b0542b0 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -291,24 +291,16 @@ pub fn LinearFifo( return self.writeAssumeCapacity(src); } - pub usingnamespace if (T == u8) - struct { - const OutStream = std.io.OutStream(*Self, Error, appendWrite); - const Error = error{OutOfMemory}; + /// Same as `write` except it returns the number of bytes written, which is always the same + /// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API. + fn appendWrite(self: *Self, bytes: []const u8) error{OutOfMemory}!usize { + try self.write(bytes); + return bytes.len; + } - /// Same as `write` except it returns the number of bytes written, which is always the same - /// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API. - pub fn appendWrite(fifo: *Self, bytes: []const u8) Error!usize { - try fifo.write(bytes); - return bytes.len; - } - - pub fn outStream(self: *Self) OutStream { - return .{ .context = self }; - } - } - else - struct {}; + pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) { + return .{ .context = self }; + } /// Make `count` items available before the current read location fn rewind(self: *Self, count: usize) void { From 34524a179227d1492229f52b93c61405455c47e8 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 2 Apr 2020 21:14:15 +1100 Subject: [PATCH 2/2] std: add LinearFifo().inStream --- lib/std/fifo.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index c97b0542b0..6bbec57072 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -215,6 +215,16 @@ pub fn LinearFifo( return dst.len - dst_left.len; } + /// Same as `read` except it returns an error union + /// The purpose of this function existing is to match `std.io.InStream` API. + fn readFn(self: *Self, dest: []u8) error{}!usize { + return self.read(dest); + } + + pub fn inStream(self: *Self) std.io.InStream(*Self, error{}, readFn) { + return .{ .context = self }; + } + /// Returns number of items available in fifo pub fn writableLength(self: Self) usize { return self.buf.len - self.count; @@ -414,6 +424,15 @@ test "LinearFifo(u8, .Dynamic)" { testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]); testing.expectEqual(@as(usize, 0), fifo.readableLength()); } + + { + try fifo.outStream().writeAll("This is a test"); + var result: [30]u8 = undefined; + testing.expectEqualSlices(u8, "This", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?); + testing.expectEqualSlices(u8, "is", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?); + testing.expectEqualSlices(u8, "a", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?); + testing.expectEqualSlices(u8, "test", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?); + } } test "LinearFifo" {