| ... | ... | @@ -110,8 +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 | | const items_copy = try self.allocator.alloc(T, self.capacity); |
| 113 | var items_copy = try self.allocator.alloc(T, self.capacity); |
| 114 | 114 | mem.copy(T, items_copy, self.items); |
| 115 | items_copy.len = self.items.len; |
| 115 | 116 | return Self{ |
| 116 | 117 | .items = items_copy, |
| 117 | 118 | .capacity = self.capacity, |
| ... | ... | @@ -502,8 +503,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 502 | 503 | |
| 503 | 504 | /// Creates a copy of this ArrayList. |
| 504 | 505 | pub fn clone(self: *Self, allocator: Allocator) !Self { |
| 505 | | const items_copy = try allocator.alloc(T, self.capacity); |
| 506 | var items_copy = try allocator.alloc(T, self.capacity); |
| 506 | 507 | mem.copy(T, items_copy, self.items); |
| 508 | items_copy.len = self.items.len; |
| 507 | 509 | return Self{ |
| 508 | 510 | .items = items_copy, |
| 509 | 511 | .capacity = self.capacity, |
| ... | ... | @@ -838,14 +840,15 @@ test "std.ArrayList/ArrayListUnmanaged.clone" { |
| 838 | 840 | while (i < array.items.len) : (i += 1) { |
| 839 | 841 | try testing.expectEqual(array.items[i], cloned.items[i]); |
| 840 | 842 | } |
| 843 | try testing.expectEqual(array.items.len, cloned.items.len); |
| 841 | 844 | try testing.expectEqual(array.capacity, cloned.capacity); |
| 842 | 845 | try testing.expectEqual(array.allocator, cloned.allocator); |
| 843 | 846 | |
| 844 | 847 | array.deinit(); |
| 845 | 848 | |
| 846 | | try testing.expectEqual(cloned.items[0], -1); |
| 847 | | try testing.expectEqual(cloned.items[1], 3); |
| 848 | | try testing.expectEqual(cloned.items[2], 5); |
| 849 | try testing.expectEqual(@as(i32, -1), cloned.items[0]); |
| 850 | try testing.expectEqual(@as(i32, 3), cloned.items[1]); |
| 851 | try testing.expectEqual(@as(i32, 5), cloned.items[2]); |
| 849 | 852 | } |
| 850 | 853 | { |
| 851 | 854 | var array = ArrayListUnmanaged(i32){}; |
| ... | ... | @@ -860,13 +863,14 @@ test "std.ArrayList/ArrayListUnmanaged.clone" { |
| 860 | 863 | while (i < array.items.len) : (i += 1) { |
| 861 | 864 | try testing.expectEqual(array.items[i], cloned.items[i]); |
| 862 | 865 | } |
| 866 | try testing.expectEqual(array.items.len, cloned.items.len); |
| 863 | 867 | try testing.expectEqual(array.capacity, cloned.capacity); |
| 864 | 868 | |
| 865 | 869 | array.deinit(a); |
| 866 | 870 | |
| 867 | | try testing.expectEqual(cloned.items[0], -1); |
| 868 | | try testing.expectEqual(cloned.items[1], 3); |
| 869 | | try testing.expectEqual(cloned.items[2], 5); |
| 871 | try testing.expectEqual(@as(i32, -1), cloned.items[0]); |
| 872 | try testing.expectEqual(@as(i32, 3), cloned.items[1]); |
| 873 | try testing.expectEqual(@as(i32, 5), cloned.items[2]); |
| 870 | 874 | } |
| 871 | 875 | } |
| 872 | 876 | |