Struct quick_protobuf::reader::Reader
source · pub struct Reader { /* private fields */ }
Expand description
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());
}
Implementations§
source§impl Reader
impl Reader
sourcepub fn from_file<P: AsRef<Path>>(src: P) -> Result<Reader>
pub fn from_file<P: AsRef<Path>>(src: P) -> Result<Reader>
Creates a new Reader
out of a file path
sourcepub fn from_bytes(bytes: Vec<u8>) -> Reader
pub fn from_bytes(bytes: Vec<u8>) -> Reader
Creates a new reader consuming the bytes
sourcepub fn inner(&mut self) -> &mut BytesReader
pub fn inner(&mut self) -> &mut BytesReader
Gets the inner BytesReader
Auto Trait Implementations§
impl RefUnwindSafe for Reader
impl Send for Reader
impl Sync for Reader
impl Unpin for Reader
impl UnwindSafe for Reader
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more