mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
Allow const for ArrayList.getLast, fix #14522
This commit is contained in:
parent
7f24993772
commit
b7c96c3bbd
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user