I'm writing a macro. I need to do something along these lines:
use core::ffi::c_void;
fn my_fn(_: i32) {}
fn foo(ptr: *mut c_void) {
let fn_ptr = if true {
unsafe { core::mem::transmute(ptr) }
} else {
my_fn
};
(fn_ptr)(17);
}
Basically, the idea is that the void pointer is a function pointer to a function of the same signature as my_fn
. I'm using type inference of the if/else branch to tell the transmute which function pointer type to transmute ptr
to.
However, this code fails:
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> src/lib.rs:7:18
|
7 | unsafe { core::mem::transmute(ptr) }
| ^^^^^^^^^^^^^^^^^^^^
|
= note: source type: `*mut c_void` (64 bits)
= note: target type: `fn(i32) {my_fn}` (0 bits)
The problem is that my_fn
is a zero-sized function item rather than a function pointer. How can I coerce my_fn
to a full function pointer so that I can let type inference do its thing?