authorgravatar for motiejus@jakstys.ltMotiejus Jakštys <motiejus@jakstys.lt> 2022-03-03 20:39:45+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-03 19:39:45+01:00
log65943010c70b33e711a038dff5f80066045ee1b7
treefa32584638d477a2e2af3847c69c45abee6b5555
parent9aa220ebb5553b12fe4f059c7b97bab0404ab689
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.BoundedArray: return explicit errors (#11044)

* std.BoundedArray: return explicit errors Makes it easier to mark explicit errors when using BoundedArray downstream. * std.BoundedArray.insert() returns Overflow only

1 files changed, 22 insertions(+), 13 deletions(-)

lib/std/bounded_array.zig+22-13
......@@ -23,7 +23,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
2323
2424 /// Set the actual length of the slice.
2525 /// Returns error.Overflow if it exceeds the length of the backing array.
26 pub fn init(len: usize) !Self {
26 pub fn init(len: usize) error{Overflow}!Self {
2727 if (len > capacity) return error.Overflow;
2828 return Self{ .len = len };
2929 }
......@@ -40,13 +40,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
4040
4141 /// Adjust the slice's length to `len`.
4242 /// Does not initialize added items if any.
43 pub fn resize(self: *Self, len: usize) !void {
43 pub fn resize(self: *Self, len: usize) error{Overflow}!void {
4444 if (len > capacity) return error.Overflow;
4545 self.len = len;
4646 }
4747
4848 /// Copy the content of an existing slice.
49 pub fn fromSlice(m: []const T) !Self {
49 pub fn fromSlice(m: []const T) error{Overflow}!Self {
5050 var list = try init(m.len);
5151 std.mem.copy(T, list.slice(), m);
5252 return list;
......@@ -68,14 +68,14 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
6868 }
6969
7070 /// Check that the slice can hold at least `additional_count` items.
71 pub fn ensureUnusedCapacity(self: Self, additional_count: usize) !void {
71 pub fn ensureUnusedCapacity(self: Self, additional_count: usize) error{Overflow}!void {
7272 if (self.len + additional_count > capacity) {
7373 return error.Overflow;
7474 }
7575 }
7676
7777 /// Increase length by 1, returning a pointer to the new item.
78 pub fn addOne(self: *Self) !*T {
78 pub fn addOne(self: *Self) error{Overflow}!*T {
7979 try self.ensureUnusedCapacity(1);
8080 return self.addOneAssumeCapacity();
8181 }
......@@ -90,7 +90,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
9090
9191 /// Resize the slice, adding `n` new elements, which have `undefined` values.
9292 /// The return value is a slice pointing to the uninitialized elements.
93 pub fn addManyAsArray(self: *Self, comptime n: usize) !*[n]T {
93 pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*[n]T {
9494 const prev_len = self.len;
9595 try self.resize(self.len + n);
9696 return self.slice()[prev_len..][0..n];
......@@ -120,9 +120,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
120120
121121 /// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.
122122 /// This operation is O(N).
123 pub fn insert(self: *Self, i: usize, item: T) !void {
123 pub fn insert(
124 self: *Self,
125 i: usize,
126 item: T,
127 ) error{Overflow}!void {
124128 if (i > self.len) {
125 return error.IndexOutOfBounds;
129 return error.Overflow;
126130 }
127131 _ = try self.addOne();
128132 var s = self.slice();
......@@ -132,7 +136,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
132136
133137 /// Insert slice `items` at index `i` by moving `slice[i .. slice.len]` to make room.
134138 /// This operation is O(N).
135 pub fn insertSlice(self: *Self, i: usize, items: []const T) !void {
139 pub fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void {
136140 try self.ensureUnusedCapacity(items.len);
137141 self.len += items.len;
138142 mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]);
......@@ -142,7 +146,12 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
142146 /// Replace range of elements `slice[start..start+len]` with `new_items`.
143147 /// Grows slice if `len < new_items.len`.
144148 /// Shrinks slice if `len > new_items.len`.
145 pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: []const T) !void {
149 pub fn replaceRange(
150 self: *Self,
151 start: usize,
152 len: usize,
153 new_items: []const T,
154 ) error{Overflow}!void {
146155 const after_range = start + len;
147156 var range = self.slice()[start..after_range];
148157
......@@ -164,7 +173,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
164173 }
165174
166175 /// Extend the slice by 1 element.
167 pub fn append(self: *Self, item: T) !void {
176 pub fn append(self: *Self, item: T) error{Overflow}!void {
168177 const new_item_ptr = try self.addOne();
169178 new_item_ptr.* = item;
170179 }
......@@ -201,7 +210,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
201210 }
202211
203212 /// Append the slice of items to the slice.
204 pub fn appendSlice(self: *Self, items: []const T) !void {
213 pub fn appendSlice(self: *Self, items: []const T) error{Overflow}!void {
205214 try self.ensureUnusedCapacity(items.len);
206215 self.appendSliceAssumeCapacity(items);
207216 }
......@@ -216,7 +225,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
216225
217226 /// Append a value to the slice `n` times.
218227 /// Allocates more memory as necessary.
219 pub fn appendNTimes(self: *Self, value: T, n: usize) !void {
228 pub fn appendNTimes(self: *Self, value: T, n: usize) error{Overflow}!void {
220229 const old_len = self.len;
221230 try self.resize(old_len + n);
222231 mem.set(T, self.slice()[old_len..self.len], value);