pub struct PbEncoder<W: PbWrite> { /* private fields */ }Expand description
Encoder that serializes Rust types into Protobuf messages and values.
Main interface for encoding Protobuf messages. Writes bytes to an underlying PbWrite
instance.
§Example
Encoding a Protobuf message:
use micropb::{PbEncoder, PbWrite, MessageEncode};
use micropb::heapless::Vec;
let mut message = ProtoMessage::default();
message.0 = 12;
// If `container-heapless` feature is enabled, then `PbWrite` will be implemented on `heapless::Vec`,
// allowing the encoder to write into it. Same applies to `container-arrayvec` and `alloc`.
let mut encoder = PbEncoder::new(Vec::<u8, 10>::new());
message.encode(&mut encoder)?;§Reducing Code Size
To prevent multiple monomorphizations and increased code size, make sure you instantiate
PbEncoder with only one writer type across the whole application. If multiple writers need to
be supported, wrap them in an enum or use a trait object.
Implementations§
Source§impl<W: PbWrite> PbEncoder<W>
impl<W: PbWrite> PbEncoder<W>
Sourcepub fn into_writer(self) -> W
pub fn into_writer(self) -> W
Transform the encoder into the underlying writer.
Sourcepub fn encode_fixed64_as_32(&mut self, u: u32) -> Result<(), W::Error>
pub fn encode_fixed64_as_32(&mut self, u: u32) -> Result<(), W::Error>
Encode a 32-bit number as fixed64.
Avoids 64-bit operations, which can have benefits on 32-bit architectures.
Sourcepub fn encode_sfixed64_as_32(&mut self, i: i32) -> Result<(), W::Error>
pub fn encode_sfixed64_as_32(&mut self, i: i32) -> Result<(), W::Error>
Encode a 32-bit number as sfixed64.
Avoids 64-bit operations, which can have benefits on 32-bit architectures.
Sourcepub fn encode_packed<T: Copy, F: FnMut(&mut Self, T) -> Result<(), W::Error>>(
&mut self,
len: usize,
elems: &[T],
encoder: F,
) -> Result<(), W::Error>
pub fn encode_packed<T: Copy, F: FnMut(&mut Self, T) -> Result<(), W::Error>>( &mut self, len: usize, elems: &[T], encoder: F, ) -> Result<(), W::Error>
Encode a repeated packed field from a slice of elements.
The encoder callback determines how each element is encoded onto the wire, and len is
the length of the packed record on the wire.
Sourcepub fn encode_map_elem<K: ?Sized, V: ?Sized, EK: FnMut(&mut Self, &K) -> Result<(), W::Error>, EV: FnMut(&mut Self, &V) -> Result<(), W::Error>>(
&mut self,
len: usize,
key: &K,
key_wtype: u8,
val: &V,
val_wtype: u8,
key_encoder: EK,
val_encoder: EV,
) -> Result<(), W::Error>
pub fn encode_map_elem<K: ?Sized, V: ?Sized, EK: FnMut(&mut Self, &K) -> Result<(), W::Error>, EV: FnMut(&mut Self, &V) -> Result<(), W::Error>>( &mut self, len: usize, key: &K, key_wtype: u8, val: &V, val_wtype: u8, key_encoder: EK, val_encoder: EV, ) -> Result<(), W::Error>
Encode a Protobuf map key-value pair onto the wire.
The key-value pair is encoded as a Protobuf message with the key in field 1 and value in field 2. The wire types of the key and value need to be provided, as well as the length of the key-value pair on the wire.
Sourcepub fn encode_message<M: MessageEncode>(
&mut self,
msg: &M,
) -> Result<(), W::Error>
pub fn encode_message<M: MessageEncode>( &mut self, msg: &M, ) -> Result<(), W::Error>
Encode a message to the wire.