Macro make_const_arr

Source
macro_rules! make_const_arr {
    ($NAME:ident, [$TYPE:ty; $SIZE:literal], $func_name:ident ) => { ... };
    ($NAME:ident, [$TYPE:ty; $SIZE:literal], |$name:ident| $body:expr ) => { ... };
    ($NAME:ident, [$TYPE:ty; $SIZE:literal], |_| $body:expr ) => { ... };
    () => { ... };
    ($_:literal) => { ... };
    ($NAME:ident) => { ... };
    ($NAME:ident, ) => { ... };
    ($NAME:ident, [$type:ty]) => { ... };
    ($NAME:ident, [$type:ty;]) => { ... };
    ($NAME:ident, [$type:ty;$size:literal]) => { ... };
    ($NAME:ident, [$type:ty;$size:literal], $num:literal) => { ... };
    ($NAME:ident, $_:tt) => { ... };
    ($NAME:ident, $_n1:tt, $_n2:tt) => { ... };
    ($NAME:ident, $_n1:tt, $_n2:tt, $_n3:tt) => { ... };
    ($NAME:ident, $_n1:tt, $_n2:tt, $_fn_name:ident) => { ... };
    ($NAME:ident, $_n1:tt, $_n2:tt, |$_cl:tt| $_b:tt) => { ... };
}
Expand description

§Wrapper around const_arr macro. Allows to specify the type of an array single time.

§Supports both closure syntax and const fn initialization.

Usage:

make_const_arr!(ARR_NAME, [TYPE; SIZE], CONST_INIT_FN);

Desugars to:

const ARR_NAME: [TYPE; SIZE] = const_arr!([TYPE; SIZE], CONST_INIT_FN);
  • CONST_INIT_FN is const function or const-like closure from array index(usize) to TYPE

Examples:

use const_array_init::make_const_arr;
 
make_const_arr!(ARR1, [i32; 5], |i| i as i32 + 1);
assert_eq!(ARR1, [1, 2, 3, 4, 5]);
 
const fn to_i32_plus_one(n: usize) -> i32 {
    n as i32 + 1
}
 
make_const_arr!(ARR2, [i32; 5], to_i32_plus_one);
assert_eq!(ARR2, [1, 2, 3, 4, 5]);