Skip to content

Instantly share code, notes, and snippets.

@DutchGhost
Created September 23, 2020 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DutchGhost/558c1f4e4f95e560d409b66fb832e97e to your computer and use it in GitHub Desktop.
Save DutchGhost/558c1f4e4f95e560d409b66fb832e97e to your computer and use it in GitHub Desktop.
transmute!
#![feature(untagged_unions)]
#![feature(const_generics, const_evaluatable_checked)]
#![allow(incomplete_features)]
use core::mem::ManuallyDrop;
struct Bool<const B: bool>;
trait True {}
impl True for Bool<true> {}
unsafe fn transmute<T, U>(t: T) -> U
where
Bool<{
core::mem::size_of::<T>() == core::mem::size_of::<U>()
}>: True
{
union Transmute<T, U> {
t: ManuallyDrop<T>,
u: ManuallyDrop<U>
}
ManuallyDrop::into_inner(Transmute { t: ManuallyDrop::new(t) }.u)
}
fn main() {
unsafe {
// let x = transmute::<u8, u16>(0);
let y = transmute::<u8, i8>(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment