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