ProtoBuilder
High-level message protocol builder for async Rust.
ProtoBuilder simplifies building type-safe communication protocols over async streams by combining:
- Enum-based message types - Define your protocol messages as Rust enums with compile-time type safety
- Automatic serialization - Uses RON (Rusty Object Notation) for human-readable, text-based message format
- Binary framing layer - Configurable framing strategies (length-prefix or chunked) for message boundary detection
- Async/Tokio native - Built from the ground up for async Rust
Features
- Type-safe protocol definitions using Rust enums
- Length-prefix framing (u16 or u32) for simple message boundaries
- Chunked framing for large messages (32KB+)
- Split protocol support for concurrent read/write
- Clean error handling with custom error types
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1", = ["full"] }
= { = "1", = ["derive"] }
Quick Start
use ;
use ;
async
Framing Strategies
Length Prefix
Simple framing using a length header before each message:
use LengthPrefix;
// 16-bit length prefix (max 64KB messages)
let framing = u16;
// 32-bit length prefix (max 4GB messages)
let framing = u32;
// Custom max size limit
let framing = u32.with_max_size; // 1MB
Chunked Framing
For large messages that need to be split across multiple chunks:
use ChunkedFraming;
// 8KB chunks
let framing = new;
// With custom max message size
let framing = new
.with_max_message_size; // 10MB
Split Protocol
For concurrent read/write operations, split the protocol into halves:
# use ;
# use ;
#
#
#
#
# async
Complete Example
See examples/basic.rs for a working client-server example:
use ;
use ;
use ;
// Server
async
Examples
Run the basic example:
Run the large data example (demonstrates chunked framing):
API Overview
Protocol Builder
// Generic form:
builder
.framing
.build
// See the Quick Start example above for a complete working example
Protocol Methods
send(packet)- Send a messagerecv()- Receive a messagesplit()- Split into read/write halvesinto_inner()- Extract the underlying streamget_ref()/get_mut()- Access the underlying stream
SplitProtocol Methods
send(packet)- Send a message (from the writer half)recv()- Receive a message (from the reader half)into_halves()- Extract the reader, writer, and framing components
Error Types
pub type Result<T> = Result;
How It Works
- Serialization: Your enum messages are serialized to RON text (e.g.,
Message("Hello")→"Message(\"Hello\")") - Framing: The text payload is wrapped with a binary framing layer for message boundary detection:
- Length Prefix: Prepends a u16/u32 length header (big-endian)
- Chunked: Splits large payloads into chunks with message IDs and continuation flags
- Transmission: The framed binary data is written to/read from the async stream
Note: The wire format is binary (due to the framing layer), but the message payload itself is human-readable RON text. For pure binary serialization (e.g., bincode, protobuf), this crate is not suitable.
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.