#![no_std]
#[macro_export]
#[rustfmt::skip]
macro_rules! const_arr {
([$TYPE:ty; $SIZE:literal], $func_name:ident) => {
{
const TEMP_ITEM: $TYPE = $func_name(0);
let mut arr: [$TYPE; $SIZE] = [TEMP_ITEM; $SIZE];
let mut ind = 0;
while ind < $SIZE {
arr[ind] = $func_name(ind);
ind += 1;
}
arr
}
};
([$TYPE:ty; $SIZE:literal], |$name:ident| $body:expr) => {
{
#[allow(non_upper_case_globals)]
let mut arr: [$TYPE; $SIZE] = {
const $name: usize = 0;
const TEMP_ITEM: $TYPE = $body;
[TEMP_ITEM; $SIZE]
};
let mut $name = 0;
while $name < $SIZE {
arr[$name] = $body;
$name += 1;
}
arr
}
};
([$TYPE:ty; $SIZE:literal], |_| $body:expr ) => {
{
const TEMP_ITEM: $TYPE = $body;
[TEMP_ITEM; $SIZE]
}
};
() => {compile_error!("Please specify array type TYPE: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
([$type:ty; $size:literal]) => {compile_error!("Please specify init function INIT_FN: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
([$type:ty; $size:literal], ) => {compile_error!("Please specify init function INIT_FN: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
([$type:ty; $size:literal], ||) => {compile_error!("Init function has wrong format. It should be |i| i: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
([$type:ty; $size:literal], || $_wha:tt) => {compile_error!("Init function has wrong format. It should be |i| i: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
([$type:ty; $size:literal], $num:literal) => {compile_error!("Please add |_| to last argument to turn it to closure: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
($type:ty) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
($type:ty, ) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
($type:ty,$size:literal) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
($type:ty,$size:literal, $_:tt) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
($type:ty,$size:literal, |$_:tt| $_n2:tt) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
}
#[macro_export]
#[rustfmt::skip]
macro_rules! make_const_arr {
($NAME:ident, [$TYPE:ty; $SIZE:literal], $func_name:ident ) => {
const $NAME: [$TYPE; $SIZE] = {
const TEMP_ITEM: $TYPE = $func_name(0);
let mut arr: [$TYPE; $SIZE] = [TEMP_ITEM; $SIZE];
let mut ind = 0;
while ind < $SIZE {
arr[ind] = $func_name(ind);
ind += 1;
}
arr
}
;
};
($NAME:ident, [$TYPE:ty; $SIZE:literal], |$name:ident| $body:expr ) => {
const $NAME: [$TYPE; $SIZE] = {
#[allow(non_upper_case_globals)]
let mut arr: [$TYPE; $SIZE] = {
const $name: usize = 0;
const TEMP_ITEM: $TYPE = $body;
[TEMP_ITEM; $SIZE]
};
let mut $name = 0;
while $name < $SIZE {
arr[$name] = $body;
$name += 1;
}
arr
};
};
($NAME:ident, [$TYPE:ty; $SIZE:literal], |_| $body:expr ) => {
const $NAME: [$TYPE; $SIZE] = {
const TEMP_ITEM: $TYPE = $body;
[TEMP_ITEM; $SIZE]
};
};
() => { compile_error!("Please specify array name ARR_NAME: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($_:literal) => { compile_error!("Please specify array name ARR_NAME: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident) => { compile_error!("Please specify array type TYPE: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, ) => { make_const_arr!($NAME); };
($NAME:ident, [$type:ty]) => { compile_error!("Please add SIZE to array type: It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, [$type:ty;]) => { compile_error!("Please add SIZE to array type: It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, [$type:ty;$size:literal]) => { compile_error!("Please specify init function INIT_FN: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, [$type:ty;$size:literal], $num:literal) => { compile_error!("Please add |_| to last argument to turn it to closure: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, $_:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, $_n1:tt, $_n2:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };($NAME:ident, $_n1:tt, $_n2:tt, $_n3:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, $_n1:tt, $_n2:tt, $_fn_name:ident) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
($NAME:ident, $_n1:tt, $_n2:tt, |$_cl:tt| $_b:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
}