authorgravatar for tetralux@teknik.ioTetralux <tetralux@teknik.io> 2020-03-31 04:32:31+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-31 10:18:58-04:00
logd57b5205c61d1110745484cbade5fcac51835023
treef2d9cd872191a580b110937a4ad439f38926762c
parent63409cf422c71fee64c55f2b77999ee94366400f

Fix std.fifo.LinearFifo

- Fix undeclared variable in 'writeItem' - Clarify docs of `read` regarding bytes vs. items - Normalize 'writeable' to 'writable' (the more common parlance)

1 files changed, 22 insertions(+), 8 deletions(-)

lib/std/fifo.zig+22-8
...@@ -160,7 +160,7 @@ pub fn LinearFifo(...@@ -160,7 +160,7 @@ pub fn LinearFifo(
160 return self.readableSliceMut(offset);160 return self.readableSliceMut(offset);
161 }161 }
162162
163 /// Discard first `count` bytes of readable data163 /// Discard first `count` items in the fifo
164 pub fn discard(self: *Self, count: usize) void {164 pub fn discard(self: *Self, count: usize) void {
165 assert(count <= self.count);165 assert(count <= self.count);
166 { // set old range to undefined. Note: may be wrapped around166 { // set old range to undefined. Note: may be wrapped around
...@@ -199,7 +199,7 @@ pub fn LinearFifo(...@@ -199,7 +199,7 @@ pub fn LinearFifo(
199 return c;199 return c;
200 }200 }
201201
202 /// Read data from the fifo into `dst`, returns number of bytes copied.202 /// Read data from the fifo into `dst`, returns number of items copied.
203 pub fn read(self: *Self, dst: []T) usize {203 pub fn read(self: *Self, dst: []T) usize {
204 var dst_left = dst;204 var dst_left = dst;
205205
...@@ -215,7 +215,7 @@ pub fn LinearFifo(...@@ -215,7 +215,7 @@ pub fn LinearFifo(
215 return dst.len - dst_left.len;215 return dst.len - dst_left.len;
216 }216 }
217217
218 /// Returns number of bytes available in fifo218 /// Returns number of items available in fifo
219 pub fn writableLength(self: Self) usize {219 pub fn writableLength(self: Self) usize {
220 return self.buf.len - self.count;220 return self.buf.len - self.count;
221 }221 }
...@@ -233,9 +233,9 @@ pub fn LinearFifo(...@@ -233,9 +233,9 @@ pub fn LinearFifo(
233 }233 }
234 }234 }
235235
236 /// Returns a writable buffer of at least `size` bytes, allocating memory as needed.236 /// Returns a writable buffer of at least `size` items, allocating memory as needed.
237 /// Use `fifo.update` once you've written data to it.237 /// Use `fifo.update` once you've written data to it.
238 pub fn writeableWithSize(self: *Self, size: usize) ![]T {238 pub fn writableWithSize(self: *Self, size: usize) ![]T {
239 try self.ensureUnusedCapacity(size);239 try self.ensureUnusedCapacity(size);
240240
241 // try to avoid realigning buffer241 // try to avoid realigning buffer
...@@ -247,7 +247,7 @@ pub fn LinearFifo(...@@ -247,7 +247,7 @@ pub fn LinearFifo(
247 return slice;247 return slice;
248 }248 }
249249
250 /// Update the tail location of the buffer (usually follows use of writable/writeableWithSize)250 /// Update the tail location of the buffer (usually follows use of writable/writableWithSize)
251 pub fn update(self: *Self, count: usize) void {251 pub fn update(self: *Self, count: usize) void {
252 assert(self.count + count <= self.buf.len);252 assert(self.count + count <= self.buf.len);
253 self.count += count;253 self.count += count;
...@@ -279,7 +279,7 @@ pub fn LinearFifo(...@@ -279,7 +279,7 @@ pub fn LinearFifo(
279 } else {279 } else {
280 tail %= self.buf.len;280 tail %= self.buf.len;
281 }281 }
282 self.buf[tail] = byte;282 self.buf[tail] = item;
283 self.update(1);283 self.update(1);
284 }284 }
285285
...@@ -395,7 +395,7 @@ test "LinearFifo(u8, .Dynamic)" {...@@ -395,7 +395,7 @@ test "LinearFifo(u8, .Dynamic)" {
395 }395 }
396396
397 {397 {
398 const buf = try fifo.writeableWithSize(12);398 const buf = try fifo.writableWithSize(12);
399 testing.expectEqual(@as(usize, 12), buf.len);399 testing.expectEqual(@as(usize, 12), buf.len);
400 var i: u8 = 0;400 var i: u8 = 0;
401 while (i < 10) : (i += 1) {401 while (i < 10) : (i += 1) {
...@@ -445,6 +445,20 @@ test "LinearFifo" {...@@ -445,6 +445,20 @@ test "LinearFifo" {
445 testing.expectEqual(@as(T, 1), try fifo.readItem());445 testing.expectEqual(@as(T, 1), try fifo.readItem());
446 testing.expectEqual(@as(T, 0), try fifo.readItem());446 testing.expectEqual(@as(T, 0), try fifo.readItem());
447 testing.expectEqual(@as(T, 1), try fifo.readItem());447 testing.expectEqual(@as(T, 1), try fifo.readItem());
448 testing.expectEqual(@as(usize, 0), fifo.readableLength());
449 }
450
451 {
452 try fifo.writeItem(1);
453 try fifo.writeItem(1);
454 try fifo.writeItem(1);
455 testing.expectEqual(@as(usize, 3), fifo.readableLength());
456 }
457
458 {
459 var readBuf: [3]T = undefined;
460 const n = fifo.read(&readBuf);
461 testing.expectEqual(@as(usize, 3), n); // NOTE: It should be the number of items.
448 }462 }
449 }463 }
450 }464 }