Skip to content

Commit b1d1b04

Browse files
jedisct1andrewrk
authored andcommitted
BoundedArray: add a len() function to get the length as a usize
The type of the active length was changed to the smallest possible type, which is nice to get a more compact representation. However, that change introduced unexpected side effects. For example, `a.len + b.len` can now trigger an integer overflow. There's also an inconsistency between functions that set the size, which all use a `usize`, and the way to read the size, that uses a different type. This is also inconsistent with pretty much anything else that represents a slice length. Replace `.len` with `len()`, a function that returns the length as a `usize` to minimize surprise and simplify application code.
1 parent 4163126 commit b1d1b04

File tree

2 files changed

+65
-56
lines changed

2 files changed

+65
-56
lines changed

lib/std/bounded_array.zig

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ pub fn BoundedArrayAligned(
4242
const Len = std.math.IntFittingRange(0, buffer_capacity);
4343

4444
buffer: [buffer_capacity]T align(alignment) = undefined,
45-
len: Len = 0,
45+
46+
/// The active array length. Not that in order to save space, this is not
47+
/// a `usize`, but rather the smallest integer type that can hold the
48+
/// maximum capacity. In order to get the length as a `usize`, use `len()`.
49+
active_len: Len = 0,
4650

4751
/// Set the actual length of the slice.
4852
/// Returns error.Overflow if it exceeds the length of the backing array.
49-
pub fn init(len: usize) error{Overflow}!Self {
50-
if (len > buffer_capacity) return error.Overflow;
51-
return Self{ .len = @intCast(len) };
53+
pub fn init(initial_len: usize) error{Overflow}!Self {
54+
if (initial_len > buffer_capacity) return error.Overflow;
55+
return Self{ .active_len = @intCast(initial_len) };
5256
}
5357

5458
/// View the internal array as a slice whose size was previously set.
@@ -57,7 +61,7 @@ pub fn BoundedArrayAligned(
5761
*align(alignment) const [buffer_capacity]T => []align(alignment) const T,
5862
else => unreachable,
5963
} {
60-
return self.buffer[0..self.len];
64+
return self.buffer[0..self.active_len];
6165
}
6266

6367
/// View the internal array as a constant slice whose size was previously set.
@@ -67,9 +71,9 @@ pub fn BoundedArrayAligned(
6771

6872
/// Adjust the slice's length to `len`.
6973
/// Does not initialize added items if any.
70-
pub fn resize(self: *Self, len: usize) error{Overflow}!void {
71-
if (len > buffer_capacity) return error.Overflow;
72-
self.len = @intCast(len);
74+
pub fn resize(self: *Self, new_len: usize) error{Overflow}!void {
75+
if (new_len > buffer_capacity) return error.Overflow;
76+
self.active_len = @intCast(new_len);
7377
}
7478

7579
/// Copy the content of an existing slice.
@@ -94,9 +98,14 @@ pub fn BoundedArrayAligned(
9498
return self.buffer.len;
9599
}
96100

101+
/// Return the current length of the slice.
102+
pub fn len(self: Self) usize {
103+
return self.active_len;
104+
}
105+
97106
/// Check that the slice can hold at least `additional_count` items.
98107
pub fn ensureUnusedCapacity(self: Self, additional_count: usize) error{Overflow}!void {
99-
if (self.len + additional_count > buffer_capacity) {
108+
if (self.active_len + additional_count > buffer_capacity) {
100109
return error.Overflow;
101110
}
102111
}
@@ -110,39 +119,39 @@ pub fn BoundedArrayAligned(
110119
/// Increase length by 1, returning pointer to the new item.
111120
/// Asserts that there is space for the new item.
112121
pub fn addOneAssumeCapacity(self: *Self) *T {
113-
assert(self.len < buffer_capacity);
114-
self.len += 1;
115-
return &self.slice()[self.len - 1];
122+
assert(self.active_len < buffer_capacity);
123+
self.active_len += 1;
124+
return &self.slice()[self.active_len - 1];
116125
}
117126

118127
/// Resize the slice, adding `n` new elements, which have `undefined` values.
119128
/// The return value is a slice pointing to the uninitialized elements.
120129
pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment) [n]T {
121-
const prev_len = self.len;
122-
try self.resize(self.len + n);
130+
const prev_len = self.active_len;
131+
try self.resize(self.active_len + n);
123132
return self.slice()[prev_len..][0..n];
124133
}
125134

126135
/// Remove and return the last element from the slice.
127136
/// Asserts the slice has at least one item.
128137
pub fn pop(self: *Self) T {
129-
const item = self.get(self.len - 1);
130-
self.len -= 1;
138+
const item = self.get(self.active_len - 1);
139+
self.active_len -= 1;
131140
return item;
132141
}
133142

134143
/// Remove and return the last element from the slice, or
135144
/// return `null` if the slice is empty.
136145
pub fn popOrNull(self: *Self) ?T {
137-
return if (self.len == 0) null else self.pop();
146+
return if (self.active_len == 0) null else self.pop();
138147
}
139148

140149
/// Return a slice of only the extra capacity after items.
141150
/// This can be useful for writing directly into it.
142151
/// Note that such an operation must be followed up with a
143152
/// call to `resize()`
144153
pub fn unusedCapacitySlice(self: *Self) []align(alignment) T {
145-
return self.buffer[self.len..];
154+
return self.buffer[self.active_len..];
146155
}
147156

148157
/// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.
@@ -152,7 +161,7 @@ pub fn BoundedArrayAligned(
152161
i: usize,
153162
item: T,
154163
) error{Overflow}!void {
155-
if (i > self.len) {
164+
if (i > self.active_len) {
156165
return error.Overflow;
157166
}
158167
_ = try self.addOne();
@@ -165,8 +174,8 @@ pub fn BoundedArrayAligned(
165174
/// This operation is O(N).
166175
pub fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void {
167176
try self.ensureUnusedCapacity(items.len);
168-
self.len = @intCast(self.len + items.len);
169-
mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]);
177+
self.active_len = @intCast(self.active_len + items.len);
178+
mem.copyBackwards(T, self.slice()[i + items.len .. self.active_len], self.constSlice()[i .. self.active_len - items.len]);
170179
@memcpy(self.slice()[i..][0..items.len], items);
171180
}
172181

@@ -176,10 +185,10 @@ pub fn BoundedArrayAligned(
176185
pub fn replaceRange(
177186
self: *Self,
178187
start: usize,
179-
len: usize,
188+
range_len: usize,
180189
new_items: []const T,
181190
) error{Overflow}!void {
182-
const after_range = start + len;
191+
const after_range = start + range_len;
183192
var range = self.slice()[start..after_range];
184193

185194
if (range.len == new_items.len) {
@@ -195,7 +204,7 @@ pub fn BoundedArrayAligned(
195204
for (self.constSlice()[after_range..], 0..) |item, i| {
196205
self.slice()[after_subrange..][i] = item;
197206
}
198-
self.len = @intCast(self.len - len + new_items.len);
207+
self.active_len = @intCast(self.active_len - range_len + new_items.len);
199208
}
200209
}
201210

@@ -217,20 +226,20 @@ pub fn BoundedArrayAligned(
217226
/// Asserts the slice has at least one item.
218227
/// This operation is O(N).
219228
pub fn orderedRemove(self: *Self, i: usize) T {
220-
const newlen = self.len - 1;
229+
const newlen = self.active_len - 1;
221230
if (newlen == i) return self.pop();
222231
const old_item = self.get(i);
223232
for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
224233
self.set(newlen, undefined);
225-
self.len = newlen;
234+
self.active_len = newlen;
226235
return old_item;
227236
}
228237

229238
/// Remove the element at the specified index and return it.
230239
/// The empty slot is filled from the end of the slice.
231240
/// This operation is O(1).
232241
pub fn swapRemove(self: *Self, i: usize) T {
233-
if (self.len - 1 == i) return self.pop();
242+
if (self.active_len - 1 == i) return self.pop();
234243
const old_item = self.get(i);
235244
self.set(i, self.pop());
236245
return old_item;
@@ -245,26 +254,26 @@ pub fn BoundedArrayAligned(
245254
/// Append the slice of items to the slice, asserting the capacity is already
246255
/// enough to store the new items.
247256
pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {
248-
const old_len = self.len;
249-
self.len = @intCast(self.len + items.len);
257+
const old_len = self.active_len;
258+
self.active_len = @intCast(self.active_len + items.len);
250259
@memcpy(self.slice()[old_len..][0..items.len], items);
251260
}
252261

253262
/// Append a value to the slice `n` times.
254263
/// Allocates more memory as necessary.
255264
pub fn appendNTimes(self: *Self, value: T, n: usize) error{Overflow}!void {
256-
const old_len = self.len;
265+
const old_len = self.active_len;
257266
try self.resize(old_len + n);
258-
@memset(self.slice()[old_len..self.len], value);
267+
@memset(self.slice()[old_len..self.active_len], value);
259268
}
260269

261270
/// Append a value to the slice `n` times.
262271
/// Asserts the capacity is enough.
263272
pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void {
264-
const old_len = self.len;
265-
assert(self.len + n <= buffer_capacity);
266-
self.len = @intCast(self.len + n);
267-
@memset(self.slice()[old_len..self.len], value);
273+
const old_len = self.active_len;
274+
assert(self.active_len + n <= buffer_capacity);
275+
self.active_len = @intCast(self.active_len + n);
276+
@memset(self.slice()[old_len..self.active_len], value);
268277
}
269278

270279
pub const Writer = if (T != u8)
@@ -295,7 +304,7 @@ test BoundedArray {
295304
try testing.expectEqual(a.constSlice().len, 32);
296305

297306
try a.resize(48);
298-
try testing.expectEqual(a.len, 48);
307+
try testing.expectEqual(a.len(), 48);
299308

300309
const x = [_]u8{1} ** 10;
301310
a = try BoundedArray(u8, 64).fromSlice(&x);
@@ -313,18 +322,18 @@ test BoundedArray {
313322
try a.ensureUnusedCapacity(a.capacity());
314323
(try a.addOne()).* = 0;
315324
try a.ensureUnusedCapacity(a.capacity() - 1);
316-
try testing.expectEqual(a.len, 1);
325+
try testing.expectEqual(a.len(), 1);
317326

318327
const uninitialized = try a.addManyAsArray(4);
319328
try testing.expectEqual(uninitialized.len, 4);
320-
try testing.expectEqual(a.len, 5);
329+
try testing.expectEqual(a.len(), 5);
321330

322331
try a.append(0xff);
323-
try testing.expectEqual(a.len, 6);
332+
try testing.expectEqual(a.len(), 6);
324333
try testing.expectEqual(a.pop(), 0xff);
325334

326335
a.appendAssumeCapacity(0xff);
327-
try testing.expectEqual(a.len, 6);
336+
try testing.expectEqual(a.len(), 6);
328337
try testing.expectEqual(a.pop(), 0xff);
329338

330339
try a.resize(1);
@@ -338,46 +347,46 @@ test BoundedArray {
338347
try a.resize(10);
339348

340349
try a.insert(5, 0xaa);
341-
try testing.expectEqual(a.len, 11);
350+
try testing.expectEqual(a.len(), 11);
342351
try testing.expectEqual(a.get(5), 0xaa);
343352
try testing.expectEqual(a.get(9), 3);
344353
try testing.expectEqual(a.get(10), 4);
345354

346355
try a.insert(11, 0xbb);
347-
try testing.expectEqual(a.len, 12);
356+
try testing.expectEqual(a.len(), 12);
348357
try testing.expectEqual(a.pop(), 0xbb);
349358

350359
try a.appendSlice(&x);
351-
try testing.expectEqual(a.len, 11 + x.len);
360+
try testing.expectEqual(a.len(), 11 + x.len);
352361

353362
try a.appendNTimes(0xbb, 5);
354-
try testing.expectEqual(a.len, 11 + x.len + 5);
363+
try testing.expectEqual(a.len(), 11 + x.len + 5);
355364
try testing.expectEqual(a.pop(), 0xbb);
356365

357366
a.appendNTimesAssumeCapacity(0xcc, 5);
358-
try testing.expectEqual(a.len, 11 + x.len + 5 - 1 + 5);
367+
try testing.expectEqual(a.len(), 11 + x.len + 5 - 1 + 5);
359368
try testing.expectEqual(a.pop(), 0xcc);
360369

361-
try testing.expectEqual(a.len, 29);
370+
try testing.expectEqual(a.len(), 29);
362371
try a.replaceRange(1, 20, &x);
363-
try testing.expectEqual(a.len, 29 + x.len - 20);
372+
try testing.expectEqual(a.len(), 29 + x.len - 20);
364373

365374
try a.insertSlice(0, &x);
366-
try testing.expectEqual(a.len, 29 + x.len - 20 + x.len);
375+
try testing.expectEqual(a.len(), 29 + x.len - 20 + x.len);
367376

368377
try a.replaceRange(1, 5, &x);
369-
try testing.expectEqual(a.len, 29 + x.len - 20 + x.len + x.len - 5);
378+
try testing.expectEqual(a.len(), 29 + x.len - 20 + x.len + x.len - 5);
370379

371380
try a.append(10);
372381
try testing.expectEqual(a.pop(), 10);
373382

374383
try a.append(20);
375384
const removed = a.orderedRemove(5);
376385
try testing.expectEqual(removed, 1);
377-
try testing.expectEqual(a.len, 34);
386+
try testing.expectEqual(a.len(), 34);
378387

379388
a.set(0, 0xdd);
380-
a.set(a.len - 1, 0xee);
389+
a.set(a.len() - 1, 0xee);
381390
const swapped = a.swapRemove(0);
382391
try testing.expectEqual(swapped, 0xdd);
383392
try testing.expectEqual(a.get(0), 0xee);
@@ -397,8 +406,8 @@ test "BoundedArray sizeOf" {
397406
try testing.expectEqual(@sizeOf(BoundedArray(u8, 3)), 4);
398407

399408
// `len` is the minimum required size to hold the maximum capacity
400-
try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 15), undefined).len), u4);
401-
try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 16), undefined).len), u5);
409+
try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 15), undefined).active_len), u4);
410+
try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 16), undefined).active_len), u5);
402411
}
403412

404413
test "BoundedArrayAligned" {

lib/std/io/Reader.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,14 @@ pub fn readIntoBoundedBytes(
258258
comptime num_bytes: usize,
259259
bounded: *std.BoundedArray(u8, num_bytes),
260260
) anyerror!void {
261-
while (bounded.len < num_bytes) {
261+
while (bounded.len() < num_bytes) {
262262
// get at most the number of bytes free in the bounded array
263263
const bytes_read = try self.read(bounded.unusedCapacitySlice());
264264
if (bytes_read == 0) return;
265265

266-
// bytes_read will never be larger than @TypeOf(bounded.len)
266+
// bytes_read will never be larger than the unused capacity
267267
// due to `self.read` being bounded by `bounded.unusedCapacitySlice()`
268-
bounded.len += @as(@TypeOf(bounded.len), @intCast(bytes_read));
268+
try bounded.resize(bounded.len() + bytes_read);
269269
}
270270
}
271271

0 commit comments

Comments
 (0)