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
| Flag | Default | Enables |
|---|---|---|
std | ✓ | std::io::Read decoders, std::collections::HashMap for map fields, thread-local JSON parse options (the json module) |
json | Proto3 JSON via serde (the json_helpers and any_registry modules) | |
text | Textproto (human-readable) encoding and decoding | |
arbitrary | arbitrary::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
| Type | Purpose |
|---|---|
Message | Core trait for encode / decode / merge |
DecodeOptions | Configurable recursion and size limits |
MessageField<T> | Optional sub-message with transparent Deref to default |
EnumValue<E> | Open enum wrapper (Known(E) / Unknown(i32)) |
UnknownFields | Unknown-field preservation for round-trip fidelity |
Extension<C> | Typed extension descriptor (codegen-emitted pub const) |
ExtensionSet | Get/set extensions via unknown-field storage |
view::MessageView | Zero-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;textpub 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_
registry json - Type registry for
google.protobuf.Anyproto3 JSON serialization. - editions
- Edition feature types and resolution.
- encoding
- Wire format encoding and decoding primitives.
- enumeration
- Enum support: the
Enumerationtrait andEnumValue<E>open-enum wrapper. - error
- Error types for buffa encoding and decoding operations.
- extension
- Typed access to protobuf extensions stored in unknown fields.
- extension_
registry json - Runtime registry for proto3 JSON serialization of extensions.
- json
json - JSON parse options for protobuf JSON deserialization.
- json_
helpers json - Proto3 JSON encoding helpers for use with serde.
- message
- The core
Messagetrait andDecodeOptionsbuilder. - message_
field - Optional message field wrapper that provides ergonomic access.
- message_
set - MessageSet wire format helpers (legacy Google-internal encoding).
- oneof
- text
text - Textproto (text format) encoding and decoding.
- type_
registry jsonortext - Unified type registry for
Anyexpansion 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.