Skip to main content

orx_split_vec/common_traits/
clone.rs

1use crate::{Growth, SplitVec};
2use alloc::vec::Vec;
3use orx_pinned_vec::PinnedVec;
4
5impl<T, G> Clone for SplitVec<T, G>
6where
7    T: Clone,
8    G: Growth,
9{
10    fn clone(&self) -> Self {
11        let mut fragments = Vec::with_capacity(self.fragments.capacity());
12        for fragment in &self.fragments {
13            fragments.push(fragment.clone());
14        }
15        Self::from_raw_parts(self.len(), fragments, self.growth().clone())
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use crate::*;
22
23    #[test]
24    fn clone() {
25        fn test<G: Growth>(mut vec: SplitVec<usize, G>) {
26            for i in 0..57 {
27                vec.push(i);
28            }
29
30            let clone = vec.clone();
31
32            assert_eq!(vec.len(), clone.len());
33            assert_eq!(vec.fragments().len(), clone.fragments().len());
34            assert_eq!(vec.capacity(), clone.capacity());
35            assert_eq!(vec.capacity_state(), clone.capacity_state());
36            assert_eq!(
37                vec.maximum_concurrent_capacity(),
38                clone.maximum_concurrent_capacity()
39            );
40
41            for (a, b) in vec.fragments().iter().zip(clone.fragments().iter()) {
42                assert_eq!(a.len(), b.len());
43                assert_eq!(a.capacity(), b.capacity());
44
45                for (x, y) in a.iter().zip(b.iter()) {
46                    assert_eq!(x, y);
47                }
48            }
49        }
50
51        test_all_growth_types!(test);
52    }
53}