Skip to main content

Crate buffa

Crate buffa 

Source
Expand description

A pure-Rust Protocol Buffers runtime with first-class editions support, zero-copy views, and no_std compatibility.

This is the runtime crate. For code generation, see buffa-build (for build.rs integration) or protoc-gen-buffa (for use as a protoc plugin).

§Quick start

Generated message types implement Message. Encode and decode:

use buffa::Message;

// Encode to a Vec<u8> or bytes::Bytes
let bytes: Vec<u8> = person.encode_to_vec();
let bytes: buffa::bytes::Bytes = person.encode_to_bytes();  // re-export of `bytes` crate

// Decode from a byte slice
let decoded = Person::decode_from_slice(&bytes)?;

// Merge into an existing message (proto3 last-wins / proto2 merge semantics)
existing.merge_from_slice(&bytes)?;

For untrusted input, use DecodeOptions to tighten limits:

use buffa::DecodeOptions;

let msg: Person = DecodeOptions::new()
    .with_recursion_limit(50)
    .with_max_message_size(1024 * 1024)  // 1 MiB
    .decode_from_slice(&bytes)?;

The trait-level convenience methods (decode_from_slice, merge_from_slice) use a fixed recursion limit of RECURSION_LIMIT (100) and no explicit size cap — a &[u8] is already bounded by whatever allocated it. Use DecodeOptions when you want to reject oversized inputs at the decode entry point rather than at the allocator.

§Zero-copy views

Every owned message type Foo has a corresponding FooView<'a> that borrows string and bytes fields directly from the input buffer — no allocation on the read path. See the view module.

let req = PersonView::decode_view(&wire_bytes)?;
println!("name: {}", req.name);  // &'a str, zero-copy

§Feature flags

FlagDefaultEnables
stdstd::io::Read decoders, std::collections::HashMap for map fields, thread-local JSON parse options (the json module)
jsonProto3 JSON via serde (the json_helpers and any_registry modules)
textTextproto (human-readable) encoding and decoding
arbitraryarbitrary::Arbitrary impls for fuzzing

With default-features = false the crate is #![no_std] (requires alloc). Proto3 JSON serialization still works without std via serde + serde_json with their own alloc features.

§Key types

TypePurpose
MessageCore trait for encode / decode / merge
DecodeOptionsConfigurable recursion and size limits
MessageField<T>Optional sub-message with transparent Deref to default
EnumValue<E>Open enum wrapper (Known(E) / Unknown(i32))
UnknownFieldsUnknown-field preservation for round-trip fidelity
Extension<C>Typed extension descriptor (codegen-emitted pub const)
ExtensionSetGet/set extensions via unknown-field storage
view::MessageViewZero-copy borrowed view trait
view::OwnedView<V>Self-contained 'static view backed by Bytes

§no_std

buffa = { version = "0.3", default-features = false }

Generated code uses ::buffa::alloc::string::String etc. rather than relying on the prelude, so it compiles unchanged on bare-metal targets. Map fields use hashbrown::HashMap under no_std; with the std feature enabled they use std::collections::HashMap for ecosystem interop.

Re-exports§

pub use enumeration::EnumValue;
pub use enumeration::Enumeration;
pub use error::DecodeError;
pub use error::EncodeError;
pub use extension::Extension;
pub use extension::ExtensionCodec;
pub use extension::ExtensionSet;
pub use message::DecodeOptions;
pub use message::Message;
pub use message::RECURSION_LIMIT;
pub use message_field::DefaultInstance;
pub use message_field::MessageField;
pub use oneof::Oneof;
pub use unknown_fields::UnknownField;
pub use unknown_fields::UnknownFieldData;
pub use unknown_fields::UnknownFields;
pub use text::TextFormat;text
pub use view::DefaultViewInstance;
pub use view::HasDefaultViewInstance;
pub use view::MapView;
pub use view::MessageFieldView;
pub use view::MessageView;
pub use view::OwnedView;
pub use view::RepeatedView;
pub use view::UnknownFieldsView;

Modules§

any_registryjson
Type registry for google.protobuf.Any proto3 JSON serialization.
editions
Edition feature types and resolution.
encoding
Wire format encoding and decoding primitives.
enumeration
Enum support: the Enumeration trait and EnumValue<E> open-enum wrapper.
error
Error types for buffa encoding and decoding operations.
extension
Typed access to protobuf extensions stored in unknown fields.
extension_registryjson
Runtime registry for proto3 JSON serialization of extensions.
jsonjson
JSON parse options for protobuf JSON deserialization.
json_helpersjson
Proto3 JSON encoding helpers for use with serde.
message
The core Message trait and DecodeOptions builder.
message_field
Optional message field wrapper that provides ergonomic access.
message_set
MessageSet wire format helpers (legacy Google-internal encoding).
oneof
texttext
Textproto (text format) encoding and decoding.
type_registryjson or text
Unified type registry for Any expansion and extension brackets — JSON + text.
types
Protobuf scalar type encoding strategies.
unknown_fields
Unknown field preservation for round-trip fidelity.
view
Zero-copy borrowed message views.