Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 20, 2019 21:38
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 rust-play/d07a37dc672c6ed66c6e94a75eb1b375 to your computer and use it in GitHub Desktop.
Save rust-play/d07a37dc672c6ed66c6e94a75eb1b375 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![feature(core_intrinsics)]
use std::intrinsics::discriminant_value;
use std::mem::size_of;
#[derive(Debug)]
#[allow(dead_code)]
enum E {
A = 0x1000_0000,
B = 0,
}
fn main() {
let x = &0xffff_ffffu32 as *const u32 as *const E;
let z = unsafe { std::ptr::read(x) };
assert_eq!(size_of::<E>(), size_of::<u32>());
let actual_discriminant = unsafe { discriminant_value(&z) };
println!("{:?}, discriminant 0x{:x}", z, actual_discriminant);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment