| ... | @@ -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 | } |
| 217 | | 217 | |
| | 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 fifo | 228 | /// 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; |
| ... | @@ -414,6 +424,15 @@ test "LinearFifo(u8, .Dynamic)" { | ... | @@ -414,6 +424,15 @@ test "LinearFifo(u8, .Dynamic)" { |
| 414 | testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]); | 424 | testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]); |
| 415 | testing.expectEqual(@as(usize, 0), fifo.readableLength()); | 425 | testing.expectEqual(@as(usize, 0), fifo.readableLength()); |
| 416 | } | 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 | } |
| 417 | } | 436 | } |
| 418 | | 437 | |
| 419 | test "LinearFifo" { | 438 | test "LinearFifo" { |