kaspa_utils/
vec.rs

1pub trait VecExtensions<T> {
2    /// Pushes the provided value to the container if the container is empty
3    fn push_if_empty(self, value: T) -> Self;
4
5    /// Inserts the provided `value` at `index` while swapping the item at index to the end of the container
6    fn swap_insert(&mut self, index: usize, value: T);
7
8    /// Merges two containers one into the other and returns the result. The method is identical
9    /// to [`Vec<T>::append`] but can be used more ergonomically in a fluent calling fashion
10    fn merge(self, other: Self) -> Self;
11}
12
13impl<T> VecExtensions<T> for Vec<T> {
14    fn push_if_empty(mut self, value: T) -> Self {
15        if self.is_empty() {
16            self.push(value);
17        }
18        self
19    }
20
21    fn swap_insert(&mut self, index: usize, value: T) {
22        self.push(value);
23        let loc = self.len() - 1;
24        self.swap(index, loc);
25    }
26
27    fn merge(mut self, mut other: Self) -> Self {
28        self.append(&mut other);
29        self
30    }
31}