File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -323,6 +323,37 @@ where
323
323
Ok ( ( ) )
324
324
}
325
325
326
+ /// Removes the last element from a vector and returns it, or `None` if it is empty.
327
+ ///
328
+ /// # Examples
329
+ ///
330
+ /// ```
331
+ /// let mut v = KVec::new();
332
+ /// v.push(1, GFP_KERNEL)?;
333
+ /// v.push(2, GFP_KERNEL)?;
334
+ /// assert_eq!(&v, &[1, 2]);
335
+ ///
336
+ /// assert_eq!(v.pop(), Some(2));
337
+ /// assert_eq!(v.pop(), Some(1));
338
+ /// assert_eq!(v.pop(), None);
339
+ /// # Ok::<(), Error>(())
340
+ /// ```
341
+ pub fn pop ( & mut self ) -> Option < T > {
342
+ if self . is_empty ( ) {
343
+ return None ;
344
+ }
345
+
346
+ let removed: * mut T = {
347
+ // SAFETY: We just checked that the length is at least one.
348
+ let slice = unsafe { self . dec_len ( 1 ) } ;
349
+ // SAFETY: The argument to `dec_len` was 1 so this returns a slice of length 1.
350
+ unsafe { slice. get_unchecked_mut ( 0 ) }
351
+ } ;
352
+
353
+ // SAFETY: The guarantees of `dec_len` allow us to take ownership of this value.
354
+ Some ( unsafe { removed. read ( ) } )
355
+ }
356
+
326
357
/// Creates a new [`Vec`] instance with at least the given capacity.
327
358
///
328
359
/// # Examples
You can’t perform that action at this time.
0 commit comments