diff options
| author | Meghan Denny <hello@nektro.net> | 2025-02-02 00:22:08 -0800 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-02-07 17:50:09 -0800 |
| commit | 84d2c6dc72738bede35651a30dbcdbae3f3b30fc (patch) | |
| tree | 23ae9ef9efaaf0592605d443f0bc4b48ff5d3269 /lib/std/multi_array_list.zig | |
| parent | a06a7108c88d7571a87a69f7c9bd59af49d538ca (diff) | |
| download | zig-84d2c6dc72738bede35651a30dbcdbae3f3b30fc.tar.gz zig-84d2c6dc72738bede35651a30dbcdbae3f3b30fc.zip | |
std.MultiArrayList: popOrNull() -> pop()
Diffstat (limited to 'lib/std/multi_array_list.zig')
| -rw-r--r-- | lib/std/multi_array_list.zig | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig index f8d32a42d2..44eea7d57e 100644 --- a/lib/std/multi_array_list.zig +++ b/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); |
