| ... | ... | @@ -58,7 +58,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 58 | 58 | /// Initialize with capacity to hold at least `num` elements. |
| 59 | 59 | /// The resulting capacity is likely to be equal to `num`. |
| 60 | 60 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 61 | | pub fn initCapacity(allocator: Allocator, num: usize) !Self { |
| 61 | pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self { |
| 62 | 62 | var self = Self.init(allocator); |
| 63 | 63 | try self.ensureTotalCapacityPrecise(num); |
| 64 | 64 | return self; |
| ... | ... | @@ -102,14 +102,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /// The caller owns the returned memory. Empties this ArrayList. |
| 105 | | pub fn toOwnedSliceSentinel(self: *Self, comptime sentinel: T) ![:sentinel]T { |
| 105 | pub fn toOwnedSliceSentinel(self: *Self, comptime sentinel: T) Allocator.Error![:sentinel]T { |
| 106 | 106 | try self.append(sentinel); |
| 107 | 107 | const result = self.toOwnedSlice(); |
| 108 | 108 | return result[0 .. result.len - 1 :sentinel]; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /// Creates a copy of this ArrayList, using the same allocator. |
| 112 | | pub fn clone(self: *Self) !Self { |
| 112 | pub fn clone(self: *Self) Allocator.Error!Self { |
| 113 | 113 | var cloned = try Self.initCapacity(self.allocator, self.capacity); |
| 114 | 114 | cloned.appendSliceAssumeCapacity(self.items); |
| 115 | 115 | return cloned; |
| ... | ... | @@ -117,7 +117,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 117 | 117 | |
| 118 | 118 | /// Insert `item` at index `n` by moving `list[n .. list.len]` to make room. |
| 119 | 119 | /// This operation is O(N). |
| 120 | | pub fn insert(self: *Self, n: usize, item: T) !void { |
| 120 | pub fn insert(self: *Self, n: usize, item: T) Allocator.Error!void { |
| 121 | 121 | try self.ensureUnusedCapacity(1); |
| 122 | 122 | self.items.len += 1; |
| 123 | 123 | |
| ... | ... | @@ -127,7 +127,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 127 | 127 | |
| 128 | 128 | /// Insert slice `items` at index `i` by moving `list[i .. list.len]` to make room. |
| 129 | 129 | /// This operation is O(N). |
| 130 | | pub fn insertSlice(self: *Self, i: usize, items: []const T) !void { |
| 130 | pub fn insertSlice(self: *Self, i: usize, items: []const T) Allocator.Error!void { |
| 131 | 131 | try self.ensureUnusedCapacity(items.len); |
| 132 | 132 | self.items.len += items.len; |
| 133 | 133 | |
| ... | ... | @@ -139,7 +139,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 139 | 139 | /// Grows list if `len < new_items.len`. |
| 140 | 140 | /// Shrinks list if `len > new_items.len`. |
| 141 | 141 | /// Invalidates pointers if this ArrayList is resized. |
| 142 | | pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: []const T) !void { |
| 142 | pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: []const T) Allocator.Error!void { |
| 143 | 143 | const after_range = start + len; |
| 144 | 144 | const range = self.items[start..after_range]; |
| 145 | 145 | |
| ... | ... | @@ -164,7 +164,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /// Extend the list by 1 element. Allocates more memory as necessary. |
| 167 | | pub fn append(self: *Self, item: T) !void { |
| 167 | pub fn append(self: *Self, item: T) Allocator.Error!void { |
| 168 | 168 | const new_item_ptr = try self.addOne(); |
| 169 | 169 | new_item_ptr.* = item; |
| 170 | 170 | } |
| ... | ... | @@ -206,7 +206,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 206 | 206 | |
| 207 | 207 | /// Append the slice of items to the list. Allocates more |
| 208 | 208 | /// memory as necessary. |
| 209 | | pub fn appendSlice(self: *Self, items: []const T) !void { |
| 209 | pub fn appendSlice(self: *Self, items: []const T) Allocator.Error!void { |
| 210 | 210 | try self.ensureUnusedCapacity(items.len); |
| 211 | 211 | self.appendSliceAssumeCapacity(items); |
| 212 | 212 | } |
| ... | ... | @@ -234,14 +234,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 234 | 234 | |
| 235 | 235 | /// Same as `append` except it returns the number of bytes written, which is always the same |
| 236 | 236 | /// as `m.len`. The purpose of this function existing is to match `std.io.Writer` API. |
| 237 | | fn appendWrite(self: *Self, m: []const u8) !usize { |
| 237 | fn appendWrite(self: *Self, m: []const u8) Allocator.Error!usize { |
| 238 | 238 | try self.appendSlice(m); |
| 239 | 239 | return m.len; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | /// Append a value to the list `n` times. |
| 243 | 243 | /// Allocates more memory as necessary. |
| 244 | | pub fn appendNTimes(self: *Self, value: T, n: usize) !void { |
| 244 | pub fn appendNTimes(self: *Self, value: T, n: usize) Allocator.Error!void { |
| 245 | 245 | const old_len = self.items.len; |
| 246 | 246 | try self.resize(self.items.len + n); |
| 247 | 247 | mem.set(T, self.items[old_len..self.items.len], value); |
| ... | ... | @@ -258,7 +258,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 258 | 258 | |
| 259 | 259 | /// Adjust the list's length to `new_len`. |
| 260 | 260 | /// Does not initialize added items if any. |
| 261 | | pub fn resize(self: *Self, new_len: usize) !void { |
| 261 | pub fn resize(self: *Self, new_len: usize) Allocator.Error!void { |
| 262 | 262 | try self.ensureTotalCapacity(new_len); |
| 263 | 263 | self.items.len = new_len; |
| 264 | 264 | } |
| ... | ... | @@ -304,7 +304,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 304 | 304 | |
| 305 | 305 | /// Modify the array so that it can hold at least `new_capacity` items. |
| 306 | 306 | /// Invalidates pointers if additional memory is needed. |
| 307 | | pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void { |
| 307 | pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) Allocator.Error!void { |
| 308 | 308 | if (@sizeOf(T) > 0) { |
| 309 | 309 | var better_capacity = self.capacity; |
| 310 | 310 | if (better_capacity >= new_capacity) return; |
| ... | ... | @@ -324,7 +324,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 324 | 324 | /// Like `ensureTotalCapacity`, but the resulting capacity is much more likely |
| 325 | 325 | /// (but not guaranteed) to be equal to `new_capacity`. |
| 326 | 326 | /// Invalidates pointers if additional memory is needed. |
| 327 | | pub fn ensureTotalCapacityPrecise(self: *Self, new_capacity: usize) !void { |
| 327 | pub fn ensureTotalCapacityPrecise(self: *Self, new_capacity: usize) Allocator.Error!void { |
| 328 | 328 | if (@sizeOf(T) > 0) { |
| 329 | 329 | if (self.capacity >= new_capacity) return; |
| 330 | 330 | |
| ... | ... | @@ -339,7 +339,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 339 | 339 | |
| 340 | 340 | /// Modify the array so that it can hold at least `additional_count` **more** items. |
| 341 | 341 | /// Invalidates pointers if additional memory is needed. |
| 342 | | pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void { |
| 342 | pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) Allocator.Error!void { |
| 343 | 343 | return self.ensureTotalCapacity(self.items.len + additional_count); |
| 344 | 344 | } |
| 345 | 345 | |
| ... | ... | @@ -351,7 +351,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 351 | 351 | |
| 352 | 352 | /// Increase length by 1, returning pointer to the new item. |
| 353 | 353 | /// The returned pointer becomes invalid when the list resized. |
| 354 | | pub fn addOne(self: *Self) !*T { |
| 354 | pub fn addOne(self: *Self) Allocator.Error!*T { |
| 355 | 355 | const newlen = self.items.len + 1; |
| 356 | 356 | try self.ensureTotalCapacity(newlen); |
| 357 | 357 | return self.addOneAssumeCapacity(); |
| ... | ... | @@ -372,7 +372,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 372 | 372 | /// The return value is an array pointing to the newly allocated elements. |
| 373 | 373 | /// The returned pointer becomes invalid when the list is resized. |
| 374 | 374 | /// Resizes list if `self.capacity` is not large enough. |
| 375 | | pub fn addManyAsArray(self: *Self, comptime n: usize) !*[n]T { |
| 375 | pub fn addManyAsArray(self: *Self, comptime n: usize) Allocator.Error!*[n]T { |
| 376 | 376 | const prev_len = self.items.len; |
| 377 | 377 | try self.resize(self.items.len + n); |
| 378 | 378 | return self.items[prev_len..][0..n]; |
| ... | ... | @@ -464,7 +464,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 464 | 464 | /// Initialize with capacity to hold at least num elements. |
| 465 | 465 | /// The resulting capacity is likely to be equal to `num`. |
| 466 | 466 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 467 | | pub fn initCapacity(allocator: Allocator, num: usize) !Self { |
| 467 | pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self { |
| 468 | 468 | var self = Self{}; |
| 469 | 469 | try self.ensureTotalCapacityPrecise(allocator, num); |
| 470 | 470 | return self; |
| ... | ... | @@ -490,14 +490,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | /// The caller owns the returned memory. ArrayList becomes empty. |
| 493 | | pub fn toOwnedSliceSentinel(self: *Self, allocator: Allocator, comptime sentinel: T) ![:sentinel]T { |
| 493 | pub fn toOwnedSliceSentinel(self: *Self, allocator: Allocator, comptime sentinel: T) Allocator.Error![:sentinel]T { |
| 494 | 494 | try self.append(allocator, sentinel); |
| 495 | 495 | const result = self.toOwnedSlice(allocator); |
| 496 | 496 | return result[0 .. result.len - 1 :sentinel]; |
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | /// Creates a copy of this ArrayList. |
| 500 | | pub fn clone(self: *Self, allocator: Allocator) !Self { |
| 500 | pub fn clone(self: *Self, allocator: Allocator) Allocator.Error!Self { |
| 501 | 501 | var cloned = try Self.initCapacity(allocator, self.capacity); |
| 502 | 502 | cloned.appendSliceAssumeCapacity(self.items); |
| 503 | 503 | return cloned; |
| ... | ... | @@ -506,7 +506,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 506 | 506 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` |
| 507 | 507 | /// to higher indices to make room. |
| 508 | 508 | /// This operation is O(N). |
| 509 | | pub fn insert(self: *Self, allocator: Allocator, n: usize, item: T) !void { |
| 509 | pub fn insert(self: *Self, allocator: Allocator, n: usize, item: T) Allocator.Error!void { |
| 510 | 510 | try self.ensureUnusedCapacity(allocator, 1); |
| 511 | 511 | self.items.len += 1; |
| 512 | 512 | |
| ... | ... | @@ -517,7 +517,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 517 | 517 | /// Insert slice `items` at index `i`. Moves `list[i .. list.len]` to |
| 518 | 518 | /// higher indicices make room. |
| 519 | 519 | /// This operation is O(N). |
| 520 | | pub fn insertSlice(self: *Self, allocator: Allocator, i: usize, items: []const T) !void { |
| 520 | pub fn insertSlice(self: *Self, allocator: Allocator, i: usize, items: []const T) Allocator.Error!void { |
| 521 | 521 | try self.ensureUnusedCapacity(allocator, items.len); |
| 522 | 522 | self.items.len += items.len; |
| 523 | 523 | |
| ... | ... | @@ -529,14 +529,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 529 | 529 | /// Grows list if `len < new_items.len`. |
| 530 | 530 | /// Shrinks list if `len > new_items.len` |
| 531 | 531 | /// Invalidates pointers if this ArrayList is resized. |
| 532 | | pub fn replaceRange(self: *Self, allocator: Allocator, start: usize, len: usize, new_items: []const T) !void { |
| 532 | pub fn replaceRange(self: *Self, allocator: Allocator, start: usize, len: usize, new_items: []const T) Allocator.Error!void { |
| 533 | 533 | var managed = self.toManaged(allocator); |
| 534 | 534 | try managed.replaceRange(start, len, new_items); |
| 535 | 535 | self.* = managed.moveToUnmanaged(); |
| 536 | 536 | } |
| 537 | 537 | |
| 538 | 538 | /// Extend the list by 1 element. Allocates more memory as necessary. |
| 539 | | pub fn append(self: *Self, allocator: Allocator, item: T) !void { |
| 539 | pub fn append(self: *Self, allocator: Allocator, item: T) Allocator.Error!void { |
| 540 | 540 | const new_item_ptr = try self.addOne(allocator); |
| 541 | 541 | new_item_ptr.* = item; |
| 542 | 542 | } |
| ... | ... | @@ -577,7 +577,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 577 | 577 | |
| 578 | 578 | /// Append the slice of items to the list. Allocates more |
| 579 | 579 | /// memory as necessary. |
| 580 | | pub fn appendSlice(self: *Self, allocator: Allocator, items: []const T) !void { |
| 580 | pub fn appendSlice(self: *Self, allocator: Allocator, items: []const T) Allocator.Error!void { |
| 581 | 581 | try self.ensureUnusedCapacity(allocator, items.len); |
| 582 | 582 | self.appendSliceAssumeCapacity(items); |
| 583 | 583 | } |
| ... | ... | @@ -610,14 +610,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 610 | 610 | |
| 611 | 611 | /// Same as `append` except it returns the number of bytes written, which is always the same |
| 612 | 612 | /// as `m.len`. The purpose of this function existing is to match `std.io.Writer` API. |
| 613 | | fn appendWrite(context: WriterContext, m: []const u8) !usize { |
| 613 | fn appendWrite(context: WriterContext, m: []const u8) Allocator.Error!usize { |
| 614 | 614 | try context.self.appendSlice(context.allocator, m); |
| 615 | 615 | return m.len; |
| 616 | 616 | } |
| 617 | 617 | |
| 618 | 618 | /// Append a value to the list `n` times. |
| 619 | 619 | /// Allocates more memory as necessary. |
| 620 | | pub fn appendNTimes(self: *Self, allocator: Allocator, value: T, n: usize) !void { |
| 620 | pub fn appendNTimes(self: *Self, allocator: Allocator, value: T, n: usize) Allocator.Error!void { |
| 621 | 621 | const old_len = self.items.len; |
| 622 | 622 | try self.resize(allocator, self.items.len + n); |
| 623 | 623 | mem.set(T, self.items[old_len..self.items.len], value); |
| ... | ... | @@ -635,7 +635,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 635 | 635 | |
| 636 | 636 | /// Adjust the list's length to `new_len`. |
| 637 | 637 | /// Does not initialize added items, if any. |
| 638 | | pub fn resize(self: *Self, allocator: Allocator, new_len: usize) !void { |
| 638 | pub fn resize(self: *Self, allocator: Allocator, new_len: usize) Allocator.Error!void { |
| 639 | 639 | try self.ensureTotalCapacity(allocator, new_len); |
| 640 | 640 | self.items.len = new_len; |
| 641 | 641 | } |
| ... | ... | @@ -677,7 +677,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 677 | 677 | |
| 678 | 678 | /// Modify the array so that it can hold at least `new_capacity` items. |
| 679 | 679 | /// Invalidates pointers if additional memory is needed. |
| 680 | | pub fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_capacity: usize) !void { |
| 680 | pub fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void { |
| 681 | 681 | var better_capacity = self.capacity; |
| 682 | 682 | if (better_capacity >= new_capacity) return; |
| 683 | 683 | |
| ... | ... | @@ -693,7 +693,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 693 | 693 | /// Like `ensureTotalCapacity`, but the resulting capacity is much more likely |
| 694 | 694 | /// (but not guaranteed) to be equal to `new_capacity`. |
| 695 | 695 | /// Invalidates pointers if additional memory is needed. |
| 696 | | pub fn ensureTotalCapacityPrecise(self: *Self, allocator: Allocator, new_capacity: usize) !void { |
| 696 | pub fn ensureTotalCapacityPrecise(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void { |
| 697 | 697 | if (self.capacity >= new_capacity) return; |
| 698 | 698 | |
| 699 | 699 | const new_memory = try allocator.reallocAtLeast(self.allocatedSlice(), new_capacity); |
| ... | ... | @@ -707,7 +707,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 707 | 707 | self: *Self, |
| 708 | 708 | allocator: Allocator, |
| 709 | 709 | additional_count: usize, |
| 710 | | ) !void { |
| 710 | ) Allocator.Error!void { |
| 711 | 711 | return self.ensureTotalCapacity(allocator, self.items.len + additional_count); |
| 712 | 712 | } |
| 713 | 713 | |
| ... | ... | @@ -720,7 +720,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 720 | 720 | |
| 721 | 721 | /// Increase length by 1, returning pointer to the new item. |
| 722 | 722 | /// The returned pointer becomes invalid when the list resized. |
| 723 | | pub fn addOne(self: *Self, allocator: Allocator) !*T { |
| 723 | pub fn addOne(self: *Self, allocator: Allocator) Allocator.Error!*T { |
| 724 | 724 | const newlen = self.items.len + 1; |
| 725 | 725 | try self.ensureTotalCapacity(allocator, newlen); |
| 726 | 726 | return self.addOneAssumeCapacity(); |
| ... | ... | @@ -740,7 +740,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 740 | 740 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| 741 | 741 | /// The return value is an array pointing to the newly allocated elements. |
| 742 | 742 | /// The returned pointer becomes invalid when the list is resized. |
| 743 | | pub fn addManyAsArray(self: *Self, allocator: Allocator, comptime n: usize) !*[n]T { |
| 743 | pub fn addManyAsArray(self: *Self, allocator: Allocator, comptime n: usize) Allocator.Error!*[n]T { |
| 744 | 744 | const prev_len = self.items.len; |
| 745 | 745 | try self.resize(allocator, self.items.len + n); |
| 746 | 746 | return self.items[prev_len..][0..n]; |