orx_split_vec/resize_multiple.rs
1use crate::{Growth, SplitVec};
2use orx_pinned_vec::PinnedVec;
3
4impl<'a, T: Clone + 'a, G> Extend<&'a T> for SplitVec<T, G>
5where
6 G: Growth,
7{
8 /// Clones and appends all elements in the iterator to the vec.
9 ///
10 /// Iterates over the `iter`, clones each element, and then appends
11 /// it to this vector.
12 ///
13 /// # Examples
14 ///
15 /// ```
16 /// use orx_split_vec::*;
17 ///
18 /// let mut vec = SplitVec::with_linear_growth(4);
19 /// vec.push(1);
20 /// vec.push(2);
21 /// vec.push(3);
22 /// assert_eq!(vec, [1, 2, 3]);
23 ///
24 /// vec.extend(&[4, 5, 6, 7]);
25 /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]);
26 ///
27 /// let mut sec_vec = SplitVec::with_linear_growth(4);
28 /// sec_vec.extend(vec.iter());
29 /// assert_eq!(sec_vec, [1, 2, 3, 4, 5, 6, 7]);
30 /// ```
31 fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
32 for x in iter {
33 self.push(x.clone());
34 }
35 }
36}
37
38impl<T, G> Extend<T> for SplitVec<T, G>
39where
40 G: Growth,
41{
42 /// Extends a collection with the contents of an iterator.
43 ///
44 /// Iterates over the `iter`, moves and appends each element
45 /// to this vector.
46 ///
47 /// # Examples
48 ///
49 /// ```
50 /// use orx_split_vec::*;
51 ///
52 /// let mut vec = SplitVec::with_linear_growth(4);
53 /// vec.push(1);
54 /// vec.push(2);
55 /// vec.push(3);
56 /// assert_eq!(vec, [1, 2, 3]);
57 ///
58 /// vec.extend(vec![4, 5, 6, 7].into_iter());
59 /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]);
60 /// ```
61 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
62 for x in iter {
63 self.push(x);
64 }
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use crate::test_all_growth_types;
71 use crate::*;
72 use alloc::vec::Vec;
73
74 #[test]
75 fn extend() {
76 fn test<G: Growth>(mut vec: SplitVec<usize, G>) {
77 vec.extend(0..42);
78 vec.extend(&(42..63).collect::<Vec<_>>());
79 vec.extend((53..90).map(|i| i + 10));
80
81 assert_eq!(100, vec.len());
82 for i in 0..100 {
83 assert_eq!(i, vec[i]);
84 }
85 }
86 test_all_growth_types!(test);
87 }
88}