| ... | ... | @@ -160,7 +160,7 @@ pub fn LinearFifo( |
| 160 | 160 | return self.readableSliceMut(offset); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | | /// Discard first `count` bytes of readable data |
| 163 | /// Discard first `count` items in the fifo |
| 164 | 164 | pub fn discard(self: *Self, count: usize) void { |
| 165 | 165 | assert(count <= self.count); |
| 166 | 166 | { // set old range to undefined. Note: may be wrapped around |
| ... | ... | @@ -199,7 +199,7 @@ pub fn LinearFifo( |
| 199 | 199 | return c; |
| 200 | 200 | } |
| 201 | 201 | |
| 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 | 203 | pub fn read(self: *Self, dst: []T) usize { |
| 204 | 204 | var dst_left = dst; |
| 205 | 205 | |
| ... | ... | @@ -215,7 +215,7 @@ pub fn LinearFifo( |
| 215 | 215 | return dst.len - dst_left.len; |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | | /// Returns number of bytes available in fifo |
| 218 | /// Returns number of items available in fifo |
| 219 | 219 | pub fn writableLength(self: Self) usize { |
| 220 | 220 | return self.buf.len - self.count; |
| 221 | 221 | } |
| ... | ... | @@ -233,9 +233,9 @@ pub fn LinearFifo( |
| 233 | 233 | } |
| 234 | 234 | } |
| 235 | 235 | |
| 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 | 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 | 239 | try self.ensureUnusedCapacity(size); |
| 240 | 240 | |
| 241 | 241 | // try to avoid realigning buffer |
| ... | ... | @@ -247,7 +247,7 @@ pub fn LinearFifo( |
| 247 | 247 | return slice; |
| 248 | 248 | } |
| 249 | 249 | |
| 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 | 251 | pub fn update(self: *Self, count: usize) void { |
| 252 | 252 | assert(self.count + count <= self.buf.len); |
| 253 | 253 | self.count += count; |
| ... | ... | @@ -279,7 +279,7 @@ pub fn LinearFifo( |
| 279 | 279 | } else { |
| 280 | 280 | tail %= self.buf.len; |
| 281 | 281 | } |
| 282 | | self.buf[tail] = byte; |
| 282 | self.buf[tail] = item; |
| 283 | 283 | self.update(1); |
| 284 | 284 | } |
| 285 | 285 | |
| ... | ... | @@ -395,7 +395,7 @@ test "LinearFifo(u8, .Dynamic)" { |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | { |
| 398 | | const buf = try fifo.writeableWithSize(12); |
| 398 | const buf = try fifo.writableWithSize(12); |
| 399 | 399 | testing.expectEqual(@as(usize, 12), buf.len); |
| 400 | 400 | var i: u8 = 0; |
| 401 | 401 | while (i < 10) : (i += 1) { |
| ... | ... | @@ -445,6 +445,20 @@ test "LinearFifo" { |
| 445 | 445 | testing.expectEqual(@as(T, 1), try fifo.readItem()); |
| 446 | 446 | testing.expectEqual(@as(T, 0), try fifo.readItem()); |
| 447 | 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 | } |