aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_list.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-02-04 14:17:47 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-02-04 14:17:47 -0700
commit09cee1d5e34dd4187242a693c2d7b5a9d1e689b1 (patch)
tree70be147cdf59171bda9079abb3c5fe318ffad72b /lib/std/array_list.zig
parentfab9b7110ed1fa7bb082aad5e095047441db2b24 (diff)
parentb7c96c3bbdc0e2172cf9edb0c9c7c52f86c2311e (diff)
downloadzig-09cee1d5e34dd4187242a693c2d7b5a9d1e689b1.tar.gz
zig-09cee1d5e34dd4187242a693c2d7b5a9d1e689b1.zip
Merge remote-tracking branch 'origin/master' into llvm16
Diffstat (limited to 'lib/std/array_list.zig')
-rw-r--r--lib/std/array_list.zig32
1 files changed, 28 insertions, 4 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index b6e78b07bd..2485668417 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -482,14 +482,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
/// Return the last element from the list.
/// Asserts the list has at least one item.
- pub fn getLast(self: *Self) T {
+ pub fn getLast(self: Self) T {
const val = self.items[self.items.len - 1];
return val;
}
/// Return the last element from the list, or
/// return `null` if list is empty.
- pub fn getLastOrNull(self: *Self) ?T {
+ pub fn getLastOrNull(self: Self) ?T {
if (self.items.len == 0) return null;
return self.getLast();
}
@@ -961,14 +961,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
/// Return the last element from the list.
/// Asserts the list has at least one item.
- pub fn getLast(self: *Self) T {
+ pub fn getLast(self: Self) T {
const val = self.items[self.items.len - 1];
return val;
}
/// Return the last element from the list, or
/// return `null` if list is empty.
- pub fn getLastOrNull(self: *Self) ?T {
+ pub fn getLastOrNull(self: Self) ?T {
if (self.items.len == 0) return null;
return self.getLast();
}
@@ -1719,3 +1719,27 @@ test "std.ArrayList(?u32).popOrNull()" {
try testing.expect(list.popOrNull().? == null);
try testing.expect(list.popOrNull() == null);
}
+
+test "std.ArrayList(u32).getLast()" {
+ const a = testing.allocator;
+
+ var list = ArrayList(u32).init(a);
+ defer list.deinit();
+
+ try list.append(2);
+ const const_list = list;
+ try testing.expectEqual(const_list.getLast(), 2);
+}
+
+test "std.ArrayList(u32).getLastOrNull()" {
+ const a = testing.allocator;
+
+ var list = ArrayList(u32).init(a);
+ defer list.deinit();
+
+ try testing.expectEqual(list.getLastOrNull(), null);
+
+ try list.append(2);
+ const const_list = list;
+ try testing.expectEqual(const_list.getLastOrNull().?, 2);
+}