macro_bits/
incomplete_const_array.rs

1#[macro_export]
2/// Allows the creation of a const array without specifying all elements.
3/// 
4/// ```
5/// use macro_bits::incomplete_const_array;
6/// 
7/// incomplete_const_array! {
8///     #[filler(0)] // Has to be first.
9///     /// Just some array.
10///     pub const ARRAY: [usize; 4] = [
11///         1 => 1337,
12///         3 => 42
13///     ];
14/// }
15/// assert_eq!(ARRAY, [0, 1337, 0, 42]);
16/// ```
17macro_rules! incomplete_const_array {
18    (
19        #[filler($filler:expr)]
20        $(#[$const_attr:meta])*
21        $const_vis:vis const $const_name:ident: [$array_type:ty; $array_length:expr] = [
22            $(
23                $idx:expr => $value:expr
24            ),*
25        ];
26    ) => {
27        $(#[$const_attr])*
28        $const_vis const $const_name: [$array_type; $array_length] = {
29            let mut array = [$filler; $array_length];
30            
31            $(
32                array[$idx] = $value;
33            )*
34
35            array
36        };
37    };
38}