I want to generate a static array (doing this at runtime is not an option) with help of a macro.
My attempts are
macro_rules! test {
($($data:expr),*) => {
[ test!(@xform $($data),*) ]
};
(@xform) => { };
(@xform $a:expr) => { be32($a) };
(@xform $a:expr, $($data:expr),*) => { be32($a), test!(@xform $($data),*) };
}
// just for simplicity...
const fn be32(v: u32) -> u32 { v + 1 }
static FILE_HEADER_0: [u32;2] = test!(1, 2);
static FILE_HEADER_1: [u32;2] = [be32(1), be32(2)];
but this fails with
error: macro expansion ignores token `,` and any following
--> src/lib.rs:8:52
|
3 | [ test!(@xform $($data),*) ]
| ------------------------- help: you might be missing a semicolon here: `;`
| |
| caused by the macro expansion here
...
8 | (@xform $a:expr, $($data:expr),*) => { be32($a), test!(@xform $($data),*) };
| ^
|
= note: the usage of `test!` is likely invalid in expression context
I expect that FILE_HEADER_0 is generated like FILE_HEADER_1
Is this possible with normal macro_rules! or do I have to use proc_macro?
std::array::from_fncan beconstwhen used with aconst fn.