authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-17 12:29:11+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-02 21:14:59-07:00
log1e6de105c8e9dd3152526c680c33518e2502a31e
tree3376a0a2c426e8037c21404b9463eaeff3b74619
parent385b7e4808d6f1564256e9f40c903f984a49dca6

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}