| ... | @@ -482,14 +482,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -482,14 +482,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 482 | | 482 | |
| 483 | /// Return the last element from the list. | 483 | /// Return the last element from the list. |
| 484 | /// Asserts the list has at least one item. | 484 | /// Asserts the list has at least one item. |
| 485 | pub fn getLast(self: *Self) T { | 485 | pub fn getLast(self: Self) T { |
| 486 | const val = self.items[self.items.len - 1]; | 486 | const val = self.items[self.items.len - 1]; |
| 487 | return val; | 487 | return val; |
| 488 | } | 488 | } |
| 489 | | 489 | |
| 490 | /// Return the last element from the list, or | 490 | /// Return the last element from the list, or |
| 491 | /// return `null` if list is empty. | 491 | /// return `null` if list is empty. |
| 492 | pub fn getLastOrNull(self: *Self) ?T { | 492 | pub fn getLastOrNull(self: Self) ?T { |
| 493 | if (self.items.len == 0) return null; | 493 | if (self.items.len == 0) return null; |
| 494 | return self.getLast(); | 494 | return self.getLast(); |
| 495 | } | 495 | } |
| ... | @@ -961,14 +961,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -961,14 +961,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 961 | | 961 | |
| 962 | /// Return the last element from the list. | 962 | /// Return the last element from the list. |
| 963 | /// Asserts the list has at least one item. | 963 | /// Asserts the list has at least one item. |
| 964 | pub fn getLast(self: *Self) T { | 964 | pub fn getLast(self: Self) T { |
| 965 | const val = self.items[self.items.len - 1]; | 965 | const val = self.items[self.items.len - 1]; |
| 966 | return val; | 966 | return val; |
| 967 | } | 967 | } |
| 968 | | 968 | |
| 969 | /// Return the last element from the list, or | 969 | /// Return the last element from the list, or |
| 970 | /// return `null` if list is empty. | 970 | /// return `null` if list is empty. |
| 971 | pub fn getLastOrNull(self: *Self) ?T { | 971 | pub fn getLastOrNull(self: Self) ?T { |
| 972 | if (self.items.len == 0) return null; | 972 | if (self.items.len == 0) return null; |
| 973 | return self.getLast(); | 973 | return self.getLast(); |
| 974 | } | 974 | } |
| ... | @@ -1719,3 +1719,27 @@ test "std.ArrayList(?u32).popOrNull()" { | ... | @@ -1719,3 +1719,27 @@ test "std.ArrayList(?u32).popOrNull()" { |
| 1719 | try testing.expect(list.popOrNull().? == null); | 1719 | try testing.expect(list.popOrNull().? == null); |
| 1720 | try testing.expect(list.popOrNull() == null); | 1720 | try testing.expect(list.popOrNull() == null); |
| 1721 | } | 1721 | } |
| | 1722 | |
| | 1723 | test "std.ArrayList(u32).getLast()" { |
| | 1724 | const a = testing.allocator; |
| | 1725 | |
| | 1726 | var list = ArrayList(u32).init(a); |
| | 1727 | defer list.deinit(); |
| | 1728 | |
| | 1729 | try list.append(2); |
| | 1730 | const const_list = list; |
| | 1731 | try testing.expectEqual(const_list.getLast(), 2); |
| | 1732 | } |
| | 1733 | |
| | 1734 | test "std.ArrayList(u32).getLastOrNull()" { |
| | 1735 | const a = testing.allocator; |
| | 1736 | |
| | 1737 | var list = ArrayList(u32).init(a); |
| | 1738 | defer list.deinit(); |
| | 1739 | |
| | 1740 | try testing.expectEqual(list.getLastOrNull(), null); |
| | 1741 | |
| | 1742 | try list.append(2); |
| | 1743 | const const_list = list; |
| | 1744 | try testing.expectEqual(const_list.getLastOrNull().?, 2); |
| | 1745 | } |