| ... | ... | @@ -188,7 +188,7 @@ pub fn MultiArrayList(comptime S: type) type { |
| 188 | 188 | /// after and including the specified index back by one and |
| 189 | 189 | /// sets the given index to the specified element. May reallocate |
| 190 | 190 | /// 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 { |
| 192 | 192 | try self.ensureUnusedCapacity(gpa, 1); |
| 193 | 193 | self.insertAssumeCapacity(index, elem); |
| 194 | 194 | } |
| ... | ... | @@ -602,3 +602,22 @@ test "ensure capacity on empty list" { |
| 602 | 602 | try testing.expectEqualSlices(u32, &[_]u32{ 9, 11 }, list.items(.a)); |
| 603 | 603 | try testing.expectEqualSlices(u8, &[_]u8{ 10, 12 }, list.items(.b)); |
| 604 | 604 | } |
| 605 | |
| 606 | test "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 | } |