beave-rs

Protobuf codec

Protobuf<T> encodes and decodes a type T implementing [prost::Message]. Enable the protobuf Cargo feature to use it:

[dependencies]
beavers = { version = "0.1", features = ["protobuf"] }

The codec passes raw Protocol Buffers message bytes through. Encoding calls prost::Message::encode_to_vec; decoding calls prost::Message::decode, so malformed or truncated payloads return an error. An empty message encodes to an empty payload and an empty payload decodes to the message's Default value.

use beavers::{Decoder, Encoder, Protobuf};

// `Event` is a type generated by prost (or derives `prost::Message`).
let codec = Protobuf::<Event>::new();
let bytes = codec.encode(&event)?;
let decoded = codec.decode(&bytes)?;
# Ok::<(), beavers::BoxError>(())

Protobuf does not add a length delimiter, envelope, or other framing. The adapter is responsible for preserving message boundaries, and the same bytes can be exchanged directly with prost's encode_to_vec and decode methods.

Markdown · Source on GitHub