authorgravatar for motiejus@jakstys.ltMotiejus Jakštys <motiejus@jakstys.lt> 2022-02-20 11:39:51+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-22 13:57:51-05:00
loge620b692c6c9f7c4b655f30aae5e51a87dc56e91
tree167716eba59152d2348e8732fb408d50ab54ca56
parentdf38c46bee9a54b9935f2e3372f6b86299730888

[std.ArrayList] return explicit errors

All errors from std.ArrayList are allocation errors. Mark them as such. This is helpful when one wants to obtain a function pointer.

1 files changed, 32 insertions(+), 32 deletions(-)

lib/std/array_list.zig+32-32
......@@ -58,7 +58,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
5858 /// Initialize with capacity to hold at least `num` elements.
5959 /// The resulting capacity is likely to be equal to `num`.
6060 /// 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 {
6262 var self = Self.init(allocator);
6363 try self.ensureTotalCapacityPrecise(num);
6464 return self;
......@@ -102,14 +102,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
102102 }
103103
104104 /// 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 {
106106 try self.append(sentinel);
107107 const result = self.toOwnedSlice();
108108 return result[0 .. result.len - 1 :sentinel];
109109 }
110110
111111 /// 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 {
113113 var cloned = try Self.initCapacity(self.allocator, self.capacity);
114114 cloned.appendSliceAssumeCapacity(self.items);
115115 return cloned;
......@@ -117,7 +117,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
117117
118118 /// Insert `item` at index `n` by moving `list[n .. list.len]` to make room.
119119 /// 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 {
121121 try self.ensureUnusedCapacity(1);
122122 self.items.len += 1;
123123
......@@ -127,7 +127,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
127127
128128 /// Insert slice `items` at index `i` by moving `list[i .. list.len]` to make room.
129129 /// 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 {
131131 try self.ensureUnusedCapacity(items.len);
132132 self.items.len += items.len;
133133
......@@ -139,7 +139,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
139139 /// Grows list if `len < new_items.len`.
140140 /// Shrinks list if `len > new_items.len`.
141141 /// 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 {
143143 const after_range = start + len;
144144 const range = self.items[start..after_range];
145145
......@@ -164,7 +164,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
164164 }
165165
166166 /// 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 {
168168 const new_item_ptr = try self.addOne();
169169 new_item_ptr.* = item;
170170 }
......@@ -206,7 +206,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
206206
207207 /// Append the slice of items to the list. Allocates more
208208 /// 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 {
210210 try self.ensureUnusedCapacity(items.len);
211211 self.appendSliceAssumeCapacity(items);
212212 }
......@@ -234,14 +234,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
234234
235235 /// Same as `append` except it returns the number of bytes written, which is always the same
236236 /// 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 {
238238 try self.appendSlice(m);
239239 return m.len;
240240 }
241241
242242 /// Append a value to the list `n` times.
243243 /// 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 {
245245 const old_len = self.items.len;
246246 try self.resize(self.items.len + n);
247247 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 {
258258
259259 /// Adjust the list's length to `new_len`.
260260 /// 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 {
262262 try self.ensureTotalCapacity(new_len);
263263 self.items.len = new_len;
264264 }
......@@ -304,7 +304,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
304304
305305 /// Modify the array so that it can hold at least `new_capacity` items.
306306 /// 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 {
308308 if (@sizeOf(T) > 0) {
309309 var better_capacity = self.capacity;
310310 if (better_capacity >= new_capacity) return;
......@@ -324,7 +324,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
324324 /// Like `ensureTotalCapacity`, but the resulting capacity is much more likely
325325 /// (but not guaranteed) to be equal to `new_capacity`.
326326 /// 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 {
328328 if (@sizeOf(T) > 0) {
329329 if (self.capacity >= new_capacity) return;
330330
......@@ -339,7 +339,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
339339
340340 /// Modify the array so that it can hold at least `additional_count` **more** items.
341341 /// 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 {
343343 return self.ensureTotalCapacity(self.items.len + additional_count);
344344 }
345345
......@@ -351,7 +351,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
351351
352352 /// Increase length by 1, returning pointer to the new item.
353353 /// 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 {
355355 const newlen = self.items.len + 1;
356356 try self.ensureTotalCapacity(newlen);
357357 return self.addOneAssumeCapacity();
......@@ -372,7 +372,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
372372 /// The return value is an array pointing to the newly allocated elements.
373373 /// The returned pointer becomes invalid when the list is resized.
374374 /// 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 {
376376 const prev_len = self.items.len;
377377 try self.resize(self.items.len + n);
378378 return self.items[prev_len..][0..n];
......@@ -464,7 +464,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
464464 /// Initialize with capacity to hold at least num elements.
465465 /// The resulting capacity is likely to be equal to `num`.
466466 /// 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 {
468468 var self = Self{};
469469 try self.ensureTotalCapacityPrecise(allocator, num);
470470 return self;
......@@ -490,14 +490,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
490490 }
491491
492492 /// 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 {
494494 try self.append(allocator, sentinel);
495495 const result = self.toOwnedSlice(allocator);
496496 return result[0 .. result.len - 1 :sentinel];
497497 }
498498
499499 /// 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 {
501501 var cloned = try Self.initCapacity(allocator, self.capacity);
502502 cloned.appendSliceAssumeCapacity(self.items);
503503 return cloned;
......@@ -506,7 +506,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
506506 /// Insert `item` at index `n`. Moves `list[n .. list.len]`
507507 /// to higher indices to make room.
508508 /// 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 {
510510 try self.ensureUnusedCapacity(allocator, 1);
511511 self.items.len += 1;
512512
......@@ -517,7 +517,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
517517 /// Insert slice `items` at index `i`. Moves `list[i .. list.len]` to
518518 /// higher indicices make room.
519519 /// 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 {
521521 try self.ensureUnusedCapacity(allocator, items.len);
522522 self.items.len += items.len;
523523
......@@ -529,14 +529,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
529529 /// Grows list if `len < new_items.len`.
530530 /// Shrinks list if `len > new_items.len`
531531 /// 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 {
533533 var managed = self.toManaged(allocator);
534534 try managed.replaceRange(start, len, new_items);
535535 self.* = managed.moveToUnmanaged();
536536 }
537537
538538 /// 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 {
540540 const new_item_ptr = try self.addOne(allocator);
541541 new_item_ptr.* = item;
542542 }
......@@ -577,7 +577,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
577577
578578 /// Append the slice of items to the list. Allocates more
579579 /// 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 {
581581 try self.ensureUnusedCapacity(allocator, items.len);
582582 self.appendSliceAssumeCapacity(items);
583583 }
......@@ -610,14 +610,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
610610
611611 /// Same as `append` except it returns the number of bytes written, which is always the same
612612 /// 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 {
614614 try context.self.appendSlice(context.allocator, m);
615615 return m.len;
616616 }
617617
618618 /// Append a value to the list `n` times.
619619 /// 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 {
621621 const old_len = self.items.len;
622622 try self.resize(allocator, self.items.len + n);
623623 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
635635
636636 /// Adjust the list's length to `new_len`.
637637 /// 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 {
639639 try self.ensureTotalCapacity(allocator, new_len);
640640 self.items.len = new_len;
641641 }
......@@ -677,7 +677,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
677677
678678 /// Modify the array so that it can hold at least `new_capacity` items.
679679 /// 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 {
681681 var better_capacity = self.capacity;
682682 if (better_capacity >= new_capacity) return;
683683
......@@ -693,7 +693,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
693693 /// Like `ensureTotalCapacity`, but the resulting capacity is much more likely
694694 /// (but not guaranteed) to be equal to `new_capacity`.
695695 /// 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 {
697697 if (self.capacity >= new_capacity) return;
698698
699699 const new_memory = try allocator.reallocAtLeast(self.allocatedSlice(), new_capacity);
......@@ -707,7 +707,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
707707 self: *Self,
708708 allocator: Allocator,
709709 additional_count: usize,
710 ) !void {
710 ) Allocator.Error!void {
711711 return self.ensureTotalCapacity(allocator, self.items.len + additional_count);
712712 }
713713
......@@ -720,7 +720,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
720720
721721 /// Increase length by 1, returning pointer to the new item.
722722 /// 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 {
724724 const newlen = self.items.len + 1;
725725 try self.ensureTotalCapacity(allocator, newlen);
726726 return self.addOneAssumeCapacity();
......@@ -740,7 +740,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
740740 /// Resize the array, adding `n` new elements, which have `undefined` values.
741741 /// The return value is an array pointing to the newly allocated elements.
742742 /// 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 {
744744 const prev_len = self.items.len;
745745 try self.resize(allocator, self.items.len + n);
746746 return self.items[prev_len..][0..n];