I am trying to translate some old embedded C codes to Rust, but I meet with some problems.
When I was using C, I can use the reference (e.g. arr, when arr is an array) to another variable to initialize a global variable. For example:
#define MASK 0xffff0000
int arr[128] = {0};
uint64_t arr_table[2] = {arr + MASK, 0};
I have to declare them at the compile time, so that they can be accessed from extern libraries (e.g. kernel assembly codes).
Is there any equivalent in Rust?
What I have tried
pub static arr: [u32; 128] = [0; 128];
pub static arr_table: [u64; 2] = [&arr as *const [u32; 128] as u64 + MASK, 0];
The compiler told me:
error: pointers cannot be cast to integers during const eval
note: at compile-time, pointers do not have an integer value
note: avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior
I have also tried mem::transmute() to do an unsafe conversion to u64, and the compiler complaint the same thing:
error[E0080]: could not evaluate static initializer
help: this code performed an operation that depends on the underlying bytes representing a pointer
help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
Question
I know it is impossible for the compiler to know the exact address of a static variable at the compile time.
I am not so familiar with the mechanism behind compilation, but I suppose that C has put off the work till link process and implied the linker to give the right values to arr_table. I wonder if it is possible to do the same thing in Rust? Thanks!