ms_pdb/utils/
vec.rs

1//! Utilities for `Vec`
2
3use std::cmp::Ordering;
4
5/// Replace a range of values in a vector with a new range. The old and new ranges can be
6/// different sizes.
7pub fn replace_range_copy<T: Copy>(v: &mut Vec<T>, start: usize, old_len: usize, values: &[T]) {
8    assert!(start <= v.len());
9    assert!(old_len <= v.len() - start);
10
11    match values.len().cmp(&old_len) {
12        Ordering::Equal => {
13            v[start..start + values.len()].copy_from_slice(values);
14        }
15
16        Ordering::Less => {
17            // The new values are shorter than the old values.
18            // Copy the overlap, then drain the remainder.
19            v[start..start + values.len()].copy_from_slice(values);
20            v.drain(start + values.len()..start + old_len);
21        }
22
23        Ordering::Greater => {
24            // Copy the overlapping values.
25            // Then append the other values.
26            // Then rotate them into position.
27            let (lo, hi) = values.split_at(old_len);
28            v.extend_from_slice(hi);
29            v[start..start + old_len].copy_from_slice(lo);
30            v[start + old_len..].rotate_right(lo.len());
31        }
32    }
33}