Struct quick_protobuf::reader::Reader [−][src]
pub struct Reader { /* fields omitted */ }
A struct to read protobuf data
Contrary to BytesReader
, this struct will own a buffer
Examples
// FooBar is a message generated from a proto file // In particular it implements the `MessageRead` trait, containing a `from_reader` function. use foo_bar::FooBar; use quick_protobuf::Reader; fn main() { // create a reader, which will parse the protobuf binary file and pop events // this reader will read the entire file into an internal buffer let mut reader = Reader::from_file("/path/to/binary/protobuf.bin") .expect("Cannot read input file"); // Use the generated module fns with the reader to convert your data into rust structs. // // Depending on your input file, the message can or not be prefixed with the encoded length // for instance, a *stream* which contains several messages generally split them using this // technique (see https://developers.google.com/protocol-buffers/docs/techniques#streaming) // // To read a message without a length prefix you can directly call `FooBar::from_reader`: // let foobar = reader.read(FooBar::from_reader).expect("Cannot read FooBar message"); // // Else to read a length then a message, you can use: let foobar: FooBar = reader.read(|r, b| r.read_message(b)) .expect("Cannot read FooBar message"); // Reader::read_message uses `FooBar::from_reader` internally through the `MessageRead` // trait. println!("Found {} foos and {} bars!", foobar.foos.len(), foobar.bars.len()); }
Methods
impl Reader
[src]
impl Reader
pub fn from_reader<R: Read>(r: R, capacity: usize) -> Result<Reader>
[src]
pub fn from_reader<R: Read>(r: R, capacity: usize) -> Result<Reader>
Creates a new Reader
pub fn from_file<P: AsRef<Path>>(src: P) -> Result<Reader>
[src]
pub fn from_file<P: AsRef<Path>>(src: P) -> Result<Reader>
Creates a new Reader
out of a file path
pub fn from_bytes(bytes: Vec<u8>) -> Reader
[src]
pub fn from_bytes(bytes: Vec<u8>) -> Reader
Creates a new reader consuming the bytes
pub fn read<'a, M, F>(&'a mut self, read: F) -> Result<M> where
F: FnMut(&mut BytesReader, &'a [u8]) -> Result<M>,
[src]
pub fn read<'a, M, F>(&'a mut self, read: F) -> Result<M> where
F: FnMut(&mut BytesReader, &'a [u8]) -> Result<M>,
Run a BytesReader
dependent function
pub fn inner(&mut self) -> &mut BytesReader
[src]
pub fn inner(&mut self) -> &mut BytesReader
Gets the inner BytesReader
pub fn buffer(&self) -> &[u8]
[src]
pub fn buffer(&self) -> &[u8]
Gets the buffer used internally