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: boolIf 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: boolIf 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>
impl<R: PbRead> PbDecoder<R>
Sourcepub fn into_reader(self) -> R
pub fn into_reader(self) -> R
Transform the decoder into the underlying reader.
Sourcepub fn bytes_read(&self) -> usize
pub fn bytes_read(&self) -> usize
Get the number of bytes that the decoder has consumed from the reader.
Sourcepub fn decode_varint32(&mut self) -> Result<u32, DecodeError<R::Error>>
pub fn decode_varint32(&mut self) -> Result<u32, DecodeError<R::Error>>
Decode an uint32.
Sourcepub fn decode_varint64(&mut self) -> Result<u64, DecodeError<R::Error>>
pub fn decode_varint64(&mut self) -> Result<u64, DecodeError<R::Error>>
Decode an uint64.
Sourcepub fn decode_int64(&mut self) -> Result<i64, DecodeError<R::Error>>
pub fn decode_int64(&mut self) -> Result<i64, DecodeError<R::Error>>
Decode an int64.
Sourcepub fn decode_int32(&mut self) -> Result<i32, DecodeError<R::Error>>
pub fn decode_int32(&mut self) -> Result<i32, DecodeError<R::Error>>
Decode an int32.
Sourcepub fn decode_sint32(&mut self) -> Result<i32, DecodeError<R::Error>>
pub fn decode_sint32(&mut self) -> Result<i32, DecodeError<R::Error>>
Decode an sint32.
Sourcepub fn decode_sint64(&mut self) -> Result<i64, DecodeError<R::Error>>
pub fn decode_sint64(&mut self) -> Result<i64, DecodeError<R::Error>>
Decode an sint64.
Sourcepub fn decode_bool(&mut self) -> Result<bool, DecodeError<R::Error>>
pub fn decode_bool(&mut self) -> Result<bool, DecodeError<R::Error>>
Decode a bool.
Sourcepub fn decode_fixed32(&mut self) -> Result<u32, DecodeError<R::Error>>
pub fn decode_fixed32(&mut self) -> Result<u32, DecodeError<R::Error>>
Decode a fixed32.
Sourcepub fn decode_fixed64(&mut self) -> Result<u64, DecodeError<R::Error>>
pub fn decode_fixed64(&mut self) -> Result<u64, DecodeError<R::Error>>
Decode a fixed64.
Sourcepub fn decode_fixed64_as_32(&mut self) -> Result<u32, DecodeError<R::Error>>
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.
Sourcepub fn decode_sfixed32(&mut self) -> Result<i32, DecodeError<R::Error>>
pub fn decode_sfixed32(&mut self) -> Result<i32, DecodeError<R::Error>>
Decode a sfixed32.
Sourcepub fn decode_sfixed64(&mut self) -> Result<i64, DecodeError<R::Error>>
pub fn decode_sfixed64(&mut self) -> Result<i64, DecodeError<R::Error>>
Decode a sfixed64.
Sourcepub fn decode_sfixed64_as_32(&mut self) -> Result<i32, DecodeError<R::Error>>
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.
Sourcepub fn decode_float(&mut self) -> Result<f32, DecodeError<R::Error>>
pub fn decode_float(&mut self) -> Result<f32, DecodeError<R::Error>>
Decode a float.
Sourcepub fn decode_double(&mut self) -> Result<f64, DecodeError<R::Error>>
pub fn decode_double(&mut self) -> Result<f64, DecodeError<R::Error>>
Decode a double.
Sourcepub fn decode_tag(&mut self) -> Result<Tag, DecodeError<R::Error>>
pub fn decode_tag(&mut self) -> Result<Tag, DecodeError<R::Error>>
Decode a Protobuf tag.
Sourcepub fn decode_string<S: PbString>(
&mut self,
string: &mut S,
presence: Presence,
) -> Result<(), DecodeError<R::Error>>
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.
Sourcepub fn decode_bytes<S: PbBytes>(
&mut self,
bytes: &mut S,
presence: Presence,
) -> Result<(), DecodeError<R::Error>>
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.
Sourcepub 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>>
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.
Sourcepub 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>>
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.
Sourcepub fn skip_bytes(&mut self, bytes: usize) -> Result<(), DecodeError<R::Error>>
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.
Sourcepub fn skip_wire_value(
&mut self,
wire_type: u8,
) -> Result<(), DecodeError<R::Error>>
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.
Sourcepub fn decode_message<M: MessageDecode + Default>(
&mut self,
len: usize,
) -> Result<M, DecodeError<R::Error>>
pub fn decode_message<M: MessageDecode + Default>( &mut self, len: usize, ) -> Result<M, DecodeError<R::Error>>
Decode a new message from the wire.