Created
September 23, 2020 12:08
-
-
Save DutchGhost/558c1f4e4f95e560d409b66fb832e97e to your computer and use it in GitHub Desktop.
transmute!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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