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