authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-02 12:13:44-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-04-02 12:13:44-04:00
log503c4207972b1357ac11c6a6fb2e49958b68c2dc
tree7ba53074b9747455d10f12016918f1c35a6ec81d
parentdeef063bbfd415c031f7281908805bf6f3920fdb
parent34524a179227d1492229f52b93c61405455c47e8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4908 from daurnimator/fifo-refactor

Modernize LinearFifo for new stream conventions

1 files changed, 28 insertions(+), 17 deletions(-)

lib/std/fifo.zig+28-17
......@@ -215,6 +215,16 @@ pub fn LinearFifo(
215215 return dst.len - dst_left.len;
216216 }
217217
218 /// Same as `read` except it returns an error union
219 /// The purpose of this function existing is to match `std.io.InStream` API.
220 fn readFn(self: *Self, dest: []u8) error{}!usize {
221 return self.read(dest);
222 }
223
224 pub fn inStream(self: *Self) std.io.InStream(*Self, error{}, readFn) {
225 return .{ .context = self };
226 }
227
218228 /// Returns number of items available in fifo
219229 pub fn writableLength(self: Self) usize {
220230 return self.buf.len - self.count;
......@@ -291,24 +301,16 @@ pub fn LinearFifo(
291301 return self.writeAssumeCapacity(src);
292302 }
293303
294 pub usingnamespace if (T == u8)
295 struct {
296 const OutStream = std.io.OutStream(*Self, Error, appendWrite);
297 const Error = error{OutOfMemory};
298
299 /// Same as `write` except it returns the number of bytes written, which is always the same
300 /// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API.
301 pub fn appendWrite(fifo: *Self, bytes: []const u8) Error!usize {
302 try fifo.write(bytes);
303 return bytes.len;
304 }
304 /// Same as `write` except it returns the number of bytes written, which is always the same
305 /// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API.
306 fn appendWrite(self: *Self, bytes: []const u8) error{OutOfMemory}!usize {
307 try self.write(bytes);
308 return bytes.len;
309 }
305310
306 pub fn outStream(self: *Self) OutStream {
307 return .{ .context = self };
308 }
309 }
310 else
311 struct {};
311 pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) {
312 return .{ .context = self };
313 }
312314
313315 /// Make `count` items available before the current read location
314316 fn rewind(self: *Self, count: usize) void {
......@@ -422,6 +424,15 @@ test "LinearFifo(u8, .Dynamic)" {
422424 testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]);
423425 testing.expectEqual(@as(usize, 0), fifo.readableLength());
424426 }
427
428 {
429 try fifo.outStream().writeAll("This is a test");
430 var result: [30]u8 = undefined;
431 testing.expectEqualSlices(u8, "This", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?);
432 testing.expectEqualSlices(u8, "is", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?);
433 testing.expectEqualSlices(u8, "a", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?);
434 testing.expectEqualSlices(u8, "test", (try fifo.inStream().readUntilDelimiterOrEof(&result, ' ')).?);
435 }
425436}
426437
427438test "LinearFifo" {