Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@djg
Created February 9, 2018 05:44
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 djg/ba2af324e9d0be65274c5baf8c88480b to your computer and use it in GitHub Desktop.
Save djg/ba2af324e9d0be65274c5baf8c88480b to your computer and use it in GitHub Desktop.
// Samples are delta-encoded
pub fn unpack_sample<T: Default + ops::AddAssign + Clone>(input: &[u8])
-> Result<Vec<T>, &'static str> {
if input.len() % mem::size_of::<T>() != 0 {
return Err("packed sample data is not aligned");
}
let packed_sample: &[T] = unsafe {
slice::from_raw_parts(input.as_ptr() as *const T,
input.len() / mem::size_of::<T>())
};
let mut unpacked_sample = Vec::<T>::with_capacity(packed_sample.len());
let mut last_sample: T = T::default();
for delta in packed_sample {
last_sample += *delta;
unpacked_sample.push(last_sample);
}
Ok(unpacked_sample)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment