orx_split_vec/fragment/
fragment_struct.rs1use alloc::vec::Vec;
2
3#[derive(Default)]
4pub struct Fragment<T> {
11 pub(crate) data: Vec<T>,
12}
13
14impl<T> Fragment<T> {
15 pub fn new_with_first_value(capacity: usize, first_value: T) -> Self {
17 let mut data = Vec::with_capacity(capacity);
18 data.push(first_value);
19 Self { data }
20 }
21
22 pub fn new(capacity: usize) -> Self {
24 Self {
25 data: Vec::with_capacity(capacity),
26 }
27 }
28
29 pub fn new_filled<F: Fn() -> T>(capacity: usize, f: F) -> Self {
31 let mut data = Vec::with_capacity(capacity);
32 for _ in 0..capacity {
33 data.push(f());
34 }
35 Self { data }
36 }
37
38 pub fn has_capacity_for_one(&self) -> bool {
40 self.data.len() < self.data.capacity()
41 }
42
43 pub fn room(&self) -> usize {
45 self.data.capacity() - self.data.len()
46 }
47
48 pub(crate) fn fragments_with_default_capacity() -> Vec<Fragment<T>> {
50 Vec::new()
51 }
52
53 pub(crate) fn into_fragments(self) -> Vec<Fragment<T>> {
54 let mut fragments = Self::fragments_with_default_capacity();
55 fragments.push(self);
56 fragments
57 }
58
59 pub(crate) fn fragments_with_capacity(fragments_capacity: usize) -> Vec<Fragment<T>> {
60 Vec::with_capacity(fragments_capacity)
61 }
62
63 pub(crate) fn into_fragments_with_capacity(
64 self,
65 fragments_capacity: usize,
66 ) -> Vec<Fragment<T>> {
67 let mut fragments = Self::fragments_with_capacity(fragments_capacity);
68 fragments.push(self);
69 fragments
70 }
71
72 #[inline(always)]
74 pub(crate) unsafe fn zero(&mut self) {
75 let slice =
76 unsafe { core::slice::from_raw_parts_mut(self.data.as_mut_ptr(), self.capacity()) };
77 slice
78 .iter_mut()
79 .for_each(|m| *m = unsafe { core::mem::zeroed() });
80 }
81}
82
83pub(crate) unsafe fn set_fragments_len<T>(fragments: &mut [Fragment<T>], len: usize) {
84 let mut remaining = len;
85
86 for fragment in fragments {
87 let capacity = fragment.capacity();
88
89 match remaining <= capacity {
90 true => {
91 unsafe { fragment.set_len(remaining) };
92 remaining = 0;
93 }
94 false => {
95 unsafe { fragment.set_len(capacity) };
96 remaining -= capacity;
97 }
98 }
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn zeroed() {
108 let mut fragment: Fragment<i32> = Fragment::new(4);
109 unsafe { fragment.zero() };
110 unsafe { fragment.set_len(4) };
111 let zero: i32 = unsafe { core::mem::zeroed() };
112 for i in 0..4 {
113 assert_eq!(fragment.get(i), Some(&zero));
114 }
115 }
116}