Expand description
A primitive utility library for Protocol Buffers in Rust.
This library provides common definitions, constants, enums, and basic logic for implementing Protocol Buffers. It is designed to minimize entry barriers for developers who want to implement Protocol Buffers functionality.
§Overview
This library provides building blocks for implementing Protocol Buffers, not a complete message parser or serializer. It focuses on:
- Low-level primitives: Raw field I/O without semantic interpretation
- Flexibility: Support for both owned and borrowed data
- Minimal dependencies: Only depends on
thiserrorfor error handling - Clear API: Trait-based extension methods following Rust conventions
§Quick Start
§Reading Fields
This library provides multiple ways to read protobuf fields from different sources:
use protobuf_core::{Field, IteratorExtProtobuf, AsRefExtProtobuf};
// From a byte iterator - returns Field<Vec<u8>> (Len values are Vec<u8>)
let bytes = vec![0x08, 0x96, 0x01]; // field 1: 150
let fields: Vec<Field<Vec<u8>>> = bytes
.into_iter()
.protobuf_fields()
.collect::<Result<Vec<Field<Vec<u8>>>, _>>()
.unwrap();
// From a slice - returns Field<&[u8]> (Len values are &[u8], zero-copy)
let slice: &[u8] = &[0x08, 0x96, 0x01];
let fields: Vec<Field<&[u8]>> = AsRefExtProtobuf::read_protobuf_fields(&slice)
.collect::<Result<Vec<Field<&[u8]>>, _>>()
.unwrap();§Writing Fields
use protobuf_core::{WriteExtProtobuf, Field, FieldValue, FieldNumber};
let mut buffer = Vec::new();
let field: Field<Vec<u8>> = Field::new(
FieldNumber::try_from(1)?,
FieldValue::from_uint64(150)
);
buffer.write_protobuf_field(&field)?;§Core Types
§Fields
Field<L>: Represents a raw protobuf field with a field number and value. TheLtype parameter represents the type used for length-delimited values (e.g.,Vec<u8>for owned data,&'a [u8]for borrowed data).FieldValue<L>: Represents the raw value of a field. It can be:Varint(Varint): Variable-width integers (Int32, Int64, UInt32, UInt64, SInt32, SInt64, Bool, Enum)I32([u8; 4]): 32-bit fixed-width values (Fixed32, SFixed32, Float)I64([u8; 8]): 64-bit fixed-width values (Fixed64, SFixed64, Double)Len(L): Length-delimited values (String, Bytes, embedded messages, packed repeated fields)
§Basic Types
Tag: Represents a protobuf tag (field number + wire type)Varint: Represents a deserialized varint value (8-byte internal representation)FieldNumber: A validated field number (range: 1 to 2^29 - 1)WireType: Represents the protobuf wire type (Varint, Int32, Int64, Len, StartGroup, EndGroup)
§Error Handling
ProtobufError: Unified error type for all protobuf operationsResult<T>: Type alias forResult<T, ProtobufError>
§Reading Traits
The library provides several traits for reading protobuf fields from different sources:
IteratorExtProtobuf: Read fields fromIterator<Item = u8>- Output:
Field<Vec<u8>>- Len values are ownedVec<u8>
- Output:
TryIteratorExtProtobuf: Read fields fromIterator<Item = Result<u8, E>>- Output:
Field<Vec<u8>>- Len values are ownedVec<u8>
- Output:
AsRefExtProtobuf: Read fields fromAsRef<[u8]>types (slices, arrays, etc.)- Output: [
Field<&[u8]>] - Len values are borrowed&[u8]slices (zero-copy)
- Output: [
ReadExtProtobuf: Read fields fromstd::io::Readtypes- Output:
Field<Vec<u8>>- Len values are ownedVec<u8>
- Output:
§Writing Traits
WriteExtProtobuf: Write fields tostd::io::Write
§Tag/Varint Traits
For lower-level operations, the library provides traits for reading and writing tags and varints:
IteratorExtTag/TryIteratorExtTag/ReadExtTag: Read tagsIteratorExtVarint/TryIteratorExtVarint/ReadExtVarint: Read varintsWriteExtVarint: Write varints
§Feature Flags
read(enabled by default): Enables field reading utilitieswrite(enabled by default): Enables field writing utilities
You can use features independently:
[dependencies]
protobuf-core = { version = "0.1.0", default-features = false, features = ["read"] }§Constants
The library also provides various constants related to the protobuf wire format:
- Field number limits:
MIN_FIELD_NUMBER,MAX_FIELD_NUMBER - Size limits:
MAX_MESSAGE_SIZE,MAX_STRING_SIZE,MAX_VARINT_BYTES - Wire format constants:
FIXED32_BYTES,FIXED64_BYTES, etc.
Structs§
- Field
- A raw field read from the wire
- Field
Number - A validated Protocol Buffers field number.
- Protobuf
Field Iterator - Iterator for reading raw protobuf fields sequentially from a reader
- Protobuf
Field Iterator From Bytes - Iterator for reading raw protobuf fields sequentially from a byte iterator
- Protobuf
Field Slice Iterator - Iterator for reading raw protobuf fields sequentially from a slice
- Tag
- A protobuf tag containing field number and wire type
- Varint
- A deserialized varint value.
Enums§
- Field
Value - A raw field value read from the wire
- Protobuf
Error - Unified error type for all protobuf operations
- Wire
Type - Wire types used in Protocol Buffers encoding.
Constants§
- FIELD_
NUMBER_ SHIFT - Bit shift for extracting the field number from a tag.
- FIXE
D32_ BYTES - Size of a 32-bit fixed-width value in bytes.
- FIXE
D64_ BYTES - Size of a 64-bit fixed-width value in bytes.
- MAX_
1_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 1 byte.
- MAX_
2_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 2 bytes.
- MAX_
3_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 3 bytes.
- MAX_
4_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 4 bytes.
- MAX_
5_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 5 bytes.
- MAX_
6_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 6 bytes.
- MAX_
7_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 7 bytes.
- MAX_
8_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 8 bytes.
- MAX_
9_ BYTE_ VARINT - Maximum variable-length integer value that can be encoded in 9 bytes.
- MAX_
FIELD_ NUMBER - Maximum field number allowed in Protocol Buffers.
- MAX_
MESSAGE_ SIZE - Maximum message size when serialized (2 GiB).
- MAX_
STRING_ SIZE - Maximum string/bytes field size (2 GiB).
- MAX_
VARINT_ BYTES - Maximum varint size in bytes.
- MIN_
FIELD_ NUMBER - Minimum field number allowed in Protocol Buffers.
- VARINT_
CONTINUATION_ BIT - Continuation bit mask for varint encoding.
- VARINT_
PAYLOAD_ MASK - Payload bit mask for varint encoding.
- WIRE_
TYPE_ MASK - Bit mask for extracting the wire type from a tag.
Traits§
- AsRef
ExtProtobuf - Extension trait for reading raw Protocol Buffer fields from slice types
- Iterator
ExtProtobuf - Extension trait for reading raw Protocol Buffer fields from byte iterators.
- Iterator
ExtTag - Extension trait for reading tag from byte iterators.
- Iterator
ExtVarint - Extension trait for collecting varints from byte iterators.
- Read
ExtProtobuf - Extension trait for reading raw Protocol Buffer fields from
Readtypes - Read
ExtTag - Extension trait for reading tags from Read instances.
- Read
ExtVarint - Extension trait for reading varints from Read instances.
- TryIterator
ExtProtobuf - Extension trait for reading raw Protocol Buffer fields from byte iterators that yield
Result<u8, E>. - TryIterator
ExtTag - Extension trait for reading tag from byte iterators that yield
Result<u8, E>. - TryIterator
ExtVarint - Extension trait for reading varints from byte iterators that yield
Result<u8, E>. - Write
ExtProtobuf - Extension trait for writing raw Protocol Buffer fields to
Writetypes - Write
ExtVarint - Extension trait for writing varints to Write instances.
Type Aliases§
- Result
- Custom Result type for protobuf operations