Created
February 3, 2015 20:53
-
-
Save rrichardson/517db0c21dc672389c41 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#[inline] | |
fn struct_to_bytes<T>(t: &T) -> &[u8] { unsafe { | |
mem::transmute(raw::Slice::<u8> { | |
data: t as *const T as *const u8, | |
len: mem::size_of::<T>(), | |
}) | |
}} | |
#[inline] | |
fn bytes_to_struct<'a, T>(t: &'a [u8]) -> Option<&'a T> { | |
if t.len() < mem::size_of::<T>() { None } | |
else { unsafe { | |
let raw : raw::Slice<u8> = mem::transmute(t); | |
Some(&*(raw.data as *const T)) | |
} } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment