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(
160160 return self.readableSliceMut(offset);
161161 }
162162
163 /// Discard first `count` bytes of readable data
163 /// Discard first `count` items in the fifo
164164 pub fn discard(self: *Self, count: usize) void {
165165 assert(count <= self.count);
166166 { // set old range to undefined. Note: may be wrapped around
......@@ -199,7 +199,7 @@ pub fn LinearFifo(
199199 return c;
200200 }
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.
203203 pub fn read(self: *Self, dst: []T) usize {
204204 var dst_left = dst;
205205
......@@ -215,7 +215,7 @@ pub fn LinearFifo(
215215 return dst.len - dst_left.len;
216216 }
217217
218 /// Returns number of bytes available in fifo
218 /// Returns number of items available in fifo
219219 pub fn writableLength(self: Self) usize {
220220 return self.buf.len - self.count;
221221 }
......@@ -233,9 +233,9 @@ pub fn LinearFifo(
233233 }
234234 }
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.
237237 /// 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 {
239239 try self.ensureUnusedCapacity(size);
240240
241241 // try to avoid realigning buffer
......@@ -247,7 +247,7 @@ pub fn LinearFifo(
247247 return slice;
248248 }
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)
251251 pub fn update(self: *Self, count: usize) void {
252252 assert(self.count + count <= self.buf.len);
253253 self.count += count;
......@@ -279,7 +279,7 @@ pub fn LinearFifo(
279279 } else {
280280 tail %= self.buf.len;
281281 }
282 self.buf[tail] = byte;
282 self.buf[tail] = item;
283283 self.update(1);
284284 }
285285
......@@ -395,7 +395,7 @@ test "LinearFifo(u8, .Dynamic)" {
395395 }
396396
397397 {
398 const buf = try fifo.writeableWithSize(12);
398 const buf = try fifo.writableWithSize(12);
399399 testing.expectEqual(@as(usize, 12), buf.len);
400400 var i: u8 = 0;
401401 while (i < 10) : (i += 1) {
......@@ -445,6 +445,20 @@ test "LinearFifo" {
445445 testing.expectEqual(@as(T, 1), try fifo.readItem());
446446 testing.expectEqual(@as(T, 0), try fifo.readItem());
447447 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.
448462 }
449463 }
450464 }