|
1 | 1 | use crate::{Growth, SplitVec};
|
2 |
| -use orx_fixed_vec::FixedVec; |
3 | 2 | use orx_pinned_vec::{NotSelfRefVecItem, PinnedVec};
|
4 | 3 |
|
5 | 4 | // std::vec::vec
|
|
79 | 78 | self.into()
|
80 | 79 | }
|
81 | 80 | }
|
82 |
| - |
83 |
| -// orx_fixed_vec::FixedVec |
84 |
| -impl<T, G> SplitVec<T, G> |
85 |
| -where |
86 |
| - G: Growth, |
87 |
| - T: NotSelfRefVecItem + Clone, |
88 |
| -{ |
89 |
| - /// Collects the split vector into a fixed vector |
90 |
| - /// with a fixed capacity being exactly equal to the length of this split vector. |
91 |
| - /// |
92 |
| - /// # Safety |
93 |
| - /// |
94 |
| - /// Since `T: NotSelfRefVecItem`, it is safe to clone the data of the elements. |
95 |
| - /// |
96 |
| - /// # Examples |
97 |
| - /// |
98 |
| - /// ``` |
99 |
| - /// use orx_split_vec::prelude::*; |
100 |
| - /// |
101 |
| - /// // SplitVec with dynamic capacity and configurable growth strategy. |
102 |
| - /// let mut split = SplitVec::with_linear_growth(5); |
103 |
| - /// for i in 0..35 { |
104 |
| - /// split.push(i); |
105 |
| - /// } |
106 |
| - /// assert_eq!(35, split.len()); |
107 |
| - /// assert_eq!(2, split.fragments().len()); |
108 |
| - /// assert_eq!(32, split.fragments()[0].len()); |
109 |
| - /// assert_eq!(3, split.fragments()[1].len()); |
110 |
| - /// |
111 |
| - /// // FixedVec with std::vec::Vec complexity & performance. |
112 |
| - /// let fixed = split.collect_fixed_vec(); |
113 |
| - /// assert_eq!(35, fixed.len()); |
114 |
| - /// assert_eq!(fixed, split); |
115 |
| - /// ``` |
116 |
| - pub fn collect_fixed_vec(&self) -> FixedVec<T> { |
117 |
| - unsafe { self.unsafe_collect_fixed_vec() } |
118 |
| - } |
119 |
| -} |
120 |
| - |
121 |
| -impl<T, G> SplitVec<T, G> |
122 |
| -where |
123 |
| - G: Growth, |
124 |
| - T: Clone, |
125 |
| -{ |
126 |
| - /// Collects the split vector into a fixed vector |
127 |
| - /// with a fixed capacity being exactly equal to the length of this split vector. |
128 |
| - /// |
129 |
| - /// # Safety |
130 |
| - /// |
131 |
| - /// Since `T` is not a `NotSelfRefVecItem`, it is assumed as a `SelfRefVecItem` |
132 |
| - /// to be conservative. A naive clone of a vector of `SelfRefVecItem` elements |
133 |
| - /// is unsafe due to the following scenario: |
134 |
| - /// |
135 |
| - /// * say the vector contains two elements `['a', 'b']` where `'a'` holds a reference to `'b'`. |
136 |
| - /// * when we clone this vector, element `'a'` of the second vector will be pointing to |
137 |
| - /// element `'b'` of the first vector, which is already incorrect. |
138 |
| - /// * furthermore, if the first vector is dropped, the abovementioned reference will be |
139 |
| - /// an dangling reference leading to UB. |
140 |
| - /// |
141 |
| - /// Therefore, cloning elements of a vector where elements are not `NotSelfRefVecItem` |
142 |
| - /// is `unsafe`. |
143 |
| - pub unsafe fn unsafe_collect_fixed_vec(&self) -> FixedVec<T> { |
144 |
| - let mut fixed = FixedVec::new(self.len()); |
145 |
| - for fragment in &self.fragments { |
146 |
| - fixed.extend_from_slice(&fragment.data); |
147 |
| - } |
148 |
| - fixed |
149 |
| - } |
150 |
| -} |
0 commit comments