PbEncoder

Struct PbEncoder 

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

Source

pub fn new(writer: W) -> Self

Construct a new encoder from a PbWrite.

Source

pub fn into_writer(self) -> W

Transform the encoder into the underlying writer.

Source

pub fn as_writer(&self) -> &W

Get reference to underlying writer.

Source

pub fn encode_varint32(&mut self, u: u32) -> Result<(), W::Error>

Encode an uint32.

Source

pub fn encode_varint64(&mut self, u: u64) -> Result<(), W::Error>

Encode an uint64.

Source

pub fn encode_int32(&mut self, i: i32) -> Result<(), W::Error>

Encode an int32.

Source

pub fn encode_int64(&mut self, i: i64) -> Result<(), W::Error>

Encode an int64.

Source

pub fn encode_sint32(&mut self, i: i32) -> Result<(), W::Error>

Encode an sint32.

Source

pub fn encode_sint64(&mut self, i: i64) -> Result<(), W::Error>

Encode an sint64.

Source

pub fn encode_bool(&mut self, b: bool) -> Result<(), W::Error>

Encode a bool.

Source

pub fn encode_fixed32(&mut self, u: u32) -> Result<(), W::Error>

Encode a fixed32.

Source

pub fn encode_fixed64(&mut self, u: u64) -> Result<(), W::Error>

Encode a fixed64.

Source

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.

Source

pub fn encode_sfixed32(&mut self, i: i32) -> Result<(), W::Error>

Encode a sfixed32.

Source

pub fn encode_sfixed64(&mut self, i: i64) -> Result<(), W::Error>

Encode a sfixed64.

Source

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.

Source

pub fn encode_float(&mut self, f: f32) -> Result<(), W::Error>

Encode a float.

Source

pub fn encode_double(&mut self, f: f64) -> Result<(), W::Error>

Encode a double.

Source

pub fn encode_tag(&mut self, tag: Tag) -> Result<(), W::Error>

Encode a Protobuf tag.

Source

pub fn encode_bytes(&mut self, bytes: &[u8]) -> Result<(), W::Error>

Encode a bytes field.

Source

pub fn encode_string(&mut self, string: &str) -> Result<(), W::Error>

Encode a string field.

Source

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.

Source

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.

Source

pub fn encode_message<M: MessageEncode>( &mut self, msg: &M, ) -> Result<(), W::Error>

Encode a message to the wire.

Trait Implementations§

Source§

impl<W: Debug + PbWrite> Debug for PbEncoder<W>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Freeze for PbEncoder<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for PbEncoder<W>
where W: RefUnwindSafe,

§

impl<W> Send for PbEncoder<W>
where W: Send,

§

impl<W> Sync for PbEncoder<W>
where W: Sync,

§

impl<W> Unpin for PbEncoder<W>
where W: Unpin,

§

impl<W> UnwindSafe for PbEncoder<W>
where W: 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.