aboutsummaryrefslogtreecommitdiff
path: root/lib/std/bounded_array.zig
diff options
context:
space:
mode:
authorSimon Brown <si@sjbrown.co.uk>2024-05-13 19:43:48 +0100
committerVeikka Tuominen <git@vexu.eu>2024-05-21 13:46:05 +0300
commit33d7815813cd166a35772da46d74dbb50c68b6c8 (patch)
tree7c1902127db5848f55aa9b67e25c9e4f924b074e /lib/std/bounded_array.zig
parent28476a5ee94d311319941b54e9da66210690ce70 (diff)
downloadzig-33d7815813cd166a35772da46d74dbb50c68b6c8.tar.gz
zig-33d7815813cd166a35772da46d74dbb50c68b6c8.zip
Implement addManyAsSlice for BoundedArray
Diffstat (limited to 'lib/std/bounded_array.zig')
-rw-r--r--lib/std/bounded_array.zig14
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/std/bounded_array.zig b/lib/std/bounded_array.zig
index d41857013c..59f68e8cf8 100644
--- a/lib/std/bounded_array.zig
+++ b/lib/std/bounded_array.zig
@@ -116,13 +116,21 @@ pub fn BoundedArrayAligned(
}
/// Resize the slice, adding `n` new elements, which have `undefined` values.
- /// The return value is a slice pointing to the uninitialized elements.
+ /// The return value is a pointer to the array of uninitialized elements.
pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment) [n]T {
const prev_len = self.len;
try self.resize(self.len + n);
return self.slice()[prev_len..][0..n];
}
+ /// Resize the slice, adding `n` new elements, which have `undefined` values.
+ /// The return value is a slice pointing to the uninitialized elements.
+ pub fn addManyAsSlice(self: *Self, n: usize) error{Overflow}![]align(alignment) T {
+ const prev_len = self.len;
+ try self.resize(self.len + n);
+ return self.slice()[prev_len..][0..n];
+ }
+
/// Remove and return the last element from the slice.
/// Asserts the slice has at least one item.
pub fn pop(self: *Self) T {
@@ -382,6 +390,10 @@ test BoundedArray {
try testing.expectEqual(swapped, 0xdd);
try testing.expectEqual(a.get(0), 0xee);
+ const added_slice = try a.addManyAsSlice(3);
+ try testing.expectEqual(added_slice.len, 3);
+ try testing.expectEqual(a.len, 36);
+
while (a.popOrNull()) |_| {}
const w = a.writer();
const s = "hello, this is a test string";