authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-11-11 00:36:58+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-11-25 09:26:32+11:00
logc0e47cb645a96e8b80ba6f93e87ad276853570e0
tree7107814acb162381f1f81a1bee02fd985e126601
parente810f485ab6395357febc85b65afddae5b061955
signaturelock-open Commit is signed but in an unrecognized format.

std: fix fifo for non-u8 types


1 files changed, 26 insertions(+), 3 deletions(-)

lib/std/fifo.zig+26-3
...@@ -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 }
230230
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 {};
234239
235 /// Make `count` bytes available before the current read location240 /// 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
349test "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}