Macro code_spells::imperio

source ·
macro_rules! imperio {
    ($will:expr) => { ... };
    ($will:expr, $src:ty => $dst:ty) => { ... };
    ($will:expr, $src:expr => $dst:expr) => { ... };
}
Expand description

Alias for mem::transmute. Disregard the rules, force the type system to do what you want!

Safety

This spell is unforgivable for a reason, see the documentation of mem::transmute for more details.

Examples

let a = [0_u8; 4];
let b: u32 = unforgivable! { imperio!(a) };
assert_eq!(b, 0);
let mut c = unforgivable! { imperio!(b, u32 => [u8; 4]) };
assert_eq!(c, [0; 4]);
let d = &mut c;
assert_eq!(unforgivable! { imperio!(d, &mut [u8; 4] => &[u8; 4]) }, &c)

Force a pointer to become a function pointer!

fn foo() -> i32 { 0 }
let pointer = foo as *const ();
let function = unforgivable! {
    imperio!(pointer, *const () => fn() -> i32)
};
assert_eq!(function(), 0);