Skip to content

Instantly share code, notes, and snippets.

@yupferris
Created September 27, 2015 14:27
Show Gist options
  • Save yupferris/c706f24de797f0bc949f to your computer and use it in GitHub Desktop.
Save yupferris/c706f24de797f0bc949f to your computer and use it in GitHub Desktop.
Rust transmute/ownership isolated drop case
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) };
}
~/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