Skip to content

Instantly share code, notes, and snippets.

@1wilkens
Created January 28, 2014 10:57
Show Gist options
  • Save 1wilkens/8665644 to your computer and use it in GitHub Desktop.
Save 1wilkens/8665644 to your computer and use it in GitHub Desktop.
I am not sure, how to allocate a vector use its fields in a struct I'm returning. My created vectors do not outlive the function call =/
pub struct GzipHeader<'a> {
id: &'a[u8], // length 2
c_meth: u8,
flags: GzipFlags,
m_time: &'a[u8], // length 4
ex_flags: GzipFlags,
os: u8
}
fn read_gzip_header<R: Reader>(r: bitio::BitReader<R>) -> GzipHeader {
// this needs to outlive the function, i think?
let buf: &mut [u8] = vec::with_capacity(10).as_mut_slice();
// i tried to use this to act as the storage behind the returned header buf it failed
let header_data: ~[u8] = vec::with_capacity(10);
match r.read_bytes(buf, 10) {
Some(bytes) => vec::bytes::copy_memory(buf, header_data),
_ => fail!(~"Error while reading GzipHeader!")
};
GzipHeader {
id: header_data.slice(0, 2),
c_meth: header_data[2],
flags: header_data[3] as GzipFlags,
m_time: header_data.slice(4, 8),
ex_flags: header_data[8] as GzipFlags,
os: header_data[9]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment