Commit 84d2c6dc72
Changed files (1)
lib
lib/std/multi_array_list.zig
@@ -263,23 +263,15 @@ pub fn MultiArrayList(comptime T: type) type {
return index;
}
- /// Remove and return the last element from the list.
- /// Asserts the list has at least one item.
+ /// Remove and return the last element from the list, or return `null` if list is empty.
/// Invalidates pointers to fields of the removed element.
- pub fn pop(self: *Self) T {
+ pub fn pop(self: *Self) ?T {
+ if (self.len == 0) return null;
const val = self.get(self.len - 1);
self.len -= 1;
return val;
}
- /// Remove and return the last element from the list, or
- /// return `null` if list is empty.
- /// Invalidates pointers to fields of the removed element, if any.
- pub fn popOrNull(self: *Self) ?T {
- if (self.len == 0) return null;
- return self.pop();
- }
-
/// Inserts an item into an ordered list. Shifts all elements
/// after and including the specified index back by one and
/// sets the given index to the specified element. May reallocate
@@ -685,11 +677,11 @@ test "basic usage" {
.b = "xnopyt",
.c = 'd',
});
- try testing.expectEqualStrings("xnopyt", list.pop().b);
- try testing.expectEqual(@as(?u8, 'c'), if (list.popOrNull()) |elem| elem.c else null);
- try testing.expectEqual(@as(u32, 2), list.pop().a);
- try testing.expectEqual(@as(u8, 'a'), list.pop().c);
- try testing.expectEqual(@as(?Foo, null), list.popOrNull());
+ try testing.expectEqualStrings("xnopyt", list.pop().?.b);
+ try testing.expectEqual(@as(?u8, 'c'), if (list.pop()) |elem| elem.c else null);
+ try testing.expectEqual(@as(u32, 2), list.pop().?.a);
+ try testing.expectEqual(@as(u8, 'a'), list.pop().?.c);
+ try testing.expectEqual(@as(?Foo, null), list.pop());
list.clearRetainingCapacity();
try testing.expectEqual(0, list.len);