Skip to content

Commit 933ba93

Browse files
authored
std.BoundedArray: popOrNull() -> pop() [v2] (#22723)
1 parent 4e4775d commit 933ba93

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

doc/langref/test_switch_dispatch_loop.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ fn evaluate(initial_stack: []const i32, code: []const Instruction) !i32 {
1515
// Because all code after `continue` is unreachable, this branch does
1616
// not provide a result.
1717
.add => {
18-
try stack.append(stack.pop() + stack.pop());
18+
try stack.append(stack.pop().? + stack.pop().?);
1919

2020
ip += 1;
2121
continue :vm code[ip];
2222
},
2323
.mul => {
24-
try stack.append(stack.pop() * stack.pop());
24+
try stack.append(stack.pop().? * stack.pop().?);
2525

2626
ip += 1;
2727
continue :vm code[ip];
2828
},
29-
.end => stack.pop(),
29+
.end => stack.pop().?,
3030
};
3131
}
3232

lib/std/bounded_array.zig

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,14 @@ pub fn BoundedArrayAligned(
134134
return self.slice()[prev_len..][0..n];
135135
}
136136

137-
/// Remove and return the last element from the slice.
138-
/// Asserts the slice has at least one item.
139-
pub fn pop(self: *Self) T {
137+
/// Remove and return the last element from the slice, or return `null` if the slice is empty.
138+
pub fn pop(self: *Self) ?T {
139+
if (self.len == 0) return null;
140140
const item = self.get(self.len - 1);
141141
self.len -= 1;
142142
return item;
143143
}
144144

145-
/// Remove and return the last element from the slice, or
146-
/// return `null` if the slice is empty.
147-
pub fn popOrNull(self: *Self) ?T {
148-
return if (self.len == 0) null else self.pop();
149-
}
150-
151145
/// Return a slice of only the extra capacity after items.
152146
/// This can be useful for writing directly into it.
153147
/// Note that such an operation must be followed up with a
@@ -229,7 +223,7 @@ pub fn BoundedArrayAligned(
229223
/// This operation is O(N).
230224
pub fn orderedRemove(self: *Self, i: usize) T {
231225
const newlen = self.len - 1;
232-
if (newlen == i) return self.pop();
226+
if (newlen == i) return self.pop().?;
233227
const old_item = self.get(i);
234228
for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
235229
self.set(newlen, undefined);
@@ -241,9 +235,9 @@ pub fn BoundedArrayAligned(
241235
/// The empty slot is filled from the end of the slice.
242236
/// This operation is O(1).
243237
pub fn swapRemove(self: *Self, i: usize) T {
244-
if (self.len - 1 == i) return self.pop();
238+
if (self.len - 1 == i) return self.pop().?;
245239
const old_item = self.get(i);
246-
self.set(i, self.pop());
240+
self.set(i, self.pop().?);
247241
return old_item;
248242
}
249243

@@ -339,8 +333,8 @@ test BoundedArray {
339333
try testing.expectEqual(a.pop(), 0xff);
340334

341335
try a.resize(1);
342-
try testing.expectEqual(a.popOrNull(), 0);
343-
try testing.expectEqual(a.popOrNull(), null);
336+
try testing.expectEqual(a.pop(), 0);
337+
try testing.expectEqual(a.pop(), null);
344338
var unused = a.unusedCapacitySlice();
345339
@memset(unused[0..8], 2);
346340
unused[8] = 3;
@@ -397,7 +391,7 @@ test BoundedArray {
397391
try testing.expectEqual(added_slice.len, 3);
398392
try testing.expectEqual(a.len, 36);
399393

400-
while (a.popOrNull()) |_| {}
394+
while (a.pop()) |_| {}
401395
const w = a.writer();
402396
const s = "hello, this is a test string";
403397
try w.writeAll(s);

0 commit comments

Comments
 (0)