| ... | @@ -228,9 +228,14 @@ pub fn FixedSizeFifo(comptime T: type) type { | ... | @@ -228,9 +228,14 @@ pub fn FixedSizeFifo(comptime T: type) type { |
| 228 | return self.writeAssumeCapacity(src); | 228 | return self.writeAssumeCapacity(src); |
| 229 | } | 229 | } |
| 230 | | 230 | |
| 231 | pub fn print(self: *Self, comptime format: []const u8, args: ...) !void { | 231 | pub usingnamespace if (T == u8) |
| 232 | return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args); | 232 | struct { |
| 233 | } | 233 | pub fn print(self: *Self, comptime format: []const u8, args: ...) !void { |
| | 234 | return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args); |
| | 235 | } |
| | 236 | } |
| | 237 | else |
| | 238 | struct {}; |
| 234 | | 239 | |
| 235 | /// Make `count` bytes available before the current read location | 240 | /// Make `count` bytes available before the current read location |
| 236 | fn rewind(self: *Self, size: usize) void { | 241 | fn rewind(self: *Self, size: usize) void { |
| ... | @@ -340,3 +345,21 @@ test "ByteFifo" { | ... | @@ -340,3 +345,21 @@ test "ByteFifo" { |
| 340 | testing.expectEqual(@as(usize, 0), fifo.readableLength()); | 345 | testing.expectEqual(@as(usize, 0), fifo.readableLength()); |
| 341 | } | 346 | } |
| 342 | } | 347 | } |
| | 348 | |
| | 349 | test "FixedSizeFifo" { |
| | 350 | inline for ([_]type{ u1, u8, u16, u64 }) |T| { |
| | 351 | var fifo = FixedSizeFifo(T).init(debug.global_allocator); |
| | 352 | defer fifo.deinit(); |
| | 353 | |
| | 354 | try fifo.write([_]T{ 0, 1, 1, 0, 1 }); |
| | 355 | testing.expectEqual(@as(usize, 5), fifo.readableLength()); |
| | 356 | |
| | 357 | { |
| | 358 | testing.expectEqual(@as(T, 0), try fifo.readItem()); |
| | 359 | testing.expectEqual(@as(T, 1), try fifo.readItem()); |
| | 360 | testing.expectEqual(@as(T, 1), try fifo.readItem()); |
| | 361 | testing.expectEqual(@as(T, 0), try fifo.readItem()); |
| | 362 | testing.expectEqual(@as(T, 1), try fifo.readItem()); |
| | 363 | } |
| | 364 | } |
| | 365 | } |