| ... | ... | @@ -110,14 +110,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 110 | 110 | |
| 111 | 111 | /// Creates a copy of this ArrayList, using the same allocator. |
| 112 | 112 | pub fn clone(self: *Self) !Self { |
| 113 | | var items_copy = try self.allocator.alloc(T, self.capacity); |
| 114 | | mem.copy(T, items_copy, self.items); |
| 115 | | items_copy.len = self.items.len; |
| 116 | | return Self{ |
| 117 | | .items = items_copy, |
| 118 | | .capacity = self.capacity, |
| 119 | | .allocator = self.allocator, |
| 120 | | }; |
| 113 | var cloned = try ArrayList(T).initCapacity(self.allocator, self.capacity); |
| 114 | cloned.appendSliceAssumeCapacity(self.items); |
| 115 | return cloned; |
| 121 | 116 | } |
| 122 | 117 | |
| 123 | 118 | /// Insert `item` at index `n` by moving `list[n .. list.len]` to make room. |
| ... | ... | @@ -503,13 +498,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 503 | 498 | |
| 504 | 499 | /// Creates a copy of this ArrayList. |
| 505 | 500 | pub fn clone(self: *Self, allocator: Allocator) !Self { |
| 506 | | var items_copy = try allocator.alloc(T, self.capacity); |
| 507 | | mem.copy(T, items_copy, self.items); |
| 508 | | items_copy.len = self.items.len; |
| 509 | | return Self{ |
| 510 | | .items = items_copy, |
| 511 | | .capacity = self.capacity, |
| 512 | | }; |
| 501 | var cloned = try ArrayListUnmanaged(T).initCapacity(allocator, self.capacity); |
| 502 | cloned.appendSliceAssumeCapacity(self.items); |
| 503 | return cloned; |
| 513 | 504 | } |
| 514 | 505 | |
| 515 | 506 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` |
| ... | ... | @@ -837,8 +828,8 @@ test "std.ArrayList/ArrayListUnmanaged.clone" { |
| 837 | 828 | defer cloned.deinit(); |
| 838 | 829 | |
| 839 | 830 | try testing.expectEqualSlices(i32, array.items, cloned.items); |
| 840 | | try testing.expectEqual(array.capacity, cloned.capacity); |
| 841 | 831 | try testing.expectEqual(array.allocator, cloned.allocator); |
| 832 | try testing.expect(cloned.capacity >= array.capacity); |
| 842 | 833 | |
| 843 | 834 | array.deinit(); |
| 844 | 835 | |
| ... | ... | @@ -856,7 +847,7 @@ test "std.ArrayList/ArrayListUnmanaged.clone" { |
| 856 | 847 | defer cloned.deinit(a); |
| 857 | 848 | |
| 858 | 849 | try testing.expectEqualSlices(i32, array.items, cloned.items); |
| 859 | | try testing.expectEqual(array.capacity, cloned.capacity); |
| 850 | try testing.expect(cloned.capacity >= array.capacity); |
| 860 | 851 | |
| 861 | 852 | array.deinit(a); |
| 862 | 853 | |