@@ -134,20 +134,14 @@ pub fn BoundedArrayAligned(
134
134
return self .slice ()[prev_len .. ][0.. n ];
135
135
}
136
136
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 ;
140
140
const item = self .get (self .len - 1 );
141
141
self .len -= 1 ;
142
142
return item ;
143
143
}
144
144
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
-
151
145
/// Return a slice of only the extra capacity after items.
152
146
/// This can be useful for writing directly into it.
153
147
/// Note that such an operation must be followed up with a
@@ -229,7 +223,7 @@ pub fn BoundedArrayAligned(
229
223
/// This operation is O(N).
230
224
pub fn orderedRemove (self : * Self , i : usize ) T {
231
225
const newlen = self .len - 1 ;
232
- if (newlen == i ) return self .pop ();
226
+ if (newlen == i ) return self .pop ().? ;
233
227
const old_item = self .get (i );
234
228
for (self .slice ()[i .. newlen ], 0.. ) | * b , j | b .* = self .get (i + 1 + j );
235
229
self .set (newlen , undefined );
@@ -241,9 +235,9 @@ pub fn BoundedArrayAligned(
241
235
/// The empty slot is filled from the end of the slice.
242
236
/// This operation is O(1).
243
237
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 ().? ;
245
239
const old_item = self .get (i );
246
- self .set (i , self .pop ());
240
+ self .set (i , self .pop ().? );
247
241
return old_item ;
248
242
}
249
243
@@ -339,8 +333,8 @@ test BoundedArray {
339
333
try testing .expectEqual (a .pop (), 0xff );
340
334
341
335
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 );
344
338
var unused = a .unusedCapacitySlice ();
345
339
@memset (unused [0.. 8], 2 );
346
340
unused [8 ] = 3 ;
@@ -397,7 +391,7 @@ test BoundedArray {
397
391
try testing .expectEqual (added_slice .len , 3 );
398
392
try testing .expectEqual (a .len , 36 );
399
393
400
- while (a .popOrNull ()) | _ | {}
394
+ while (a .pop ()) | _ | {}
401
395
const w = a .writer ();
402
396
const s = "hello, this is a test string" ;
403
397
try w .writeAll (s );
0 commit comments