std: fix fifo for non-u8 types

This commit is contained in:
daurnimator 2019-11-11 00:36:58 +11:00
parent e810f485ab
commit c0e47cb645
No known key found for this signature in database
GPG Key ID: 45B429A8F9D9D22A

View File

@ -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());
}
}
}