| ... | ... | @@ -1391,12 +1391,19 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type { |
| 1391 | 1391 | return self.allocatedSlice()[self.items.len..]; |
| 1392 | 1392 | } |
| 1393 | 1393 | |
| 1394 | | /// Returns the last element from the list, or `null` if the list is empty. |
| 1394 | /// Deprecated in favor of `last`. |
| 1395 | 1395 | pub fn getLast(self: Self) ?T { |
| 1396 | 1396 | if (self.items.len == 0) return null; |
| 1397 | 1397 | return self.items[self.items.len - 1]; |
| 1398 | 1398 | } |
| 1399 | 1399 | |
| 1400 | /// Returns a pointer to the last element from the list, or `null` if |
| 1401 | /// the list is empty. |
| 1402 | pub fn last(self: Self) ?*T { |
| 1403 | if (self.items.len == 0) return null; |
| 1404 | return &self.items[self.items.len - 1]; |
| 1405 | } |
| 1406 | |
| 1400 | 1407 | /// Called when memory growth is necessary. Returns a capacity larger than |
| 1401 | 1408 | /// minimum that grows super-linearly. |
| 1402 | 1409 | pub fn growCapacity(minimum: usize) usize { |
| ... | ... | @@ -2378,17 +2385,16 @@ test "Managed(?u32).pop()" { |
| 2378 | 2385 | try testing.expect(list.pop() == null); |
| 2379 | 2386 | } |
| 2380 | 2387 | |
| 2381 | | test "Managed(u32).getLast()" { |
| 2388 | test "last" { |
| 2382 | 2389 | const a = testing.allocator; |
| 2383 | 2390 | |
| 2384 | | var list = Managed(u32).init(a); |
| 2385 | | defer list.deinit(); |
| 2391 | var list: ArrayList(u32) = .empty; |
| 2392 | defer list.deinit(a); |
| 2386 | 2393 | |
| 2387 | | try testing.expectEqual(list.getLast(), null); |
| 2394 | try testing.expectEqual(list.last(), null); |
| 2388 | 2395 | |
| 2389 | | try list.append(2); |
| 2390 | | const const_list = list; |
| 2391 | | try testing.expectEqual(const_list.getLast().?, 2); |
| 2396 | try list.append(a, 2); |
| 2397 | try testing.expectEqual(list.last().?.*, 2); |
| 2392 | 2398 | } |
| 2393 | 2399 | |
| 2394 | 2400 | test "return OutOfMemory when capacity would exceed maximum usize integer value" { |