clean up some assertions

This commit is contained in:
Mitchell Hashimoto
2022-09-01 23:25:21 -07:00
parent 81b805b8c2
commit d22a323896
2 changed files with 6 additions and 7 deletions

View File

@ -566,7 +566,7 @@ fn scrollDelta(self: *Screen, delta: isize, grow: bool) !void {
if (!grow) {
self.viewport = @minimum(
self.history,
self.viewport +| @intCast(usize, delta),
self.viewport + @intCast(usize, delta),
);
return;
}
@ -574,7 +574,7 @@ fn scrollDelta(self: *Screen, delta: isize, grow: bool) !void {
// Add our delta to our viewport. If we're less than the max currently
// allowed to scroll to the bottom (the end of the history), then we
// have space and we just return.
self.viewport +|= @intCast(usize, delta);
self.viewport += @intCast(usize, delta);
if (self.viewport <= self.history) return;
// Our viewport is bigger than our max. The number of new rows we need
@ -610,10 +610,6 @@ fn scrollDelta(self: *Screen, delta: isize, grow: bool) !void {
const rows_to_delete = rows_final - self.rowsCapacity();
self.viewport -= rows_to_delete;
self.storage.deleteOldest(rows_to_delete * (self.cols + 1));
// If we grew down like this, we must be at the bottom.
assert(self.viewportIsBottom());
break :deleted rows_to_delete;
} else 0;

View File

@ -112,6 +112,8 @@ pub fn CircBuf(comptime T: type, comptime default: T) type {
/// Delete the oldest n values from the buffer. If there are less
/// than n values in the buffer, it'll delete everything.
pub fn deleteOldest(self: *Self, n: usize) void {
assert(n <= self.storage.len);
// Clear the values back to default
const slices = self.getPtrSlice(0, n);
for (slices) |slice| std.mem.set(T, slice, default);
@ -128,7 +130,8 @@ pub fn CircBuf(comptime T: type, comptime default: T) type {
/// the end of our buffer. This never "rotates" the buffer because
/// the offset can only be within the size of the buffer.
pub fn getPtrSlice(self: *Self, offset: usize, slice_len: usize) [2][]T {
assert(slice_len > 0);
// Note: this assertion is very important, it hints the compiler
// which generates ~10% faster code than without it.
assert(offset + slice_len <= self.capacity());
// End offset is the last offset (exclusive) for our slice.