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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use hicc::{AbiClass, AbiType, ClassArray, ClassMutArray};
use std::slice;
hicc::cpp! {
#include <array>
}
hicc::import_class! {
#[cpp(class = "template<class T, size_t N> std::array<T, N>")]
pub class array<T> {
#[cpp(method = "const T* data() const")]
fn data(&self) -> *const T;
/// ```
/// use hicc_std::ArrayInt;
/// let array = ArrayInt::new_10();
/// assert_eq!(array.size(), 10);
/// ```
#[cpp(method = "size_t size() const")]
pub fn size(&self) -> usize;
/// ```
/// use hicc_std::ArrayInt;
/// let array = ArrayInt::new_10();
/// assert!(!array.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.size() == 0
}
/// ```
/// use hicc_std::ArrayInt;
/// let mut array = ArrayInt::new_10();
/// assert_eq!(array.get(0), Some(&0));
/// assert_eq!(array.get(10), None);
/// ```
pub fn get(&self, pos: usize) -> Option<T::OutputRef<'_>> {
if pos < self.size() {
return unsafe { Some(self._at(pos)) };
}
None
}
#[cpp(method = "const T& at(size_t) const")]
unsafe fn _at(&self, pos: usize) -> &T;
/// ```
/// use hicc_std::ArrayInt;
/// let mut array = ArrayInt::new_10();
/// assert_eq!(array.get_mut(0), Some(&mut 0));
/// assert_eq!(array.get(10), None);
/// ```
pub fn get_mut(&mut self, pos: usize) -> Option<T::OutputRefMut<'_>> {
if pos < self.size() {
return unsafe { Some(self._at_mut(pos)) };
}
None
}
#[cpp(method = "T& at(size_t)")]
unsafe fn _at_mut(&mut self, pos: usize) -> &mut T;
/// ```
/// use hicc_std::ArrayInt;
/// let array = ArrayInt::new_10();
/// assert_eq!(array.back(), Some(&0));
/// ```
pub fn back(&self) -> Option<T::OutputRef<'_>> {
if !self.is_empty() {
return unsafe { Some(self._back()) };
}
None
}
#[cpp(method = "const T& back() const")]
unsafe fn _back(&self) -> &T;
/// ```
/// use hicc_std::ArrayInt;
/// let mut array = ArrayInt::new_10();
/// assert!(array.back_mut().is_some());
/// *array.back_mut().unwrap() += 1;
/// assert_eq!(array.back_mut(), Some(&mut 1));
/// ```
pub fn back_mut(&mut self) -> Option<T::OutputRefMut<'_>> {
if !self.is_empty() {
return unsafe { Some(self._back_mut()) };
}
None
}
#[cpp(method = "T& back()")]
unsafe fn _back_mut(&mut self) -> &mut T;
/// ```
/// use hicc_std::ArrayInt;
/// let array = ArrayInt::new_10();
/// assert!(array.front().is_some());
/// ```
pub fn front(&self) -> Option<T::OutputRef<'_>> {
if !self.is_empty() {
return unsafe { Some(self._front()) };
}
None
}
#[cpp(method = "const T& front() const")]
unsafe fn _front(&self) -> &T;
/// ```
/// use hicc_std::ArrayInt;
/// let mut array = ArrayInt::new_10();
/// assert!(array.front().is_some());
/// *array.front_mut().unwrap() += 1;
/// assert_eq!(array.front(), Some(&1));
/// ```
pub fn front_mut(&mut self) -> Option<T::OutputRefMut<'_>> {
if !self.is_empty() {
return unsafe { Some(self._front_mut()) };
}
None
}
#[cpp(method = "T& front()")]
unsafe fn _front_mut(&mut self) -> &mut T;
/// ```
/// use hicc_std::ArrayInt;
/// let mut array = ArrayInt::new_10();
/// array.fill(&1);
/// assert_eq!(array.front(), Some(&1));
/// ```
#[cpp(method = "void fill(const T&)")]
pub fn fill(&mut self, val: &T);
}
}
unsafe impl<T: AbiType + Sync> Send for array<T> {}
unsafe impl<T: AbiType + Sync> Sync for array<T> {}
impl<T: Sized + 'static> array<hicc::Pod<T>> {
/// ```
/// use hicc_std::ArrayInt;
/// let mut array = ArrayInt::new_10();
/// array.fill(&1);
/// array.as_slice().iter().for_each(|v| println!("{v}"));
/// ```
pub fn as_slice(&self) -> &[T] {
let data = self.data();
let size = self.size();
unsafe { slice::from_raw_parts(data, size) }
}
/// ```
/// use hicc_std::ArrayInt;
/// let mut array = ArrayInt::new_10();
/// array.fill(&1);
/// array.as_slice_mut().iter_mut().for_each(|v| {*v += 1;});
/// ```
pub fn as_slice_mut(&mut self) -> &mut [T] {
let data = self.data();
let size = self.size();
unsafe { slice::from_raw_parts_mut(data.cast_mut(), size) }
}
}
impl<T: AbiClass + 'static> array<T> {
/// ```
/// use hicc_std::{ArrayString, string};
/// let mut array = ArrayString::new_10();
/// array.fill(&string::from(hicc::cstr!("hello")));
/// array.as_array().iter().for_each(|msg| {
/// // ...
/// println!("msg.size() = {}", msg.size());
/// });
/// ```
pub fn as_array(&self) -> ClassArray<'_, T> {
unsafe { self.data().into_array(self.size()) }
}
/// ```
/// use hicc_std::{ArrayString, string};
/// let mut array = ArrayString::new_10();
/// array.fill(&string::from(hicc::cstr!("hello")));
/// array.as_mut_array().iter_mut().for_each(|mut msg| {
/// msg.append(1, b'c' as i8);
/// });
/// ```
pub fn as_mut_array(&mut self) -> ClassMutArray<'_, T> {
unsafe { self.data().into_mut_ptr().into_mut_array(self.size()) }
}
}