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

source

pub fn from_reader<R: Read>(r: R, capacity: usize) -> Result<Reader>

Creates a new Reader

source

pub fn from_file<P: AsRef<Path>>(src: P) -> Result<Reader>

Creates a new Reader out of a file path

source

pub fn from_bytes(bytes: Vec<u8>) -> Reader

Creates a new reader consuming the bytes

source

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

source

pub fn inner(&mut self) -> &mut BytesReader

Gets the inner BytesReader

source

pub fn buffer(&self) -> &[u8]

Gets the buffer used internally

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.