macro_rules! id_name_conv {
($($id:literal -> $name:literal)*) => {
const fn id2name(id: u32) -> &'static str {
match id {
$($id => $name,)*
_ => unreachable!(),
}
}
const fn name2id(name: &'static str) -> u32 {
match name {
$($name => $id,)*
_ => unreachable!(),
}
}
};
}
id_name_conv!(
0 -> "a"
1 -> "b"
);
raises:
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> main.rs:13:23
|
13 | $($name => $id,)*
| ^^^^^
...
20 | / id_name_conv!(
21 | | 0 -> "a"
22 | | 1 -> "b"
23 | | );
| |_____- in this macro invocation
|
= note: this error originates in the macro `id_name_conv` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0015`.
After search I found an open issue, proves that the error is caused by comparing two &'static str
s, and also mentions that comparing two &'static [u8]
s is fine.
Without getting into the confusing error reporting, is there any reason why this cannot be implemented?