In my program I have constants
pub const HEIGHT: usize = 6;
pub const WIDTH: usize = 7;
Now I want my program to use the smallest unsigned integer with at least HEIGHT * WIDTH bits. So intuitively something like
type CustomInt = if HEIGHT * WIDTH < 64 {
u64
} else {
u128
}
Now this does not work of course, as the type needs to be known by the compiler. So I tried doing it with macros, but couldn't get it to work.
Here's what I've tried so far:
Declarative Macros seem to not be powerful enough to make comparisons ( > 64 ).
I tried using a Procedural Macro: I defined the constants with macros as well, so HEIGHT!()
is 6 and WIDTH!()
is 7. I then invoke my macro with
custom_macros::uint_min!(WIDTH!() * HEIGHT!())
but instead of evaluating WIDTH!()
and HEIGHT!()
first to 6 and 7 respectively, the precompiler just parses the whole argument as a string to the uint_min
-macro, which lives in another crate and therefore has no knowledge of what WIDTH!()
and HEIGHT!()
are.