diff --git a/lib/std/bounded_array.zig b/lib/std/bounded_array.zig index 587251458e..18328683ae 100644 --- a/lib/std/bounded_array.zig +++ b/lib/std/bounded_array.zig @@ -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);