orx_split_vec/growth/linear/from.rs
1use super::constants::FIXED_CAPACITIES;
2use crate::{Linear, SplitVec};
3use alloc::vec::Vec;
4
5// into SplitVec
6impl<T> From<Vec<T>> for SplitVec<T, Linear> {
7 /// Converts a `Vec` into a `SplitVec` by
8 /// moving the vector into the split vector as the first fragment,
9 /// without copying the data.
10 ///
11 /// # Examples
12 ///
13 /// ```
14 /// use orx_split_vec::*;
15 ///
16 /// let vec = vec!['a', 'b', 'c'];
17 /// let vec_capacity = vec.capacity();
18 ///
19 /// let split_vec: SplitVec<_, Linear> = vec.into();
20 ///
21 /// assert_eq!(split_vec, &['a', 'b', 'c']);
22 /// assert_eq!(1, split_vec.fragments().len());
23 /// assert!(vec_capacity <= split_vec.capacity());
24 /// ```
25 fn from(value: Vec<T>) -> Self {
26 let len = value.len();
27 let f = FIXED_CAPACITIES
28 .iter()
29 .enumerate()
30 .find(|(_, fixed_cap)| **fixed_cap > len)
31 .map(|(f, _)| f)
32 .expect("overflow");
33 let growth = Linear::new(f);
34 let fragments = alloc::vec![value.into()];
35 Self::from_raw_parts(len, fragments, growth)
36 }
37}