| ... | ... | @@ -23,7 +23,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 23 | 23 | |
| 24 | 24 | /// Set the actual length of the slice. |
| 25 | 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 | 27 | if (len > capacity) return error.Overflow; |
| 28 | 28 | return Self{ .len = len }; |
| 29 | 29 | } |
| ... | ... | @@ -40,13 +40,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 40 | 40 | |
| 41 | 41 | /// Adjust the slice's length to `len`. |
| 42 | 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 | 44 | if (len > capacity) return error.Overflow; |
| 45 | 45 | self.len = len; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 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 | 50 | var list = try init(m.len); |
| 51 | 51 | std.mem.copy(T, list.slice(), m); |
| 52 | 52 | return list; |
| ... | ... | @@ -68,14 +68,14 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 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 | 72 | if (self.len + additional_count > capacity) { |
| 73 | 73 | return error.Overflow; |
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 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 | 79 | try self.ensureUnusedCapacity(1); |
| 80 | 80 | return self.addOneAssumeCapacity(); |
| 81 | 81 | } |
| ... | ... | @@ -90,7 +90,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 90 | 90 | |
| 91 | 91 | /// Resize the slice, adding `n` new elements, which have `undefined` values. |
| 92 | 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 | 94 | const prev_len = self.len; |
| 95 | 95 | try self.resize(self.len + n); |
| 96 | 96 | return self.slice()[prev_len..][0..n]; |
| ... | ... | @@ -120,9 +120,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 120 | 120 | |
| 121 | 121 | /// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room. |
| 122 | 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 | 128 | if (i > self.len) { |
| 125 | | return error.IndexOutOfBounds; |
| 129 | return error.Overflow; |
| 126 | 130 | } |
| 127 | 131 | _ = try self.addOne(); |
| 128 | 132 | var s = self.slice(); |
| ... | ... | @@ -132,7 +136,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 132 | 136 | |
| 133 | 137 | /// Insert slice `items` at index `i` by moving `slice[i .. slice.len]` to make room. |
| 134 | 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 | 140 | try self.ensureUnusedCapacity(items.len); |
| 137 | 141 | self.len += items.len; |
| 138 | 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 | 146 | /// Replace range of elements `slice[start..start+len]` with `new_items`. |
| 143 | 147 | /// Grows slice if `len < new_items.len`. |
| 144 | 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 | 155 | const after_range = start + len; |
| 147 | 156 | var range = self.slice()[start..after_range]; |
| 148 | 157 | |
| ... | ... | @@ -164,7 +173,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 164 | 173 | } |
| 165 | 174 | |
| 166 | 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 | 177 | const new_item_ptr = try self.addOne(); |
| 169 | 178 | new_item_ptr.* = item; |
| 170 | 179 | } |
| ... | ... | @@ -201,7 +210,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 201 | 210 | } |
| 202 | 211 | |
| 203 | 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 | 214 | try self.ensureUnusedCapacity(items.len); |
| 206 | 215 | self.appendSliceAssumeCapacity(items); |
| 207 | 216 | } |
| ... | ... | @@ -216,7 +225,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 216 | 225 | |
| 217 | 226 | /// Append a value to the slice `n` times. |
| 218 | 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 | 229 | const old_len = self.len; |
| 221 | 230 | try self.resize(old_len + n); |
| 222 | 231 | mem.set(T, self.slice()[old_len..self.len], value); |