From c0e47cb645a96e8b80ba6f93e87ad276853570e0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 00:36:58 +1100 Subject: [PATCH] std: fix fifo for non-u8 types --- lib/std/fifo.zig | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 04c681fcf1..198d1f2f67 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -228,9 +228,14 @@ pub fn FixedSizeFifo(comptime T: type) type { return self.writeAssumeCapacity(src); } - pub fn print(self: *Self, comptime format: []const u8, args: ...) !void { - return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args); - } + pub usingnamespace if (T == u8) + struct { + pub fn print(self: *Self, comptime format: []const u8, args: ...) !void { + return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args); + } + } + else + struct {}; /// Make `count` bytes available before the current read location fn rewind(self: *Self, size: usize) void { @@ -340,3 +345,21 @@ test "ByteFifo" { testing.expectEqual(@as(usize, 0), fifo.readableLength()); } } + +test "FixedSizeFifo" { + inline for ([_]type{ u1, u8, u16, u64 }) |T| { + var fifo = FixedSizeFifo(T).init(debug.global_allocator); + defer fifo.deinit(); + + try fifo.write([_]T{ 0, 1, 1, 0, 1 }); + testing.expectEqual(@as(usize, 5), fifo.readableLength()); + + { + testing.expectEqual(@as(T, 0), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + testing.expectEqual(@as(T, 0), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + } + } +}