authorgravatar for goon.pri.low@gmail.comKendall Condon <goon.pri.low@gmail.com> 2024-07-14 00:56:29+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-14 00:56:29+00:00
logee6a52b40f9836beb0d35820d1987f3c7e522f45
tree2b2997877b7d2d4b8c6038271edbfeb17e37f787
parent944c6d40ce520f345450a15006498dd760fc3d3a
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.ArrayList.unusedCapacitySlice: Return unaligned slice (#20490)


1 files changed, 9 insertions(+), 2 deletions(-)

lib/std/array_list.zig+9-2
...@@ -566,7 +566,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {...@@ -566,7 +566,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
566 /// This can be useful for writing directly into an ArrayList.566 /// This can be useful for writing directly into an ArrayList.
567 /// Note that such an operation must be followed up with a direct567 /// Note that such an operation must be followed up with a direct
568 /// modification of `self.items.len`.568 /// modification of `self.items.len`.
569 pub fn unusedCapacitySlice(self: Self) Slice {569 pub fn unusedCapacitySlice(self: Self) []T {
570 return self.allocatedSlice()[self.items.len..];570 return self.allocatedSlice()[self.items.len..];
571 }571 }
572572
...@@ -1193,7 +1193,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ...@@ -1193,7 +1193,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
1193 /// This can be useful for writing directly into an ArrayList.1193 /// This can be useful for writing directly into an ArrayList.
1194 /// Note that such an operation must be followed up with a direct1194 /// Note that such an operation must be followed up with a direct
1195 /// modification of `self.items.len`.1195 /// modification of `self.items.len`.
1196 pub fn unusedCapacitySlice(self: Self) Slice {1196 pub fn unusedCapacitySlice(self: Self) []T {
1197 return self.allocatedSlice()[self.items.len..];1197 return self.allocatedSlice()[self.items.len..];
1198 }1198 }
11991199
...@@ -2242,3 +2242,10 @@ test "return OutOfMemory when capacity would exceed maximum usize integer value"...@@ -2242,3 +2242,10 @@ test "return OutOfMemory when capacity would exceed maximum usize integer value"
2242 try testing.expectError(error.OutOfMemory, list.ensureUnusedCapacity(2));2242 try testing.expectError(error.OutOfMemory, list.ensureUnusedCapacity(2));
2243 }2243 }
2244}2244}
2245
2246test "ArrayListAligned with non-native alignment compiles unusedCapabitySlice" {
2247 var list = ArrayListAligned(u8, 4).init(testing.allocator);
2248 defer list.deinit();
2249 try list.appendNTimes(1, 4);
2250 _ = list.unusedCapacitySlice();
2251}