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 {...@@ -23,7 +23,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
2323
24 /// Set the actual length of the slice.24 /// Set the actual length of the slice.
25 /// Returns error.Overflow if it exceeds the length of the backing array.25 /// 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 {
27 if (len > capacity) return error.Overflow;27 if (len > capacity) return error.Overflow;
28 return Self{ .len = len };28 return Self{ .len = len };
29 }29 }
...@@ -40,13 +40,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -40,13 +40,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
4040
41 /// Adjust the slice's length to `len`.41 /// Adjust the slice's length to `len`.
42 /// Does not initialize added items if any.42 /// 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 {
44 if (len > capacity) return error.Overflow;44 if (len > capacity) return error.Overflow;
45 self.len = len;45 self.len = len;
46 }46 }
4747
48 /// Copy the content of an existing slice.48 /// 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 {
50 var list = try init(m.len);50 var list = try init(m.len);
51 std.mem.copy(T, list.slice(), m);51 std.mem.copy(T, list.slice(), m);
52 return list;52 return list;
...@@ -68,14 +68,14 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -68,14 +68,14 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
68 }68 }
6969
70 /// Check that the slice can hold at least `additional_count` items.70 /// 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 {
72 if (self.len + additional_count > capacity) {72 if (self.len + additional_count > capacity) {
73 return error.Overflow;73 return error.Overflow;
74 }74 }
75 }75 }
7676
77 /// Increase length by 1, returning a pointer to the new item.77 /// 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 {
79 try self.ensureUnusedCapacity(1);79 try self.ensureUnusedCapacity(1);
80 return self.addOneAssumeCapacity();80 return self.addOneAssumeCapacity();
81 }81 }
...@@ -90,7 +90,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -90,7 +90,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
9090
91 /// Resize the slice, adding `n` new elements, which have `undefined` values.91 /// Resize the slice, adding `n` new elements, which have `undefined` values.
92 /// The return value is a slice pointing to the uninitialized elements.92 /// 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 {
94 const prev_len = self.len;94 const prev_len = self.len;
95 try self.resize(self.len + n);95 try self.resize(self.len + n);
96 return self.slice()[prev_len..][0..n];96 return self.slice()[prev_len..][0..n];
...@@ -120,9 +120,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -120,9 +120,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
120120
121 /// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.121 /// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.
122 /// This operation is O(N).122 /// 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 {
124 if (i > self.len) {128 if (i > self.len) {
125 return error.IndexOutOfBounds;129 return error.Overflow;
126 }130 }
127 _ = try self.addOne();131 _ = try self.addOne();
128 var s = self.slice();132 var s = self.slice();
...@@ -132,7 +136,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -132,7 +136,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
132136
133 /// Insert slice `items` at index `i` by moving `slice[i .. slice.len]` to make room.137 /// Insert slice `items` at index `i` by moving `slice[i .. slice.len]` to make room.
134 /// This operation is O(N).138 /// 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 {
136 try self.ensureUnusedCapacity(items.len);140 try self.ensureUnusedCapacity(items.len);
137 self.len += items.len;141 self.len += items.len;
138 mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]);142 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 {...@@ -142,7 +146,12 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
142 /// Replace range of elements `slice[start..start+len]` with `new_items`.146 /// Replace range of elements `slice[start..start+len]` with `new_items`.
143 /// Grows slice if `len < new_items.len`.147 /// Grows slice if `len < new_items.len`.
144 /// Shrinks slice if `len > new_items.len`.148 /// 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 {
146 const after_range = start + len;155 const after_range = start + len;
147 var range = self.slice()[start..after_range];156 var range = self.slice()[start..after_range];
148157
...@@ -164,7 +173,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -164,7 +173,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
164 }173 }
165174
166 /// Extend the slice by 1 element.175 /// 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 {
168 const new_item_ptr = try self.addOne();177 const new_item_ptr = try self.addOne();
169 new_item_ptr.* = item;178 new_item_ptr.* = item;
170 }179 }
...@@ -201,7 +210,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -201,7 +210,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
201 }210 }
202211
203 /// Append the slice of items to the slice.212 /// 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 {
205 try self.ensureUnusedCapacity(items.len);214 try self.ensureUnusedCapacity(items.len);
206 self.appendSliceAssumeCapacity(items);215 self.appendSliceAssumeCapacity(items);
207 }216 }
...@@ -216,7 +225,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -216,7 +225,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
216225
217 /// Append a value to the slice `n` times.226 /// Append a value to the slice `n` times.
218 /// Allocates more memory as necessary.227 /// 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 {
220 const old_len = self.len;229 const old_len = self.len;
221 try self.resize(old_len + n);230 try self.resize(old_len + n);
222 mem.set(T, self.slice()[old_len..self.len], value);231 mem.set(T, self.slice()[old_len..self.len], value);