Created
September 27, 2015 14:27
-
-
Save yupferris/c706f24de797f0bc949f to your computer and use it in GitHub Desktop.
Rust transmute/ownership isolated drop case
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
use std::mem; | |
struct Val { | |
value: i32 | |
} | |
impl Val { | |
fn new(value: i32) -> Val { | |
println!("Created {}", value); | |
Val { value: value } | |
} | |
} | |
impl Drop for Val { | |
fn drop(&mut self) { | |
println!("Destroyed {}", self.value); | |
} | |
} | |
fn main() { | |
let val = Box::new(Val::new(32)); | |
let ptr: *mut Val = unsafe { mem::transmute(val) }; | |
let _: Box<Val> = unsafe { mem::transmute(ptr) }; | |
} |
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
~/dev/projects/droptest $ cargo run | |
Compiling droptest v0.1.0 (file:///Users/yupferris/dev/projects/droptest) | |
Running `target/debug/droptest` | |
Created 32 | |
Destroyed 32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment