Expand description
§Loro Syncing Protocol (Rust)
Rust encoder/decoder for the Loro wire format described in /protocol.md
It mirrors the TypeScript implementation in packages/loro-protocol and
is shared by the Rust WebSocket server/client crates.
§Guarantees
- Enforces the 256 KiB per-message limit during encoding
- Validates CRDT magic bytes and room id length (<= 128 bytes)
- Uses compact, allocation-friendly varint encoding for strings/bytes
§Crate layout
protocol: message enums/constants and theProtocolMessageenumencoding:encode,decode, andtry_decodehelpersbytes:BytesWriter/BytesReaderfor varint-friendly buffers
§Quick start
use loro_protocol::{encode, decode, ProtocolMessage, CrdtType};
// Build a JoinRequest
let msg = ProtocolMessage::JoinRequest {
crdt: CrdtType::Loro,
room_id: "room-123".to_string(),
auth: vec![10,20,30],
version: vec![40,50,60],
};
// Encode to bytes and decode back
let bytes = encode(&msg).unwrap();
let roundtrip = decode(&bytes).unwrap();
assert_eq!(roundtrip, msg);§Streaming decode
use loro_protocol::{encode, try_decode, ProtocolMessage, CrdtType};
// Prepare any valid message buffer
let buf = encode(&ProtocolMessage::Leave {
crdt: CrdtType::Loro,
room_id: "room-123".into(),
}).unwrap();
let maybe = try_decode(&buf);
assert!(maybe.is_some());See protocol.md and protocol-e2ee.md for the full field ordering and
validation rules.
Re-exports§
pub use bytes::BytesReader;pub use bytes::BytesWriter;pub use encoding::decode;pub use encoding::encode;pub use encoding::try_decode;pub use elo::*;pub use protocol::*;
Modules§
- bytes
- elo
%ELOcontainer and record header parsing (no crypto). Mirrors the TypeScript implementation inpackages/loro-protocol/src/e2ee.tsfor container decode and plaintext header parsing. This module is intentionally crypto-free; consumers can use the parsedaad(exact header bytes) andivwith their own AES-GCM bindings if desired. NOTE:%ELOsupport on the Rust side is work-in-progress; only the container and header parsing surface is considered stable today.- encoding
- Binary encoder/decoder for the Loro protocol wire format.
- protocol
- Protocol types and constants mirrored from
protocol.mdand the TS package.