rust/tests/run-pass/option_box_transmute_ptr.rs

16 lines
431 B
Rust
Raw Normal View History

// This tests that the size of Option<Box<i32>> is the same as *const i32.
fn option_box_deref() -> i32 {
let val = Some(Box::new(42));
unsafe {
let ptr: *const i32 = std::mem::transmute::<Option<Box<i32>>, *const i32>(val);
2017-02-14 09:55:30 -06:00
let ret = *ptr;
// unleak memory
std::mem::transmute::<*const i32, Option<Box<i32>>>(ptr);
ret
}
}
fn main() {
assert_eq!(option_box_deref(), 42);
}