Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created April 8, 2018 21:42
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 mitsuhiko/f6478a0dd1ef174b33c63d905babc89a to your computer and use it in GitHub Desktop.
Save mitsuhiko/f6478a0dd1ef174b33c63d905babc89a to your computer and use it in GitHub Desktop.
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);
}
}
@jonhoo
Copy link

jonhoo commented Apr 10, 2018

For those that come across this through whatever magical means, note that the third argument to ptr::copy_nonoverlapping is count, not size. So the last argument to both those calls should be 1, not mem::size_of::<T>(). See also mit-pdos/noria@36b3485.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment