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
#![no_std]
#[macro_export]
macro_rules! concat_arrays_size {
($( $array:tt ),*) => {{
0 $(+ $array.len())*
}};
}
#[macro_export]
macro_rules! concat_arrays {
($t:ty; $( $array:tt ),*; $init_value:expr) => ({
const __ARRAY_SIZE__: usize = $crate::concat_arrays_size!($($array),*);
const __CONCAT__: [$t; __ARRAY_SIZE__] = {
let mut result = [$init_value; __ARRAY_SIZE__];
let mut result_index = 0;
$(
let mut index = 0;
while index < $array.len() {
result[result_index] = $array[index];
result_index += 1;
index += 1;
}
)*
["Initialization Failed"][(result_index != __ARRAY_SIZE__) as usize];
result
};
__CONCAT__
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple_concat() {
const A: [u32; 3] = [1, 2, 3];
const B: [u32; 3] = [4, 5, 6];
const C: [u32; concat_arrays_size!(A, B)] = concat_arrays!(u32; A, B; u32::MIN);
assert_eq!([1, 2, 3, 4, 5, 6], C);
}
#[test]
fn test_different_sizes() {
const A: [u32; 3] = [1, 2, 3];
const B: [u32; 2] = [4, 5];
const C: [u32; concat_arrays_size!(A, B)] = concat_arrays!(u32; A, B; u32::MIN);
assert_eq!([1, 2, 3, 4, 5], C);
}
}