PbDecoder

Struct PbDecoder 

Source
pub struct PbDecoder<R: PbRead> {
    pub ignore_repeated_cap_err: bool,
    pub ignore_wrong_len: bool,
    /* private fields */
}
Expand description

Decoder that reads Protobuf bytes and decodes them into Rust types.

Main interface for decoding Protobuf messages. Reads bytes from an underlying PbRead instance.

§Example

Decoding a Protobuf message:

use micropb::{PbRead, PbDecoder, MessageDecode, DecodeError};


let data = [0x08, 0x96, 0x01];
// Slices implement `PbRead` out of the box
let mut decoder = PbDecoder::new(data.as_slice());

let mut message = ProtoMessage::default();
message.decode(&mut decoder, data.len())?;

§Reducing Code Size

To prevent multiple monomorphizations and increased code size, make sure you instantiate PbDecoder with only one reader type across the whole application. If multiple readers need to be supported, wrap them in an enum or use a trait object.

Likewise, try to only use one set of string, vec, and map types across the application to reduce code size.

Fields§

§ignore_repeated_cap_err: bool

If this flag is set, then the decoder will never report a capacity error when decoding repeated fields. When the container is filled, the decoder will instead ignore excess elements on the wire. The decoder will still report capacity errors when decoding bytes and string values that exceed their fixed containers.

§ignore_wrong_len: bool

If this flag is set, then the decoder will never report a WrongLen error if a length-delimited repeated or map field’s actual length does not match its length prefix. Note that other errors can still happen down the line if the length is wrong.

Implementations§

Source§

impl<R: PbRead> PbDecoder<R>

Source

pub fn new(reader: R) -> Self

Construct a new decoder from a PbRead.

Source

pub fn into_reader(self) -> R

Transform the decoder into the underlying reader.

Source

pub fn as_reader(&self) -> &R

Get reference to underlying reader.

Source

pub fn bytes_read(&self) -> usize

Get the number of bytes that the decoder has consumed from the reader.

Source

pub fn decode_varint32(&mut self) -> Result<u32, DecodeError<R::Error>>

Decode an uint32.

Source

pub fn decode_varint64(&mut self) -> Result<u64, DecodeError<R::Error>>

Decode an uint64.

Source

pub fn decode_int64(&mut self) -> Result<i64, DecodeError<R::Error>>

Decode an int64.

Source

pub fn decode_int32(&mut self) -> Result<i32, DecodeError<R::Error>>

Decode an int32.

Source

pub fn decode_sint32(&mut self) -> Result<i32, DecodeError<R::Error>>

Decode an sint32.

Source

pub fn decode_sint64(&mut self) -> Result<i64, DecodeError<R::Error>>

Decode an sint64.

Source

pub fn decode_bool(&mut self) -> Result<bool, DecodeError<R::Error>>

Decode a bool.

Source

pub fn decode_fixed32(&mut self) -> Result<u32, DecodeError<R::Error>>

Decode a fixed32.

Source

pub fn decode_fixed64(&mut self) -> Result<u64, DecodeError<R::Error>>

Decode a fixed64.

Source

pub fn decode_fixed64_as_32(&mut self) -> Result<u32, DecodeError<R::Error>>

Decode a fixed64 but keep only the lower 32 bits.

Avoids 64-bit operations for fixed64 if only the lower bits are needed. This can have performance benefits on 32-bit architectures.

Source

pub fn decode_sfixed32(&mut self) -> Result<i32, DecodeError<R::Error>>

Decode a sfixed32.

Source

pub fn decode_sfixed64(&mut self) -> Result<i64, DecodeError<R::Error>>

Decode a sfixed64.

Source

pub fn decode_sfixed64_as_32(&mut self) -> Result<i32, DecodeError<R::Error>>

Decode a sfixed64 but keep only the lower 32 bits.

Avoids 64-bit operations for sfixed64 if only the lower bits are needed. This can have performance benefits on 32-bit architectures.

Source

pub fn decode_float(&mut self) -> Result<f32, DecodeError<R::Error>>

Decode a float.

Source

pub fn decode_double(&mut self) -> Result<f64, DecodeError<R::Error>>

Decode a double.

Source

pub fn decode_tag(&mut self) -> Result<Tag, DecodeError<R::Error>>

Decode a Protobuf tag.

Source

pub fn decode_string<S: PbString>( &mut self, string: &mut S, presence: Presence, ) -> Result<(), DecodeError<R::Error>>

Decode a string into a PbString container.

The string container’s existing contents will be replaced by the string decoded from the wire. However, if presence is implicit and the new string is empty, the existing string will remain unchanged.

§Errors

If the length of the string on the wire exceeds the fixed capacity of the string container, return DecodeError::Capacity. If the string on the wire if not UTF-8, return DecodeError::Utf8.

Source

pub fn decode_bytes<S: PbBytes>( &mut self, bytes: &mut S, presence: Presence, ) -> Result<(), DecodeError<R::Error>>

Decode a bytes into a PbVec<u8> container.

The byte container’s existing contents will be replaced by the bytes decoded from the wire. However, if presence is implicit and the new bytes is empty, the existing container will remain unchanged.

§Errors

If the length of the bytes on the wire exceeds the fixed capacity of the byte container, return DecodeError::Capacity.

Source

pub fn decode_packed<T: Copy, S: PbVec<T>, F: Fn(&mut Self) -> Result<T, DecodeError<R::Error>>>( &mut self, vec: &mut S, decoder: F, ) -> Result<(), DecodeError<R::Error>>

Decode a repeated packed field and append the elements to a PbVec container.

The decoder callback determines how each element is decoded from the wire. If the number of elements on the wire exceeds the remaining fixed capacity of the container and the ignore_repeated_cap_err flag is not set, return DecodeError::Capacity.

Source

pub fn decode_map_elem<K: Default, V: Default, UK: Fn(&mut K, &mut Self) -> Result<(), DecodeError<R::Error>>, UV: Fn(&mut V, &mut Self) -> Result<(), DecodeError<R::Error>>>( &mut self, key_update: UK, val_update: UV, ) -> Result<Option<(K, V)>, DecodeError<R::Error>>

Decode a Protobuf map key-value pair from the decoder.

According the the Protobuf spec, the key-value pair is formatted as a Protobuf message with the key in field 1 and the value in field 2. Other field numbers are ignored.

The key_update and val_update callbacks are expected to decode the key and value respectively. If either key or value field is not found, return None.

Source

pub fn skip_bytes(&mut self, bytes: usize) -> Result<(), DecodeError<R::Error>>

Consume some bytes from the reader.

If reader reached EOF before the specified number of bytes are skipped, return DecodeError::UnexpectedEof.

Source

pub fn skip_wire_value( &mut self, wire_type: u8, ) -> Result<(), DecodeError<R::Error>>

Skip the next Protobuf value/payload on the wire.

The type of the Protobuf payload is determined by wire_type, which must be a valid Protobuf wire type. This is mainly used to skip unknown fields.

Source

pub fn decode_message<M: MessageDecode + Default>( &mut self, len: usize, ) -> Result<M, DecodeError<R::Error>>

Decode a new message from the wire.

Trait Implementations§

Source§

impl<R: Debug + PbRead> Debug for PbDecoder<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for PbDecoder<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for PbDecoder<R>
where R: RefUnwindSafe,

§

impl<R> Send for PbDecoder<R>
where R: Send,

§

impl<R> Sync for PbDecoder<R>
where R: Sync,

§

impl<R> Unpin for PbDecoder<R>
where R: Unpin,

§

impl<R> UnwindSafe for PbDecoder<R>
where R: UnwindSafe,

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

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.