Skip to content

Instantly share code, notes, and snippets.

@andydude
Created February 4, 2015 02:50
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 andydude/212557250d173ced1103 to your computer and use it in GitHub Desktop.
Save andydude/212557250d173ced1103 to your computer and use it in GitHub Desktop.
TransmuteMemory
#![feature(core)]
extern crate "rustc-serialize" as serialize;
use std::mem::{size_of, transmute};
use std::slice::bytes::copy_memory;
use serialize::hex::ToHex;
pub fn transmute_memory<D: Sized, S: Sized>(dst: &mut D, src: &S) {
unsafe {
let size = size_of::<S>();
assert_eq!(size_of::<D>(), size);
let d = transmute::<(&mut D, usize), &mut [u8]>((dst, size));
let s = transmute::<(&S, usize), &[u8]>((src, size));
copy_memory(d, s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment