1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This file is part of copy-stack-vec.
// SPDX-License-Identifier: MIT OR Apache-2.0
// Crate imports
use crate::vec::CopyStackVec;
impl<T: Copy, const N: usize> CopyStackVec<T, N> {
/// Removes and returns the element at `index`, shifting subsequent elements left.
///
/// Returns `None` if `index >= len`. Uses `copy_within` for overlap-safe shifting.
#[inline]
pub fn remove(&mut self, index: usize) -> Option<T> {
if index >= self.len {
return None;
}
let len = self.len;
// Read the element at `index` as a `T`.
let out = unsafe {
// SAFETY: `index < self.len`, so `buf[index]` is within the initialized
// prefix `buf[..len]` by invariant and contains a valid `T`.
self.buf[index].assume_init()
};
// Shift left: [index+1..len) -> [index..len-1)
if index + 1 < len {
self.buf.copy_within(index + 1..len, index);
}
self.len = len - 1;
Some(out)
}
/// Removes and returns the element at `index` by swapping with the last element.
///
/// Preserves neither order nor contiguity of the last two elements. Returns `None`
/// when `index >= len`. Removing the last element avoids a swap.
#[inline]
pub fn swap_remove(&mut self, index: usize) -> Option<T> {
if index >= self.len {
return None;
}
self.len -= 1;
let last = self.len;
// Read out the element at `index` as a `T`.
let out = unsafe {
// SAFETY: Before decrement, `index < old_len`, so `buf[index]` is
// within the initialized prefix and contains a valid `T`.
self.buf[index].assume_init()
};
// If it's not the last element, move the last slot into the hole.
if index != last {
self.buf[index] = self.buf[last];
}
Some(out)
}
}