Skip to content

Instantly share code, notes, and snippets.

@ben0x539
Created October 10, 2012 13:57
Show Gist options
  • Save ben0x539/3865798 to your computer and use it in GitHub Desktop.
Save ben0x539/3865798 to your computer and use it in GitHub Desktop.
fn read_struct<R: io::Reader, T>(reader: R, obj: &mut T) {
let size: uint = sys::size_of::<T>();
unsafe {
let borrowed_ptr: &u8 = cast::reinterpret_cast(&obj);
let ptr: *u8 = ptr::to_unsafe_ptr(borrowed_ptr);
do vec::raw::buf_as_slice(ptr, size) |buf: &[u8]| {
let mut_buf: &[mut u8] = cast::reinterpret_cast(&buf);
let ret = reader.read(mut_buf, size);
assert ret == size;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment