Skip to main content

transmute

Function transmute 

Source
pub const unsafe fn transmute<Src, Dst>(val: Src) -> Dst
Expand description

Version of transmute that performs size check at monomorphization-time.

Use this instead of core::mem::transmute when it is known that sizes are identical but this cannot be proven by the compiler during type checking and can be proven during monomorphization.

The signature is equivalent to Rust standard library’s unstable transmute_neo and that of RFC 3844.

§Safety

Same as core::mem::transmute.

§Examples

This is typically used in generic code where it’s known that type will have the same size, but the compiler cannot prove it generically.

trait IsU32 {}
impl IsU32 for u32 {}

fn to_u32<T: IsU32>(v: T) -> u32 {
    // `core::mem::transmute` won't work here.
    // SAFETY: We know that `v` is u32!
    unsafe { kernel::mem::transmute(v) }
}

to_u32(1u32);