diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-07-31 21:54:07 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-08-11 15:52:49 -0700 |
| commit | 749f10af49022597d873d41df5c600e97e5c4a37 (patch) | |
| tree | cb6da80d28fa284bdeb7b40d26ce8de9ca9b2306 /lib/std/array_list.zig | |
| parent | d625158354a02a18e9ae7975a144f30838884d5c (diff) | |
| download | zig-749f10af49022597d873d41df5c600e97e5c4a37.tar.gz zig-749f10af49022597d873d41df5c600e97e5c4a37.zip | |
std.ArrayList: make unmanaged the default
Diffstat (limited to 'lib/std/array_list.zig')
| -rw-r--r-- | lib/std/array_list.zig | 223 |
1 files changed, 103 insertions, 120 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 0542fccc68..2272490996 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -5,27 +5,18 @@ const testing = std.testing; const mem = std.mem; const math = std.math; const Allocator = mem.Allocator; +const ArrayList = std.ArrayList; -/// A contiguous, growable list of items in memory. -/// This is a wrapper around an array of T values. Initialize with `init`. -/// -/// This struct internally stores a `std.mem.Allocator` for memory management. -/// To manually specify an allocator with each function call see `ArrayListUnmanaged`. -pub fn ArrayList(comptime T: type) type { - return ArrayListAligned(T, null); +/// Deprecated. +pub fn Managed(comptime T: type) type { + return AlignedManaged(T, null); } -/// A contiguous, growable list of arbitrarily aligned items in memory. -/// This is a wrapper around an array of T values aligned to `alignment`-byte -/// addresses. If the specified alignment is `null`, then `@alignOf(T)` is used. -/// Initialize with `init`. -/// -/// This struct internally stores a `std.mem.Allocator` for memory management. -/// To manually specify an allocator with each function call see `ArrayListAlignedUnmanaged`. -pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) type { +/// Deprecated. +pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type { if (alignment) |a| { if (a.toByteUnits() == @alignOf(T)) { - return ArrayListAligned(T, null); + return AlignedManaged(T, null); } } return struct { @@ -96,11 +87,11 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty }; } - /// Initializes an ArrayListUnmanaged with the `items` and `capacity` fields + /// Initializes an ArrayList with the `items` and `capacity` fields /// of this ArrayList. Empties this ArrayList. - pub fn moveToUnmanaged(self: *Self) ArrayListAlignedUnmanaged(T, alignment) { + pub fn moveToUnmanaged(self: *Self) Aligned(T, alignment) { const allocator = self.allocator; - const result: ArrayListAlignedUnmanaged(T, alignment) = .{ .items = self.items, .capacity = self.capacity }; + const result: Aligned(T, alignment) = .{ .items = self.items, .capacity = self.capacity }; self.* = init(allocator); return result; } @@ -181,7 +172,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty // a new buffer and doing our own copy. With a realloc() call, // the allocator implementation would pointlessly copy our // extra capacity. - const new_capacity = ArrayListAlignedUnmanaged(T, alignment).growCapacity(self.capacity, new_len); + const new_capacity = Aligned(T, alignment).growCapacity(self.capacity, new_len); const old_memory = self.allocatedSlice(); if (self.allocator.remap(old_memory, new_capacity)) |new_memory| { self.items.ptr = new_memory.ptr; @@ -449,7 +440,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty if (self.capacity >= new_capacity) return; - const better_capacity = ArrayListAlignedUnmanaged(T, alignment).growCapacity(self.capacity, new_capacity); + const better_capacity = Aligned(T, alignment).growCapacity(self.capacity, new_capacity); return self.ensureTotalCapacityPrecise(better_capacity); } @@ -597,14 +588,6 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty }; } -/// An ArrayList, but the allocator is passed as a parameter to the relevant functions -/// rather than stored in the struct itself. The same allocator must be used throughout -/// the entire lifetime of an ArrayListUnmanaged. Initialize directly or with -/// `initCapacity`, and deinitialize with `deinit` or use `toOwnedSlice`. -pub fn ArrayListUnmanaged(comptime T: type) type { - return ArrayListAlignedUnmanaged(T, null); -} - /// A contiguous, growable list of arbitrarily aligned items in memory. /// This is a wrapper around an array of T values aligned to `alignment`-byte /// addresses. If the specified alignment is `null`, then `@alignOf(T)` is used. @@ -614,10 +597,10 @@ pub fn ArrayListUnmanaged(comptime T: type) type { /// or use `toOwnedSlice`. /// /// Default initialization of this struct is deprecated; use `.empty` instead. -pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alignment) type { +pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type { if (alignment) |a| { if (a.toByteUnits() == @alignOf(T)) { - return ArrayListAlignedUnmanaged(T, null); + return Aligned(T, null); } } return struct { @@ -675,11 +658,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig /// Convert this list into an analogous memory-managed one. /// The returned list has ownership of the underlying memory. - pub fn toManaged(self: *Self, gpa: Allocator) ArrayListAligned(T, alignment) { + pub fn toManaged(self: *Self, gpa: Allocator) AlignedManaged(T, alignment) { return .{ .items = self.items, .capacity = self.capacity, .allocator = gpa }; } - /// ArrayListUnmanaged takes ownership of the passed in slice. + /// ArrayList takes ownership of the passed in slice. /// Deinitialize with `deinit` or use `toOwnedSlice`. pub fn fromOwnedSlice(slice: Slice) Self { return Self{ @@ -688,7 +671,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig }; } - /// ArrayListUnmanaged takes ownership of the passed in slice. + /// ArrayList takes ownership of the passed in slice. /// Deinitialize with `deinit` or use `toOwnedSlice`. pub fn fromOwnedSliceSentinel(comptime sentinel: T, slice: [:sentinel]T) Self { return Self{ @@ -1444,7 +1427,7 @@ fn addOrOom(a: usize, b: usize) error{OutOfMemory}!usize { test "init" { { - var list = ArrayList(i32).init(testing.allocator); + var list = Managed(i32).init(testing.allocator); defer list.deinit(); try testing.expect(list.items.len == 0); @@ -1452,7 +1435,7 @@ test "init" { } { - const list: ArrayListUnmanaged(i32) = .empty; + const list: ArrayList(i32) = .empty; try testing.expect(list.items.len == 0); try testing.expect(list.capacity == 0); @@ -1462,13 +1445,13 @@ test "init" { test "initCapacity" { const a = testing.allocator; { - var list = try ArrayList(i8).initCapacity(a, 200); + var list = try Managed(i8).initCapacity(a, 200); defer list.deinit(); try testing.expect(list.items.len == 0); try testing.expect(list.capacity >= 200); } { - var list = try ArrayListUnmanaged(i8).initCapacity(a, 200); + var list = try ArrayList(i8).initCapacity(a, 200); defer list.deinit(a); try testing.expect(list.items.len == 0); try testing.expect(list.capacity >= 200); @@ -1478,7 +1461,7 @@ test "initCapacity" { test "clone" { const a = testing.allocator; { - var array = ArrayList(i32).init(a); + var array = Managed(i32).init(a); try array.append(-1); try array.append(3); try array.append(5); @@ -1497,7 +1480,7 @@ test "clone" { try testing.expectEqual(@as(i32, 5), cloned.items[2]); } { - var array: ArrayListUnmanaged(i32) = .empty; + var array: ArrayList(i32) = .empty; try array.append(a, -1); try array.append(a, 3); try array.append(a, 5); @@ -1519,7 +1502,7 @@ test "clone" { test "basic" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); { @@ -1569,7 +1552,7 @@ test "basic" { try testing.expect(list.pop() == 33); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); { @@ -1623,7 +1606,7 @@ test "basic" { test "appendNTimes" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendNTimes(2, 10); @@ -1633,7 +1616,7 @@ test "appendNTimes" { } } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendNTimes(a, 2, 10); @@ -1647,12 +1630,12 @@ test "appendNTimes" { test "appendNTimes with failing allocator" { const a = testing.failing_allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10)); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try testing.expectError(error.OutOfMemory, list.appendNTimes(a, 2, 10)); } @@ -1661,7 +1644,7 @@ test "appendNTimes with failing allocator" { test "orderedRemove" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.append(1); @@ -1687,7 +1670,7 @@ test "orderedRemove" { try testing.expectEqual(@as(usize, 4), list.items.len); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.append(a, 1); @@ -1714,7 +1697,7 @@ test "orderedRemove" { } { // remove last item - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.append(1); try testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); @@ -1722,7 +1705,7 @@ test "orderedRemove" { } { // remove last item - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.append(a, 1); try testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); @@ -1733,7 +1716,7 @@ test "orderedRemove" { test "swapRemove" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.append(1); @@ -1759,7 +1742,7 @@ test "swapRemove" { try testing.expect(list.items.len == 4); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.append(a, 1); @@ -1789,7 +1772,7 @@ test "swapRemove" { test "insert" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.insert(0, 1); @@ -1802,7 +1785,7 @@ test "insert" { try testing.expect(list.items[3] == 3); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.insert(a, 0, 1); @@ -1819,7 +1802,7 @@ test "insert" { test "insertSlice" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.append(1); @@ -1840,7 +1823,7 @@ test "insertSlice" { try testing.expect(list.items[0] == 1); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.append(a, 1); @@ -1862,11 +1845,11 @@ test "insertSlice" { } } -test "ArrayList.replaceRange" { +test "Managed.replaceRange" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1875,7 +1858,7 @@ test "ArrayList.replaceRange" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }, list.items); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1888,7 +1871,7 @@ test "ArrayList.replaceRange" { ); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1897,7 +1880,7 @@ test "ArrayList.replaceRange" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 4, 5 }, list.items); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1906,7 +1889,7 @@ test "ArrayList.replaceRange" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 5 }, list.items); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1916,11 +1899,11 @@ test "ArrayList.replaceRange" { } } -test "ArrayList.replaceRangeAssumeCapacity" { +test "Managed.replaceRangeAssumeCapacity" { const a = testing.allocator; { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1929,7 +1912,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }, list.items); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1942,7 +1925,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { ); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1951,7 +1934,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 4, 5 }, list.items); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1960,7 +1943,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 5 }, list.items); } { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); @@ -1970,11 +1953,11 @@ test "ArrayList.replaceRangeAssumeCapacity" { } } -test "ArrayListUnmanaged.replaceRange" { +test "ArrayList.replaceRange" { const a = testing.allocator; { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -1983,7 +1966,7 @@ test "ArrayListUnmanaged.replaceRange" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }, list.items); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -1996,7 +1979,7 @@ test "ArrayListUnmanaged.replaceRange" { ); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2005,7 +1988,7 @@ test "ArrayListUnmanaged.replaceRange" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 4, 5 }, list.items); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2014,7 +1997,7 @@ test "ArrayListUnmanaged.replaceRange" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 5 }, list.items); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2024,11 +2007,11 @@ test "ArrayListUnmanaged.replaceRange" { } } -test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { +test "ArrayList.replaceRangeAssumeCapacity" { const a = testing.allocator; { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2037,7 +2020,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }, list.items); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2050,7 +2033,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { ); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2059,7 +2042,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 4, 5 }, list.items); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2068,7 +2051,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 5 }, list.items); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); @@ -2080,15 +2063,15 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { const Item = struct { integer: i32, - sub_items: ArrayList(Item), + sub_items: Managed(Item), }; const ItemUnmanaged = struct { integer: i32, - sub_items: ArrayListUnmanaged(ItemUnmanaged), + sub_items: ArrayList(ItemUnmanaged), }; -test "ArrayList(T) of struct T" { +test "Managed(T) of struct T" { const a = std.testing.allocator; { var root = Item{ .integer = 1, .sub_items = .init(a) }; @@ -2104,11 +2087,11 @@ test "ArrayList(T) of struct T" { } } -test "ArrayList(u8) implements writer" { +test "Managed(u8) implements writer" { const a = testing.allocator; { - var buffer = ArrayList(u8).init(a); + var buffer = Managed(u8).init(a); defer buffer.deinit(); const x: i32 = 42; @@ -2118,7 +2101,7 @@ test "ArrayList(u8) implements writer" { try testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.items); } { - var list = ArrayListAligned(u8, .@"2").init(a); + var list = AlignedManaged(u8, .@"2").init(a); defer list.deinit(); const writer = list.writer(); @@ -2131,11 +2114,11 @@ test "ArrayList(u8) implements writer" { } } -test "ArrayListUnmanaged(u8) implements writer" { +test "ArrayList(u8) implements writer" { const a = testing.allocator; { - var buffer: ArrayListUnmanaged(u8) = .empty; + var buffer: ArrayList(u8) = .empty; defer buffer.deinit(a); const x: i32 = 42; @@ -2145,7 +2128,7 @@ test "ArrayListUnmanaged(u8) implements writer" { try testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.items); } { - var list: ArrayListAlignedUnmanaged(u8, .@"2") = .empty; + var list: Aligned(u8, .@"2") = .empty; defer list.deinit(a); const writer = list.writer(a); @@ -2163,7 +2146,7 @@ test "shrink still sets length when resizing is disabled" { const a = failing_allocator.allocator(); { - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.append(1); @@ -2174,7 +2157,7 @@ test "shrink still sets length when resizing is disabled" { try testing.expect(list.items.len == 1); } { - var list: ArrayListUnmanaged(i32) = .empty; + var list: ArrayList(i32) = .empty; defer list.deinit(a); try list.append(a, 1); @@ -2190,7 +2173,7 @@ test "shrinkAndFree with a copy" { var failing_allocator = testing.FailingAllocator.init(testing.allocator, .{ .resize_fail_index = 0 }); const a = failing_allocator.allocator(); - var list = ArrayList(i32).init(a); + var list = Managed(i32).init(a); defer list.deinit(); try list.appendNTimes(3, 16); @@ -2201,7 +2184,7 @@ test "shrinkAndFree with a copy" { test "addManyAsArray" { const a = std.testing.allocator; { - var list = ArrayList(u8).init(a); + var list = Managed(u8).init(a); defer list.deinit(); (try list.addManyAsArray(4)).* = "aoeu".*; @@ -2211,7 +2194,7 @@ test "addManyAsArray" { try testing.expectEqualSlices(u8, list.items, "aoeuasdf"); } { - var list: ArrayListUnmanaged(u8) = .empty; + var list: ArrayList(u8) = .empty; defer list.deinit(a); (try list.addManyAsArray(a, 4)).* = "aoeu".*; @@ -2227,7 +2210,7 @@ test "growing memory preserves contents" { // will be triggered in the next operation. const a = std.testing.allocator; { - var list = ArrayList(u8).init(a); + var list = Managed(u8).init(a); defer list.deinit(); (try list.addManyAsArray(4)).* = "abcd".*; @@ -2241,7 +2224,7 @@ test "growing memory preserves contents" { try testing.expectEqualSlices(u8, list.items, "abcdijklefgh"); } { - var list: ArrayListUnmanaged(u8) = .empty; + var list: ArrayList(u8) = .empty; defer list.deinit(a); (try list.addManyAsArray(a, 4)).* = "abcd".*; @@ -2259,22 +2242,22 @@ test "growing memory preserves contents" { test "fromOwnedSlice" { const a = testing.allocator; { - var orig_list = ArrayList(u8).init(a); + var orig_list = Managed(u8).init(a); defer orig_list.deinit(); try orig_list.appendSlice("foobar"); const slice = try orig_list.toOwnedSlice(); - var list = ArrayList(u8).fromOwnedSlice(a, slice); + var list = Managed(u8).fromOwnedSlice(a, slice); defer list.deinit(); try testing.expectEqualStrings(list.items, "foobar"); } { - var list = ArrayList(u8).init(a); + var list = Managed(u8).init(a); defer list.deinit(); try list.appendSlice("foobar"); const slice = try list.toOwnedSlice(); - var unmanaged = ArrayListUnmanaged(u8).fromOwnedSlice(slice); + var unmanaged = ArrayList(u8).fromOwnedSlice(slice); defer unmanaged.deinit(a); try testing.expectEqualStrings(unmanaged.items, "foobar"); } @@ -2283,22 +2266,22 @@ test "fromOwnedSlice" { test "fromOwnedSliceSentinel" { const a = testing.allocator; { - var orig_list = ArrayList(u8).init(a); + var orig_list = Managed(u8).init(a); defer orig_list.deinit(); try orig_list.appendSlice("foobar"); const sentinel_slice = try orig_list.toOwnedSliceSentinel(0); - var list = ArrayList(u8).fromOwnedSliceSentinel(a, 0, sentinel_slice); + var list = Managed(u8).fromOwnedSliceSentinel(a, 0, sentinel_slice); defer list.deinit(); try testing.expectEqualStrings(list.items, "foobar"); } { - var list = ArrayList(u8).init(a); + var list = Managed(u8).init(a); defer list.deinit(); try list.appendSlice("foobar"); const sentinel_slice = try list.toOwnedSliceSentinel(0); - var unmanaged = ArrayListUnmanaged(u8).fromOwnedSliceSentinel(0, sentinel_slice); + var unmanaged = ArrayList(u8).fromOwnedSliceSentinel(0, sentinel_slice); defer unmanaged.deinit(a); try testing.expectEqualStrings(unmanaged.items, "foobar"); } @@ -2307,7 +2290,7 @@ test "fromOwnedSliceSentinel" { test "toOwnedSliceSentinel" { const a = testing.allocator; { - var list = ArrayList(u8).init(a); + var list = Managed(u8).init(a); defer list.deinit(); try list.appendSlice("foobar"); @@ -2317,7 +2300,7 @@ test "toOwnedSliceSentinel" { try testing.expectEqualStrings(result, mem.sliceTo(result.ptr, 0)); } { - var list: ArrayListUnmanaged(u8) = .empty; + var list: ArrayList(u8) = .empty; defer list.deinit(a); try list.appendSlice(a, "foobar"); @@ -2331,7 +2314,7 @@ test "toOwnedSliceSentinel" { test "accepts unaligned slices" { const a = testing.allocator; { - var list = std.ArrayListAligned(u8, .@"8").init(a); + var list = AlignedManaged(u8, .@"8").init(a); defer list.deinit(); try list.appendSlice(&.{ 0, 1, 2, 3 }); @@ -2341,7 +2324,7 @@ test "accepts unaligned slices" { try testing.expectEqualSlices(u8, list.items, &.{ 0, 8, 9, 6, 7, 2, 3 }); } { - var list: std.ArrayListAlignedUnmanaged(u8, .@"8") = .empty; + var list: Aligned(u8, .@"8") = .empty; defer list.deinit(a); try list.appendSlice(a, &.{ 0, 1, 2, 3 }); @@ -2352,11 +2335,11 @@ test "accepts unaligned slices" { } } -test "ArrayList(u0)" { - // An ArrayList on zero-sized types should not need to allocate +test "Managed(u0)" { + // An Managed on zero-sized types should not need to allocate const a = testing.failing_allocator; - var list = ArrayList(u0).init(a); + var list = Managed(u0).init(a); defer list.deinit(); try list.append(0); @@ -2372,10 +2355,10 @@ test "ArrayList(u0)" { try testing.expectEqual(count, 3); } -test "ArrayList(?u32).pop()" { +test "Managed(?u32).pop()" { const a = testing.allocator; - var list = ArrayList(?u32).init(a); + var list = Managed(?u32).init(a); defer list.deinit(); try list.append(null); @@ -2389,10 +2372,10 @@ test "ArrayList(?u32).pop()" { try testing.expect(list.pop() == null); } -test "ArrayList(u32).getLast()" { +test "Managed(u32).getLast()" { const a = testing.allocator; - var list = ArrayList(u32).init(a); + var list = Managed(u32).init(a); defer list.deinit(); try list.append(2); @@ -2400,10 +2383,10 @@ test "ArrayList(u32).getLast()" { try testing.expectEqual(const_list.getLast(), 2); } -test "ArrayList(u32).getLastOrNull()" { +test "Managed(u32).getLastOrNull()" { const a = testing.allocator; - var list = ArrayList(u32).init(a); + var list = Managed(u32).init(a); defer list.deinit(); try testing.expectEqual(list.getLastOrNull(), null); @@ -2419,7 +2402,7 @@ test "return OutOfMemory when capacity would exceed maximum usize integer value" const items = &.{ 42, 43 }; { - var list: ArrayListUnmanaged(u32) = .{ + var list: ArrayList(u32) = .{ .items = undefined, .capacity = math.maxInt(usize) - 1, }; @@ -2436,7 +2419,7 @@ test "return OutOfMemory when capacity would exceed maximum usize integer value" } { - var list: ArrayList(u32) = .{ + var list: Managed(u32) = .{ .items = undefined, .capacity = math.maxInt(usize) - 1, .allocator = a, @@ -2457,7 +2440,7 @@ test "return OutOfMemory when capacity would exceed maximum usize integer value" test "orderedRemoveMany" { const gpa = testing.allocator; - var list: ArrayListUnmanaged(usize) = .empty; + var list: Aligned(usize, null) = .empty; defer list.deinit(gpa); for (0..10) |n| { |
