authorgravatar for si@sjbrown.co.ukSimon Brown <si@sjbrown.co.uk> 2024-05-13 19:43:48+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-05-21 13:46:05+03:00
log33d7815813cd166a35772da46d74dbb50c68b6c8
tree7c1902127db5848f55aa9b67e25c9e4f924b074e
parent28476a5ee94d311319941b54e9da66210690ce70

Implement addManyAsSlice for BoundedArray


1 files changed, 13 insertions(+), 1 deletions(-)

lib/std/bounded_array.zig+13-1
......@@ -116,13 +116,21 @@ pub fn BoundedArrayAligned(
116116 }
117117
118118 /// Resize the slice, adding `n` new elements, which have `undefined` values.
119 /// The return value is a slice pointing to the uninitialized elements.
119 /// The return value is a pointer to the array of uninitialized elements.
120120 pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment) [n]T {
121121 const prev_len = self.len;
122122 try self.resize(self.len + n);
123123 return self.slice()[prev_len..][0..n];
124124 }
125125
126 /// Resize the slice, adding `n` new elements, which have `undefined` values.
127 /// The return value is a slice pointing to the uninitialized elements.
128 pub fn addManyAsSlice(self: *Self, n: usize) error{Overflow}![]align(alignment) T {
129 const prev_len = self.len;
130 try self.resize(self.len + n);
131 return self.slice()[prev_len..][0..n];
132 }
133
126134 /// Remove and return the last element from the slice.
127135 /// Asserts the slice has at least one item.
128136 pub fn pop(self: *Self) T {
......@@ -382,6 +390,10 @@ test BoundedArray {
382390 try testing.expectEqual(swapped, 0xdd);
383391 try testing.expectEqual(a.get(0), 0xee);
384392
393 const added_slice = try a.addManyAsSlice(3);
394 try testing.expectEqual(added_slice.len, 3);
395 try testing.expectEqual(a.len, 36);
396
385397 while (a.popOrNull()) |_| {}
386398 const w = a.writer();
387399 const s = "hello, this is a test string";