BoundedArray: add appendAssumeCapacity

This commit is contained in:
Gregory Anders
2021-11-10 14:36:08 -07:00
committed by Andrew Kelley
parent 46af3a320c
commit b26b72f540

View File

@@ -169,6 +169,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
new_item_ptr.* = item;
}
/// Extend the slice by 1 element, asserting the capacity is already
/// enough to store the new item.
pub fn appendAssumeCapacity(self: *Self, item: T) void {
const new_item_ptr = self.addOneAssumeCapacity();
new_item_ptr.* = item;
}
/// Remove the element at index `i`, shift elements after index
/// `i` forward, and return the removed element.
/// Asserts the slice has at least one item.
@@ -262,6 +269,10 @@ test "BoundedArray" {
try testing.expectEqual(a.len, 6);
try testing.expectEqual(a.pop(), 0xff);
a.appendAssumeCapacity(0xff);
try testing.expectEqual(a.len, 6);
try testing.expectEqual(a.pop(), 0xff);
try a.resize(1);
try testing.expectEqual(a.popOrNull(), 0);
try testing.expectEqual(a.popOrNull(), null);