quick_protobuf::reader

Struct BytesReader

Source
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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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>,

Source§

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>,

Source§

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.