Expand description
Zero-copy borrowed message views.
Buffa generates two representations for each protobuf message:
-
Owned (
MyMessage): usesString,Vec<u8>,Vec<T>for fields. Suitable for building messages, long-lived storage, and mutation. -
Borrowed (
MyMessageView<'a>): uses&'a str,&'a [u8], and slice-backed repeated fields. Borrows directly from the input buffer for zero-copy deserialization on the read path.
§Motivation
In a typical RPC handler, the request is parsed from a buffer, fields are read, and the buffer is discarded. With owned types, every string and bytes field requires an allocation + copy. With view types, strings and bytes borrow directly from the input buffer — no allocation at all.
This is analogous to how Cap’n Proto’s Rust implementation works, and how Go achieves zero-copy string deserialization via its garbage collector.
§Usage pattern
// Decode a view (zero-copy, borrows from `wire_bytes`)
let request = MyRequestView::decode_view(&wire_bytes)?;
println!("name: {}", request.name); // &str, no allocation
// Build an owned response
let response = MyResponse {
id: request.id,
status: "ok".into(),
..Default::default()
};
// Convert view to owned if needed for storage
let owned: MyRequest = request.to_owned_message();§Generated code shape
For a message like:
message Person {
string name = 1;
int32 id = 2;
bytes avatar = 3;
repeated string tags = 4;
Address address = 5;
}Buffa generates:
// Owned type (heap-allocated strings and vecs)
pub struct Person {
pub name: String,
pub id: i32,
pub avatar: Vec<u8>,
pub tags: Vec<String>,
pub address: MessageField<Address>,
#[doc(hidden)] pub __buffa_unknown_fields: UnknownFields,
#[doc(hidden)] pub __buffa_cached_size: /* internal */,
}
// Borrowed view type (zero-copy from input buffer)
pub struct PersonView<'a> {
pub name: &'a str,
pub id: i32,
pub avatar: &'a [u8],
pub tags: RepeatedView<'a, &'a str>,
pub address: MessageFieldView<AddressView<'a>>,
pub __buffa_unknown_fields: UnknownFieldsView<'a>,
}Structs§
- MapView
- A borrowed view of a map field.
- Message
Field View - A borrowed view of an optional message field.
- Owned
View - An owned,
'staticcontainer for a decoded message view. - Repeated
View - A borrowed view of a repeated field.
- Unknown
Fields View - A borrowed view of unknown fields.
Traits§
- Default
View Instance - Provides access to a lazily-initialized default view instance.
- HasDefault
View Instance - Marker trait linking a lifetime-parameterized view type
V(e.g.,FooView<'a>) to its'staticinstantiation that implementsDefaultViewInstance. Generated code implements this for every view type. - Message
View - Trait for zero-copy borrowed message views.