pub struct BytesReader { /* private fields */ }
Expand description

A struct to read protocol binary files

Examples


// FooBar is a message generated from a proto file
// in parcicular it contains a `from_reader` function
use foo_bar::FooBar;
use quick_protobuf::{MessageRead, BytesReader};

fn main() {
    // bytes is a buffer on the data we want to deserialize
    // typically bytes is read from a `Read`:
    // r.read_to_end(&mut bytes).expect("cannot read bytes");
    let mut bytes: Vec<u8>;

    // we can build a bytes reader directly out of the bytes
    let mut reader = BytesReader::from_bytes(&bytes);

    // now using the generated module decoding is as easy as:
    let foobar = FooBar::from_reader(&mut reader, &bytes).expect("Cannot read FooBar");

    // if instead the buffer contains a length delimited stream of message we could use:
    // while !r.is_eof() {
    //     let foobar: FooBar = r.read_message(&bytes).expect(...);
    //     ...
    // }
    println!("Found {} foos and {} bars", foobar.foos.len(), foobar.bars.len());
}

Implementations§

source§

impl BytesReader

source

pub fn from_bytes(bytes: &[u8]) -> BytesReader

Creates a new reader from chunks of data

source

pub fn next_tag(&mut self, bytes: &[u8]) -> Result<u32>

Reads next tag, None if all bytes have been read

source

pub fn read_u8(&mut self, bytes: &[u8]) -> Result<u8>

Reads the next byte

source

pub fn read_varint32(&mut self, bytes: &[u8]) -> Result<u32>

Reads the next varint encoded u64

source

pub fn read_varint64(&mut self, bytes: &[u8]) -> Result<u64>

Reads the next varint encoded u64

source

pub fn read_int32(&mut self, bytes: &[u8]) -> Result<i32>

Reads int32 (varint)

source

pub fn read_int64(&mut self, bytes: &[u8]) -> Result<i64>

Reads int64 (varint)

source

pub fn read_uint32(&mut self, bytes: &[u8]) -> Result<u32>

Reads uint32 (varint)

source

pub fn read_uint64(&mut self, bytes: &[u8]) -> Result<u64>

Reads uint64 (varint)

source

pub fn read_sint32(&mut self, bytes: &[u8]) -> Result<i32>

Reads sint32 (varint)

source

pub fn read_sint64(&mut self, bytes: &[u8]) -> Result<i64>

Reads sint64 (varint)

source

pub fn read_fixed64(&mut self, bytes: &[u8]) -> Result<u64>

Reads fixed64 (little endian u64)

source

pub fn read_fixed32(&mut self, bytes: &[u8]) -> Result<u32>

Reads fixed32 (little endian u32)

source

pub fn read_sfixed64(&mut self, bytes: &[u8]) -> Result<i64>

Reads sfixed64 (little endian i64)

source

pub fn read_sfixed32(&mut self, bytes: &[u8]) -> Result<i32>

Reads sfixed32 (little endian i32)

source

pub fn read_float(&mut self, bytes: &[u8]) -> Result<f32>

Reads float (little endian f32)

source

pub fn read_double(&mut self, bytes: &[u8]) -> Result<f64>

Reads double (little endian f64)

source

pub fn read_bool(&mut self, bytes: &[u8]) -> Result<bool>

Reads bool (varint, check if == 0)

source

pub fn read_enum<E: From<i32>>(&mut self, bytes: &[u8]) -> Result<E>

Reads enum, encoded as i32

source

pub fn read_bytes<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a [u8]>

Reads bytes (Vec)

source

pub fn read_string<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a str>

Reads string (String)

source

pub fn read_packed<'a, M, F>( &mut self, bytes: &'a [u8], read: F ) -> Result<Vec<M>>
where F: FnMut(&mut BytesReader, &'a [u8]) -> Result<M>,

Reads packed repeated field (Vec)

Note: packed field are stored as a variable length chunk of data, while regular repeated fields behaves like an iterator, yielding their tag everytime

source

pub fn read_packed_fixed<'a, M>(&mut self, bytes: &'a [u8]) -> Result<&'a [M]>

Reads packed repeated field where M can directly be transmutted from raw bytes

Note: packed field are stored as a variable length chunk of data, while regular repeated fields behaves like an iterator, yielding their tag everytime

source

pub fn read_message<'a, M>(&mut self, bytes: &'a [u8]) -> Result<M>
where M: MessageRead<'a>,

Reads a nested message

First reads a varint and interprets it as the length of the message

source

pub fn read_message_by_len<'a, M>( &mut self, bytes: &'a [u8], len: usize ) -> Result<M>
where M: MessageRead<'a>,

Reads a nested message

Reads just the message and does not try to read it’s size first.

  • ‘len’ - The length of the message to be read.
source

pub fn read_map<'a, K, V, F, G>( &mut self, bytes: &'a [u8], read_key: F, read_val: G ) -> Result<(K, V)>
where F: FnMut(&mut BytesReader, &'a [u8]) -> Result<K>, G: FnMut(&mut BytesReader, &'a [u8]) -> Result<V>, K: Debug + Default, V: Debug + Default,

Reads a map item: (key, value)

source

pub fn read_unknown(&mut self, bytes: &[u8], tag_value: u32) -> Result<()>

Reads unknown data, based on its tag value (which itself gives us the wire_type value)

source

pub fn len(&self) -> usize

Gets the remaining length of bytes not read yet

source

pub fn is_eof(&self) -> bool

Checks if self.len == 0

source

pub fn read_to_end(&mut self)

Advance inner cursor to the end

Trait Implementations§

source§

impl Clone for BytesReader

source§

fn clone(&self) -> BytesReader

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for BytesReader

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for BytesReader

source§

fn eq(&self, other: &BytesReader) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for BytesReader

source§

impl StructuralEq for BytesReader

source§

impl StructuralPartialEq for BytesReader

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.