Skip to content

Instantly share code, notes, and snippets.

@rrichardson
Created February 3, 2015 20:53
Show Gist options
  • Save rrichardson/517db0c21dc672389c41 to your computer and use it in GitHub Desktop.
Save rrichardson/517db0c21dc672389c41 to your computer and use it in GitHub Desktop.
#[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