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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! A few utility macros.
//!
//! If you work with byte arrays, consider using the utilities from the
//! `crate::bytes` module instead.
//!
use Array;
use typenum;
use cratearray_from_core;
use cratearray_to_core;
/// Splits an [`Array`] into multiple sub-`Array`s.
///
/// Notice that each sub-`Array` must have a defined size, and the total
/// size of the sub-`Array`s must match the size of the `Aarray` on the
/// right-hand side.
///
/// The macro expects a let-assignment style syntax with a array pattern,
/// where each element happens to be bind a new variable with type `Array`.
///
/// If the size of a sub-`Array` cannot be inferred, you can specify it
/// explicitly by adding `: array <size>` after the variable name, where
/// `<size>` is a type of an `typenum` type-"number".
///
/// This macro is the reversal of the [`crate::merge_array`] macro.
///
/// # Syntax
///
/// ```text
/// split_array!{
/// let split \[
/// ( <name> [ : array <typenum-size> ] , )*
/// \] = <value>;
/// }
/// ```
///
/// # Example
///
/// ```
/// use hybrid_array::Array;
/// use array_fusion::array_from_core;
/// use array_fusion::array_to_core;
/// use array_fusion::split_array;
/// use hybrid_array::sizes::U4;
///
/// // An Array consisting of a 2-byte and a 4-byte sub array
/// let data: Array<_,_> = array_from_core([
/// 0, 42, // foo
/// 0x0, 0x0, 0x12, 0x34, // bar
/// ]);
///
/// // Split it into a `foo` and a `bar`.
/// // The size of foo is inferred by the subsequent usage, while bar is given
/// // an explicit size.
/// split_array!{
/// let split [
/// foo,
/// bar: array U4,
/// ] = data;
/// };
///
/// // Use foo in a way to make its size inferrable, where `from_be_bytes`
/// // gives the size of the core array, and `array_to_core` inferres the
/// // Array size from the size of that core array.
/// let foo = u16::from_be_bytes(array_to_core(foo));
/// assert_eq!(foo, 42);
///
/// // Use bar, actually this could also infer the size, but we specified it
/// // already above.
/// assert_eq!(u32::from_be_bytes(array_to_core(bar)), 0x1234);
/// ```
///
;
}
/// Split a core arrays in core arrays.
///
/// This function is essentially [`split_array`], but on core arrays.
/// In fact, it is implemented using `split_array` and surrounds the right-hand
/// side with [`array_from_core`] and the output arrays with [`array_to_core`].
///
/// Also if you need to specify the size of an array you have to specifiy the
/// full core array.
///
///
/// # Syntax
///
/// ```text
/// split_core_array!{
/// let split \[
/// ( <name> [ : <array-type> ] , )*
/// \] = <value>;
/// }
/// ```
///
/// # Example
///
/// ```
/// use array_fusion::split_core_array;
///
/// let data = [
/// 0, 42, // foo
/// 0x0, 0x0, 0x12, 0x34, // bar
/// ];
///
/// // Split the array, core to core
/// // The size of `foo` is inferred from the subsequent usage of it, whereas
/// // the size of `bar` is explicitly specified.
/// split_core_array! {
/// let split [
/// foo,
/// bar: [_; 4],
/// ] = data;
/// };
///
/// // Use foo in a way to make it's size inferrable
/// let foo = u16::from_be_bytes(foo);
/// assert_eq!(foo, 42);
///
/// // Use bar, actually this would also infere its size, but we specified it
/// // already above.
/// assert_eq!(u32::from_be_bytes(bar), 0x1234);
/// ```
///
$*
};
}
/// Merge multiple [`Array`]s into one combined [`Array`].
///
/// Notice that each component Array's size must be known before hand.
///
/// The macro expects an array list `[a,b,c]` expression with each element
/// being an `Array`.
///
/// The result of this macro will be a single `Array` value that contains
/// the concatination of all vaules from all the given arrays.
///
/// This macro is the reversal of the [`split_array`] macro.
///
///
/// # Syntax
///
/// ```text
/// merge_array!{
/// \[
/// ( <array-vaule> , )*
/// \];
/// }
/// ```
///
/// # Example
///
/// ```
/// use hybrid_array::Array;
/// use hybrid_array::sizes::U2;
/// use hybrid_array::sizes::U4;
/// use array_fusion::merge_array;
///
/// let foo: Array<_, U2> = Array([1, 2]);
/// let bar: Array<_, U4> = Array([0x12, 0x34, 0x56, 0x78]);
///
/// let combi: Array<_,_> = merge_array!(
/// [
/// foo,
/// bar,
/// ]
/// );
///
/// assert_eq!(combi, [
/// 1, 2, // foo
/// 0x12, 0x34, 0x56, 0x78, // bar
/// ]);
/// ```
///
///
///
/// Merge core arrays into a combined core array
///
///
///
/// # Example
///
/// ```
/// use array_fusion::merge_core_array;
///
/// let foo: [u8; 2] = [1, 2];
/// let bar: [u8; 4] = [0x12, 0x34, 0x56, 0x78];
///
/// let combi = merge_core_array!(
/// [
/// foo,
/// bar,
/// ]
/// );
///
/// assert_eq!(combi, [
/// 1, 2, // foo
/// 0x12, 0x34, 0x56, 0x78, // bar
/// ]);
/// ```
///