Array Transmutery @WaffleLapkin @BoxyUwU
pub unsafe const fn my_zeroed<T>() -> T {
assert!(std::mem::size_of::<T>() <= 256);
unsafe {
std::mem::transmute_copy(&[0u8; 256])
}
}
fn main() {
unsafe {
my_zeroed::<[*mut u32; 64]>();
}
}
Solution
error: expected one of `extern` or `fn`, found keyword `const`
--> examples/misc_1.rs:1:12
|
1 | pub unsafe const fn my_zeroed<T>() -> T {
| -------^^^^^
| | |
| | expected one of `extern` or `fn`
| help: `const` must come before `unsafe`: `const unsafe`
|
= note: keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`
error: could not compile `code` (example "misc_1") due to 1 previous error
As the error says, there is a given order function qualifiers must go in.
Careful readers may also have noticed that the size of [*mut u32; 64] is not necessarily equal to [u8; 256]. On 64bit platforms the array of pointers would be 512 bytes large, 8 bytes * 64 elements = 256 bytes. While this is a logical error and would result in a panic at runtime, the code can never be executed due to the compilation error.