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