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