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