Expand description
Wire-format codec abstraction for the Riak protocol layer.
dyn-encoding defines a small, object-safe trait surface that lets
a single connection negotiate between a number of structured
encodings on a per-request basis. The crate ships baseline
implementations for all seven encodings called out in the brief:
JsonCodec–application/json(viaserde_json)CborCodec–application/cbor(viaciborium)ProtobufCodec–application/x-protobuf(viaprost)FlatbuffersCodec–application/octet-stream;schema=flatbuffers(viaflatbuffers)CapnpCodec–application/capnproto(viacapnp)BebopCodec–application/x-bebop(viabebop)BsonCodec–application/bson(viabson)
Schema-first codecs (protobuf, FlatBuffers, Cap’n Proto, Bebop)
reify bytes through per-type traits exported alongside each
codec (FlatbuffersWire, CapnpWire, BebopWire; the
protobuf codec uses prost::Message directly). Schema-less
codecs (JSON, CBOR, BSON) reuse serde::Serialize + DeserializeOwned. The trait surface is shaped so adding an
eighth codec is a mechanical exercise: a new module under
codec/, a new register entry point bounded on the codec’s
native trait, and a single line in
CodecRegistry::with_baseline.
§Two-layer design
The codec abstraction is split into two cooperating traits:
WireValue: implemented by the structured types that travel on the wire. Provides a stableWireTypeIdfor content-type plus type negotiation.ErasedWireValue: an object-safe view overWireValueso codec implementations can take&dyn ErasedWireValuewithout being generic. A blanket impl on everyT: WireValuemakes this transparent at call sites.
Each codec is itself object-safe via the WireCodec trait,
so CodecRegistry can store a heterogeneous bag of codecs
and look one up by content-type.
§Per-codec type registration
Schema-first codecs (protobuf today, FlatBuffers / Cap’n Proto /
Bebop tomorrow) require knowing the concrete message type to
reify bytes. Schema-less codecs (JSON / CBOR / BSON) could in
principle dispatch through erased_serde, but doing so would
diverge from the schema-first path. To keep all codec impls
shaped the same way, every codec carries an internal
per-WireTypeId registration table populated through the
codec’s own register::<T>() entry point.
§Example
use dyn_encoding::{
CodecRegistry, ErasedWireValue, JsonCodec, WireTypeId, WireValue,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Deserialize, PartialEq, Serialize)]
struct GetReq {
bucket: String,
key: Vec<u8>,
timeout_ms: u32,
}
impl WireValue for GetReq {
fn wire_type_id() -> WireTypeId {
WireTypeId::new("riak.GetReq")
}
}
// Build a codec, register the message type, install into a
// registry keyed by content-type.
let mut json = JsonCodec::new();
json.register::<GetReq>();
let mut registry = CodecRegistry::new();
registry.register(json);
let codec = registry
.for_content_type("application/json")
.expect("json codec is registered");
let req = GetReq {
bucket: "bk".into(),
key: b"k".to_vec(),
timeout_ms: 5_000,
};
let bytes = codec.encode(&req).expect("encode");
let back = codec.decode(GetReq::wire_type_id(), &bytes).expect("decode");
assert_eq!(back.type_id(), GetReq::wire_type_id());Structs§
- Bebop
Codec - Codec that serialises
WireValuetypes as Bebop messages via per-typeBebopWireimplementations. - Bson
Codec - Codec that serialises
WireValuetypes as BSON via the upstreambsoncrate’s serde integration. - Capnp
Codec - Codec that serialises
WireValuetypes as Cap’n Proto messages via per-typeCapnpWireimplementations. - Cbor
Codec - Codec that serialises
WireValuetypes as CBOR viaciborium. - Codec
Registry - Bag of
WireCodecimplementations indexed by content-type. - Flatbuffers
Codec - Codec that serialises
WireValuetypes as FlatBuffers via per-typeFlatbuffersWireimplementations. - Json
Codec - Codec that serialises
WireValuetypes as JSON. - Protobuf
Codec - Codec that serialises
WireValuetypes using protobuf’s length-undelimited wire format viaprost. - Wire
Type Id - Stable identifier for a structured wire-message type.
Enums§
- Codec
Error - Errors produced by the codec layer.
Traits§
- Bebop
Wire - Per-message-type Bebop encode/decode contract.
- Capnp
Wire - Per-message-type Cap’n Proto encode/decode contract.
- Erased
Wire Value - Object-safe view over
WireValue. - Flatbuffers
Wire - Per-message-type FlatBuffers encode/decode contract.
- Wire
Codec - A wire-format codec that turns a structured request or response value into bytes and back.
- Wire
Value - A value that can travel on the wire under some
WireCodec.