| ... | @@ -219,7 +219,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -219,7 +219,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 219 | assert(new_len <= self.items.len); | 219 | assert(new_len <= self.items.len); |
| 220 | | 220 | |
| 221 | self.items = self.allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) { | 221 | self.items = self.allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) { |
| 222 | error.OutOfMemory => return, // no problem, capacity is still correct then. | 222 | error.OutOfMemory => { // no problem, capacity is still correct then. |
| | 223 | self.items.len = new_len; |
| | 224 | return; |
| | 225 | }, |
| 223 | }; | 226 | }; |
| 224 | self.capacity = new_len; | 227 | self.capacity = new_len; |
| 225 | } | 228 | } |
| ... | @@ -511,3 +514,18 @@ test "std.ArrayList(u8) implements outStream" { | ... | @@ -511,3 +514,18 @@ test "std.ArrayList(u8) implements outStream" { |
| 511 | | 514 | |
| 512 | testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.span()); | 515 | testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.span()); |
| 513 | } | 516 | } |
| | 517 | |
| | 518 | test "std.ArrayList.shrink still sets length on error.OutOfMemory" { |
| | 519 | // use an arena allocator to make sure realloc returns error.OutOfMemory |
| | 520 | var arena = std.heap.ArenaAllocator.init(testing.allocator); |
| | 521 | defer arena.deinit(); |
| | 522 | |
| | 523 | var list = ArrayList(i32).init(&arena.allocator); |
| | 524 | |
| | 525 | try list.append(1); |
| | 526 | try list.append(2); |
| | 527 | try list.append(3); |
| | 528 | |
| | 529 | list.shrink(1); |
| | 530 | testing.expect(list.items.len == 1); |
| | 531 | } |