| ... | ... | @@ -263,23 +263,15 @@ pub fn MultiArrayList(comptime T: type) type { |
| 263 | 263 | return index; |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | | /// Remove and return the last element from the list. |
| 267 | | /// Asserts the list has at least one item. |
| 266 | /// Remove and return the last element from the list, or return `null` if list is empty. |
| 268 | 267 | /// Invalidates pointers to fields of the removed element. |
| 269 | | pub fn pop(self: *Self) T { |
| 268 | pub fn pop(self: *Self) ?T { |
| 269 | if (self.len == 0) return null; |
| 270 | 270 | const val = self.get(self.len - 1); |
| 271 | 271 | self.len -= 1; |
| 272 | 272 | return val; |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | | /// Remove and return the last element from the list, or |
| 276 | | /// return `null` if list is empty. |
| 277 | | /// Invalidates pointers to fields of the removed element, if any. |
| 278 | | pub fn popOrNull(self: *Self) ?T { |
| 279 | | if (self.len == 0) return null; |
| 280 | | return self.pop(); |
| 281 | | } |
| 282 | | |
| 283 | 275 | /// Inserts an item into an ordered list. Shifts all elements |
| 284 | 276 | /// after and including the specified index back by one and |
| 285 | 277 | /// sets the given index to the specified element. May reallocate |
| ... | ... | @@ -685,11 +677,11 @@ test "basic usage" { |
| 685 | 677 | .b = "xnopyt", |
| 686 | 678 | .c = 'd', |
| 687 | 679 | }); |
| 688 | | try testing.expectEqualStrings("xnopyt", list.pop().b); |
| 689 | | try testing.expectEqual(@as(?u8, 'c'), if (list.popOrNull()) |elem| elem.c else null); |
| 690 | | try testing.expectEqual(@as(u32, 2), list.pop().a); |
| 691 | | try testing.expectEqual(@as(u8, 'a'), list.pop().c); |
| 692 | | try testing.expectEqual(@as(?Foo, null), list.popOrNull()); |
| 680 | try testing.expectEqualStrings("xnopyt", list.pop().?.b); |
| 681 | try testing.expectEqual(@as(?u8, 'c'), if (list.pop()) |elem| elem.c else null); |
| 682 | try testing.expectEqual(@as(u32, 2), list.pop().?.a); |
| 683 | try testing.expectEqual(@as(u8, 'a'), list.pop().?.c); |
| 684 | try testing.expectEqual(@as(?Foo, null), list.pop()); |
| 693 | 685 | |
| 694 | 686 | list.clearRetainingCapacity(); |
| 695 | 687 | try testing.expectEqual(0, list.len); |