1use crate::SmartBuffer;
2
3use core::mem::size_of;
4use alloc::alloc::{dealloc};
5
6impl<T, const N: usize> IntoIterator for SmartBuffer<T,N>
7 where T: Clone
8{
9 type Item = T;
10 type IntoIter = SmartBufferIter<T,N>;
11 fn into_iter(mut self) -> Self::IntoIter {
13 let stack_ptr = self.s_buf.as_mut_ptr();
14 let heap_ptr = self.d_buf;
15 let total_elem = self.size;
16
17 Self::IntoIter {
18 smart_buffer: self, stack_ptr,
20 heap_ptr,
21 total_elem,
22 count: 0
23 }
24 }
25}
26
27impl<'a, T, const N: usize> IntoIterator for &'a SmartBuffer<T,N>
28 where T: Clone
29{
30 type Item = &'a T;
31 type IntoIter = SmartBufferIterRef<'a,T,N>;
32 fn into_iter(self) -> Self::IntoIter {
34 let stack_ptr = self.s_buf.as_ptr();
35 let heap_ptr = self.d_buf;
36 let total_elem = self.size;
37
38 Self::IntoIter {
39 smart_buffer: self,
40 stack_ptr,
41 heap_ptr,
42 total_elem,
43 count: 0
44 }
45
46 }
47}
48
49impl<'a, T, const N: usize> IntoIterator for &'a mut SmartBuffer<T,N>
50 where T: Clone
51{
52 type Item = &'a mut T;
53 type IntoIter = SmartBufferIterRefMut<'a,T,N>;
54 fn into_iter(self) -> Self::IntoIter {
56 let stack_ptr = self.s_buf.as_mut_ptr();
57 let heap_ptr = self.d_buf.clone();
58 let total_elem = self.size;
59
60 Self::IntoIter {
61 smart_buffer: self,
62 stack_ptr,
63 heap_ptr,
64 total_elem,
65 count: 0
66 }
67 }
68}
69
70pub struct SmartBufferIter<T, const N:usize>
72 where T: Clone
73{
74 smart_buffer: SmartBuffer<T,N>,
75 stack_ptr: *mut T,
76 heap_ptr: Option<*mut T>,
77 total_elem: usize,
78 count: usize,
79}
80
81impl<T, const N: usize> Iterator for SmartBufferIter<T,N>
82 where T: Clone
83{
84 type Item = T;
85
86 fn next(&mut self) -> Option<Self::Item> {
87 if self.count < self.total_elem{
88 if self.count < N{
89 self.count += 1;
90 return unsafe {Some((*((self.stack_ptr as usize + (self.count - 1) * size_of::<T>()) as *mut T)).clone())}
91 }
92 self.count += 1;
93 return unsafe {Some((*((self.heap_ptr.unwrap() as usize + (self.count - N - 1) * size_of::<T>()) as *mut T)).clone())}
94 }
95 None
96 }
97}
98
99pub struct SmartBufferIterRef<'a, T, const N:usize>
101 where T: 'a + Clone
102{
103 smart_buffer: &'a SmartBuffer<T,N>,
104 stack_ptr: *const T,
105 heap_ptr: Option<*mut T>, total_elem: usize,
107 count: usize,
108}
109
110impl<'a, T, const N: usize> Iterator for SmartBufferIterRef<'a, T,N>
111 where T: 'a + Clone
112{
113 type Item = &'a T;
114
115 fn next(&mut self) -> Option<Self::Item> {
116 if self.count < self.total_elem{
117 if self.count < N{
118 self.count += 1;
119 return unsafe {Some(&*((self.stack_ptr as usize + (self.count - 1) * size_of::<T>()) as *const T))}
120 }
121 self.count += 1;
122 return unsafe {Some(&*((self.heap_ptr.unwrap() as usize + (self.count - N - 1) * size_of::<T>()) as *const T))}
123 }
124 None
125 }
126}
127
128pub struct SmartBufferIterRefMut<'a, T, const N:usize>
130 where T: 'a + Clone
131{
132 smart_buffer: &'a mut SmartBuffer<T,N>,
133 stack_ptr: *mut T,
134 heap_ptr: Option<*mut T>, total_elem: usize,
136 count: usize,
137}
138
139impl<'a, T, const N: usize> Iterator for SmartBufferIterRefMut<'a, T,N>
140 where T: 'a + Clone
141{
142 type Item = &'a mut T;
143
144 fn next(&mut self) -> Option<Self::Item> {
145 if self.count < self.total_elem{
146 if self.count < N{
147 self.count += 1;
148 return unsafe {Some(&mut *((self.stack_ptr as usize + (self.count - 1) * size_of::<T>()) as *mut T))}
149 }
150 self.count += 1;
151 return unsafe {Some(&mut *((self.heap_ptr.unwrap() as usize + (self.count - N - 1) * size_of::<T>()) as *mut T))}
152 }
153 None
154 }
155}