authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-02-02 00:22:08-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-07 17:50:09-08:00
log84d2c6dc72738bede35651a30dbcdbae3f3b30fc
tree23ae9ef9efaaf0592605d443f0bc4b48ff5d3269
parenta06a7108c88d7571a87a69f7c9bd59af49d538ca

std.MultiArrayList: popOrNull() -> pop()


1 files changed, 8 insertions(+), 16 deletions(-)

lib/std/multi_array_list.zig+8-16
......@@ -263,23 +263,15 @@ pub fn MultiArrayList(comptime T: type) type {
263263 return index;
264264 }
265265
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.
268267 /// 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;
270270 const val = self.get(self.len - 1);
271271 self.len -= 1;
272272 return val;
273273 }
274274
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
283275 /// Inserts an item into an ordered list. Shifts all elements
284276 /// after and including the specified index back by one and
285277 /// sets the given index to the specified element. May reallocate
......@@ -685,11 +677,11 @@ test "basic usage" {
685677 .b = "xnopyt",
686678 .c = 'd',
687679 });
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());
693685
694686 list.clearRetainingCapacity();
695687 try testing.expectEqual(0, list.len);