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(...@@ -215,6 +215,16 @@ pub fn LinearFifo(
215 return dst.len - dst_left.len;215 return dst.len - dst_left.len;
216 }216 }
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
218 /// Returns number of items available in fifo228 /// Returns number of items available in fifo
219 pub fn writableLength(self: Self) usize {229 pub fn writableLength(self: Self) usize {
220 return self.buf.len - self.count;230 return self.buf.len - self.count;
...@@ -291,24 +301,16 @@ pub fn LinearFifo(...@@ -291,24 +301,16 @@ pub fn LinearFifo(
291 return self.writeAssumeCapacity(src);301 return self.writeAssumeCapacity(src);
292 }302 }
293303
294 pub usingnamespace if (T == u8)304 /// Same as `write` except it returns the number of bytes written, which is always the same
295 struct {305 /// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API.
296 const OutStream = std.io.OutStream(*Self, Error, appendWrite);306 fn appendWrite(self: *Self, bytes: []const u8) error{OutOfMemory}!usize {
297 const Error = error{OutOfMemory};307 try self.write(bytes);
298308 return bytes.len;
299 /// Same as `write` except it returns the number of bytes written, which is always the same309 }
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 }
305310
306 pub fn outStream(self: *Self) OutStream {311 pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) {
307 return .{ .context = self };312 return .{ .context = self };
308 }313 }
309 }
310 else
311 struct {};
312314
313 /// Make `count` items available before the current read location315 /// Make `count` items available before the current read location
314 fn rewind(self: *Self, count: usize) void {316 fn rewind(self: *Self, count: usize) void {
...@@ -422,6 +424,15 @@ test "LinearFifo(u8, .Dynamic)" {...@@ -422,6 +424,15 @@ test "LinearFifo(u8, .Dynamic)" {
422 testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]);424 testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]);
423 testing.expectEqual(@as(usize, 0), fifo.readableLength());425 testing.expectEqual(@as(usize, 0), fifo.readableLength());
424 }426 }
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 }
425}436}
426437
427test "LinearFifo" {438test "LinearFifo" {