authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-17 12:29:11+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-17 12:29:11+01:00
logf72a0a290735636775f83eaebf689357bee50778
tree378382b93462a2cbb400b306841473ecfccd3e09
parent97063efb69d11bfe7210555948b1f59ab6fb9d08
parent90343d1868df01899b5cceea96f680e7bca38767

Merge branch 'Jarred-Sumner-patch-1'


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

lib/std/multi_array_list.zig+20-1
......@@ -188,7 +188,7 @@ pub fn MultiArrayList(comptime S: type) type {
188188 /// after and including the specified index back by one and
189189 /// sets the given index to the specified element. May reallocate
190190 /// and invalidate iterators.
191 pub fn insert(self: *Self, gpa: Allocator, index: usize, elem: S) void {
191 pub fn insert(self: *Self, gpa: Allocator, index: usize, elem: S) !void {
192192 try self.ensureUnusedCapacity(gpa, 1);
193193 self.insertAssumeCapacity(index, elem);
194194 }
......@@ -602,3 +602,22 @@ test "ensure capacity on empty list" {
602602 try testing.expectEqualSlices(u32, &[_]u32{ 9, 11 }, list.items(.a));
603603 try testing.expectEqualSlices(u8, &[_]u8{ 10, 12 }, list.items(.b));
604604}
605
606test "insert elements" {
607 const ally = testing.allocator;
608
609 const Foo = struct {
610 a: u8,
611 b: u32,
612 };
613
614 var list = MultiArrayList(Foo){};
615 defer list.deinit(ally);
616
617 try list.insert(ally, 0, .{ .a = 1, .b = 2 });
618 try list.ensureUnusedCapacity(ally, 1);
619 list.insertAssumeCapacity(1, .{ .a = 2, .b = 3 });
620
621 try testing.expectEqualSlices(u8, &[_]u8{ 1, 2 }, list.items(.a));
622 try testing.expectEqualSlices(u32, &[_]u32{ 2, 3 }, list.items(.b));
623}