aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-05-14 13:20:27 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-05-14 13:20:27 -0400
commit0986dcf1cf5b67ad2b2e606622bf9b2c22e01194 (patch)
tree0f2a6536a654932e15985717d2ddd78b944ccb66 /lib
parentfb947c365e95298a4619b8db2c0d40b9d69172f2 (diff)
downloadzig-0986dcf1cf5b67ad2b2e606622bf9b2c22e01194.tar.gz
zig-0986dcf1cf5b67ad2b2e606622bf9b2c22e01194.zip
self-hosted: fix codegen and resolve some analysis bugs
Diffstat (limited to 'lib')
-rw-r--r--lib/std/fifo.zig37
1 files changed, 20 insertions, 17 deletions
diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig
index 6bbec57072..de75130363 100644
--- a/lib/std/fifo.zig
+++ b/lib/std/fifo.zig
@@ -191,8 +191,8 @@ pub fn LinearFifo(
}
/// Read the next item from the fifo
- pub fn readItem(self: *Self) !T {
- if (self.count == 0) return error.EndOfStream;
+ pub fn readItem(self: *Self) ?T {
+ if (self.count == 0) return null;
const c = self.buf[self.head];
self.discard(1);
@@ -282,7 +282,10 @@ pub fn LinearFifo(
/// Write a single item to the fifo
pub fn writeItem(self: *Self, item: T) !void {
try self.ensureUnusedCapacity(1);
+ return self.writeItemAssumeCapacity(item);
+ }
+ pub fn writeItemAssumeCapacity(self: *Self, item: T) void {
var tail = self.head + self.count;
if (powers_of_two) {
tail &= self.buf.len - 1;
@@ -342,10 +345,10 @@ pub fn LinearFifo(
}
}
- /// Peek at the item at `offset`
- pub fn peekItem(self: Self, offset: usize) error{EndOfStream}!T {
- if (offset >= self.count)
- return error.EndOfStream;
+ /// Returns the item at `offset`.
+ /// Asserts offset is within bounds.
+ pub fn peekItem(self: Self, offset: usize) T {
+ assert(offset < self.count);
var index = self.head + offset;
if (powers_of_two) {
@@ -369,18 +372,18 @@ test "LinearFifo(u8, .Dynamic)" {
{
var i: usize = 0;
while (i < 5) : (i += 1) {
- try fifo.write(&[_]u8{try fifo.peekItem(i)});
+ try fifo.write(&[_]u8{fifo.peekItem(i)});
}
testing.expectEqual(@as(usize, 10), fifo.readableLength());
testing.expectEqualSlices(u8, "HELLOHELLO", fifo.readableSlice(0));
}
{
- testing.expectEqual(@as(u8, 'H'), try fifo.readItem());
- testing.expectEqual(@as(u8, 'E'), try fifo.readItem());
- testing.expectEqual(@as(u8, 'L'), try fifo.readItem());
- testing.expectEqual(@as(u8, 'L'), try fifo.readItem());
- testing.expectEqual(@as(u8, 'O'), try fifo.readItem());
+ testing.expectEqual(@as(u8, 'H'), fifo.readItem().?);
+ testing.expectEqual(@as(u8, 'E'), fifo.readItem().?);
+ testing.expectEqual(@as(u8, 'L'), fifo.readItem().?);
+ testing.expectEqual(@as(u8, 'L'), fifo.readItem().?);
+ testing.expectEqual(@as(u8, 'O'), fifo.readItem().?);
}
testing.expectEqual(@as(usize, 5), fifo.readableLength());
@@ -451,11 +454,11 @@ test "LinearFifo" {
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());
+ testing.expectEqual(@as(T, 0), fifo.readItem().?);
+ testing.expectEqual(@as(T, 1), fifo.readItem().?);
+ testing.expectEqual(@as(T, 1), fifo.readItem().?);
+ testing.expectEqual(@as(T, 0), fifo.readItem().?);
+ testing.expectEqual(@as(T, 1), fifo.readItem().?);
testing.expectEqual(@as(usize, 0), fifo.readableLength());
}