Trait bytepack::Unpacker [] [src]

pub trait Unpacker {
    fn unpack<T: Packed>(&mut self) -> Result<T>;
    fn unpack_to_end<T: Packed>(&mut self, buf: &mut Vec<T>) -> Result<usize>;
    fn unpack_exact<T: Packed>(&mut self, buf: &mut [T]) -> Result<()>;
}

Unpacker provides the std::io::Read API but for any type T implementing Packed. It does not perform any endianness conversion and thus always reads data using the system endianness.

Example

Example of reading a file containing a few float samples.

use std::fs::File;
use std::iter::repeat;

use bytepack::Unpacker;

fn read_samples(file: &str) -> Vec<f32> {
    let mut file = File::open(file).unwrap();
    let num_samples : u32 = file.unpack().unwrap();
    let mut samples : Vec<f32> = repeat(0f32).take(num_samples as usize).collect();
    file.unpack_exact(&mut samples[..]).unwrap();
    return samples;
}

Required Methods

Unpack a single value of type T.

let mut file = File::open("test").unwrap();
let float : f32 = file.unpack().unwrap();

Unpack values of type T until EOF is reached and place them in buf. An error is returned if the number of bytes read is not a multiple of the size of T.

let mut file = File::open("test").unwrap();
let mut buffer = Vec::<u64>::new();
file.unpack_to_end(&mut buffer).unwrap();

Unpack the exact number of values of type T to fill buf. An error is returned if not enough byte could be read.

let mut file = File::open("test").unwrap();
let mut buffer = vec![0i32; 10];
file.unpack_exact(&mut buffer[..]).unwrap();

Implementors