Created
April 8, 2018 21:42
-
-
Save mitsuhiko/f6478a0dd1ef174b33c63d905babc89a to your computer and use it in GitHub Desktop.
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; | |
use std::ptr; | |
use std::sync::Arc; | |
use std::collections::HashMap; | |
fn main() { | |
let mut hm = HashMap::new(); | |
let key = (Arc::new("1".to_string()), Arc::new("2".to_string())); | |
let data = vec![key.0.clone(), key.1.clone()]; | |
hm.insert(key, 42); | |
unsafe { | |
let mut stack_key: (Arc<String>, Arc<String>) = mem::uninitialized(); | |
ptr::copy_nonoverlapping(&data[0] as *const Arc<String>, &mut stack_key.0 as *mut Arc<String>, mem::size_of::<Arc<String>>()); | |
ptr::copy_nonoverlapping(&data[1] as *const Arc<String>, &mut stack_key.1 as *mut Arc<String>, mem::size_of::<Arc<String>>()); | |
println!("lookup result: {}", hm[&stack_key]); | |
mem::forget(stack_key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For those that come across this through whatever magical means, note that the third argument to
ptr::copy_nonoverlapping
iscount
, notsize
. So the last argument to both those calls should be1
, notmem::size_of::<T>()
. See also mit-pdos/noria@36b3485.