Struct quick_protobuf::reader::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
impl BytesReader
sourcepub fn from_bytes(bytes: &[u8]) -> BytesReader
pub fn from_bytes(bytes: &[u8]) -> BytesReader
Creates a new reader from chunks of data
sourcepub fn next_tag(&mut self, bytes: &[u8]) -> Result<u32>
pub fn next_tag(&mut self, bytes: &[u8]) -> Result<u32>
Reads next tag, None
if all bytes have been read
sourcepub fn read_varint32(&mut self, bytes: &[u8]) -> Result<u32>
pub fn read_varint32(&mut self, bytes: &[u8]) -> Result<u32>
Reads the next varint encoded u64
sourcepub fn read_varint64(&mut self, bytes: &[u8]) -> Result<u64>
pub fn read_varint64(&mut self, bytes: &[u8]) -> Result<u64>
Reads the next varint encoded u64
sourcepub fn read_int32(&mut self, bytes: &[u8]) -> Result<i32>
pub fn read_int32(&mut self, bytes: &[u8]) -> Result<i32>
Reads int32 (varint)
sourcepub fn read_int64(&mut self, bytes: &[u8]) -> Result<i64>
pub fn read_int64(&mut self, bytes: &[u8]) -> Result<i64>
Reads int64 (varint)
sourcepub fn read_uint32(&mut self, bytes: &[u8]) -> Result<u32>
pub fn read_uint32(&mut self, bytes: &[u8]) -> Result<u32>
Reads uint32 (varint)
sourcepub fn read_uint64(&mut self, bytes: &[u8]) -> Result<u64>
pub fn read_uint64(&mut self, bytes: &[u8]) -> Result<u64>
Reads uint64 (varint)
sourcepub fn read_sint32(&mut self, bytes: &[u8]) -> Result<i32>
pub fn read_sint32(&mut self, bytes: &[u8]) -> Result<i32>
Reads sint32 (varint)
sourcepub fn read_sint64(&mut self, bytes: &[u8]) -> Result<i64>
pub fn read_sint64(&mut self, bytes: &[u8]) -> Result<i64>
Reads sint64 (varint)
sourcepub fn read_fixed64(&mut self, bytes: &[u8]) -> Result<u64>
pub fn read_fixed64(&mut self, bytes: &[u8]) -> Result<u64>
Reads fixed64 (little endian u64)
sourcepub fn read_fixed32(&mut self, bytes: &[u8]) -> Result<u32>
pub fn read_fixed32(&mut self, bytes: &[u8]) -> Result<u32>
Reads fixed32 (little endian u32)
sourcepub fn read_sfixed64(&mut self, bytes: &[u8]) -> Result<i64>
pub fn read_sfixed64(&mut self, bytes: &[u8]) -> Result<i64>
Reads sfixed64 (little endian i64)
sourcepub fn read_sfixed32(&mut self, bytes: &[u8]) -> Result<i32>
pub fn read_sfixed32(&mut self, bytes: &[u8]) -> Result<i32>
Reads sfixed32 (little endian i32)
sourcepub fn read_float(&mut self, bytes: &[u8]) -> Result<f32>
pub fn read_float(&mut self, bytes: &[u8]) -> Result<f32>
Reads float (little endian f32)
sourcepub fn read_double(&mut self, bytes: &[u8]) -> Result<f64>
pub fn read_double(&mut self, bytes: &[u8]) -> Result<f64>
Reads double (little endian f64)
sourcepub fn read_enum<E: From<i32>>(&mut self, bytes: &[u8]) -> Result<E>
pub fn read_enum<E: From<i32>>(&mut self, bytes: &[u8]) -> Result<E>
Reads enum, encoded as i32
sourcepub fn read_bytes<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a [u8]>
pub fn read_bytes<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a [u8]>
Reads bytes (Vec
sourcepub fn read_string<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a str>
pub fn read_string<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a str>
Reads string (String)
sourcepub fn read_packed<'a, M, F>(
&mut self,
bytes: &'a [u8],
read: F
) -> Result<Vec<M>>
pub fn read_packed<'a, M, F>( &mut self, bytes: &'a [u8], read: F ) -> Result<Vec<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
sourcepub fn read_packed_fixed<'a, M>(&mut self, bytes: &'a [u8]) -> Result<&'a [M]>
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
sourcepub fn read_message<'a, M>(&mut self, bytes: &'a [u8]) -> Result<M>where
M: MessageRead<'a>,
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
sourcepub fn read_message_by_len<'a, M>(
&mut self,
bytes: &'a [u8],
len: usize
) -> Result<M>where
M: MessageRead<'a>,
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.
sourcepub fn read_map<'a, K, V, F, G>(
&mut self,
bytes: &'a [u8],
read_key: F,
read_val: G
) -> Result<(K, V)>
pub fn read_map<'a, K, V, F, G>( &mut self, bytes: &'a [u8], read_key: F, read_val: G ) -> Result<(K, V)>
Reads a map item: (key, value)
sourcepub fn read_unknown(&mut self, bytes: &[u8], tag_value: u32) -> Result<()>
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)
sourcepub fn read_to_end(&mut self)
pub fn read_to_end(&mut self)
Advance inner cursor to the end
Trait Implementations§
source§impl Clone for BytesReader
impl Clone for BytesReader
source§fn clone(&self) -> BytesReader
fn clone(&self) -> BytesReader
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for BytesReader
impl Debug for BytesReader
source§impl PartialEq for BytesReader
impl PartialEq for BytesReader
source§fn eq(&self, other: &BytesReader) -> bool
fn eq(&self, other: &BytesReader) -> bool
self
and other
values to be equal, and is used
by ==
.