1#[derive(Debug)]
2#[repr(C)]
3pub struct ArrayBox<T> {
4 pub data: *mut T,
5 pub length: usize,
6 pub capacity: usize,
7 pub owned: bool,
8}
9
10impl<T> ArrayBox<T> {
11 pub fn new() -> Self {
12 ArrayBox {
13 length: 0,
14 capacity: 0,
15 data: std::ptr::null_mut(),
16 owned: true,
17 }
18 }
19
20 pub fn from_vector(vector: Vec<T>) -> Self {
21 let mut array = Self::new();
22 array.set_vector(vector);
23 array
24 }
25
26 pub fn from_array(array_buffer: &[T]) -> Self
28 where
29 T: Clone,
30 {
31 Self::from_vector(Vec::<T>::from(array_buffer))
32 }
33
34 pub fn from_data(data: *mut T, length: usize) -> Self {
36 ArrayBox {
37 length,
38 capacity: length,
39 data,
40 owned: false,
41 }
42 }
43
44 pub fn set_vector(&mut self, vector: Vec<T>) {
46 Self::free_buffer(self.data, self.length, self.capacity, self.owned);
48 let mut data = vector;
49 data.shrink_to_fit();
50
51 self.length = data.len();
52 self.capacity = data.capacity();
53 self.data = Self::vec_to_buffer(data)
54 }
55
56 pub fn set_array(&mut self, array_buffer: &[T])
58 where
59 T: Clone,
60 {
61 let vector = Vec::<T>::from(array_buffer);
62 self.set_vector(vector);
63 }
64
65 pub fn to_slice(&self) -> &mut [T] {
66 unsafe { std::slice::from_raw_parts_mut(self.data, self.length) }
67 }
68
69 pub fn copy_into(&self, another_array: &mut ArrayBox<T>) {
70 assert!(
71 self.length <= another_array.length,
72 "The source does not fit into destination"
73 );
74 assert!(!self.data.is_null(), "The source data must not be nil");
75 assert!(
76 !another_array.data.is_null(),
77 "The destination data must not be nil"
78 );
79 unsafe { std::ptr::copy_nonoverlapping::<T>(self.data, another_array.data, self.length) }
80 }
81
82 pub fn to_vector(mut self) -> Vec<T>
83 where
84 T: Clone,
85 {
86 let vector = unsafe { Vec::from_raw_parts(self.data, self.length, self.capacity) };
87 if self.owned {
88 self.owned = false;
90 self.data = std::ptr::null_mut();
91 vector
92 } else {
93 let clone = vector.clone();
94 std::mem::forget(vector);
96 clone
97 }
98 }
99
100 pub fn at_put(&mut self, index: usize, object: T) {
101 assert!(index < self.length, "Index must be less than array length");
102
103 let slice = self.to_slice();
104 slice[index] = object;
105 }
106
107 pub fn at(&self, index: usize) -> T
108 where
109 T: Clone,
110 {
111 assert!(index < self.length, "Index must be less than array length");
112
113 let slice = self.to_slice();
114 slice[index].clone()
115 }
116}
117
118impl<T> ArrayBox<T> {
119 fn vec_to_buffer(mut _data: Vec<T>) -> *mut T {
120 let _ptr = _data.as_mut_ptr();
121 std::mem::forget(_data);
122 _ptr
123 }
124
125 fn free_buffer(_ptr_data: *mut T, _length: usize, _capacity: usize, _owned: bool) {
126 if _ptr_data.is_null() {
127 return;
128 }
129 if !_owned {
130 return;
131 }
132 drop(unsafe { Vec::from_raw_parts(_ptr_data, _length, _capacity) });
133 }
134}
135
136impl<T> Default for ArrayBox<T> {
137 fn default() -> Self {
138 ArrayBox::from_vector(vec![])
139 }
140}
141
142impl<T> Drop for ArrayBox<T> {
143 fn drop(&mut self) {
144 Self::free_buffer(self.data, self.length, self.capacity, self.owned);
145 self.data = std::ptr::null_mut();
146 self.length = 0;
147 self.capacity = 0;
148 }
149}
150
151impl<T> ArrayBox<T>
152where
153 T: Default + Copy,
154{
155 pub fn byte_size(count: usize) -> usize {
156 std::mem::size_of::<T>() * count
157 }
158
159 pub fn new_with(element: T, amount: usize) -> ArrayBox<T> {
160 ArrayBox::<T>::from_vector(vec![element; amount])
161 }
162}
163
164#[cfg(test)]
165mod test {
166 use super::*;
167
168 #[test]
169 fn default_array_u8() {
170 let array = ArrayBox::<u8>::default();
171 assert_eq!(array.capacity, 0);
172 assert_eq!(array.length, 0);
173 assert_eq!(array.data.is_null(), false);
174 }
175
176 #[test]
177 fn new_array_u8() {
178 let array = ArrayBox::<u8>::from_vector(vec![0, 1, 2, 3, 4]);
179 assert_eq!(array.capacity, 5);
180 assert_eq!(array.length, 5);
181 assert_eq!(array.data.is_null(), false);
182 }
183}