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