Commit b26b72f540

Gregory Anders <greg@gpanders.com>
2021-11-10 22:36:08
BoundedArray: add appendAssumeCapacity
1 parent 46af3a3
Changed files (1)
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);