Skip to main content

WireCodec

Trait WireCodec 

Source
pub trait WireCodec:
    Send
    + Sync
    + 'static {
    // Required methods
    fn content_type(&self) -> &'static str;
    fn encode(&self, value: &dyn ErasedWireValue) -> Result<Vec<u8>, CodecError>;
    fn decode(
        &self,
        type_id: WireTypeId,
        bytes: &[u8],
    ) -> Result<Box<dyn ErasedWireValue>, CodecError>;
}
Expand description

A wire-format codec that turns a structured request or response value into bytes and back.

Schema-first codecs (protobuf today, plus the deferred FlatBuffers / Cap’n Proto / Bebop) and schema-less codecs (JSON / CBOR / BSON) are both addressable through this trait. Per-type registration happens on the concrete codec; the trait itself is intentionally narrow so it can be stored as Box<dyn WireCodec> inside the crate::CodecRegistry.

Required Methods§

Source

fn content_type(&self) -> &'static str

Content-type header value that identifies this codec on the wire (for example "application/x-protobuf", "application/json", "application/cbor").

Source

fn encode(&self, value: &dyn ErasedWireValue) -> Result<Vec<u8>, CodecError>

Encode a WireValue to bytes.

Source

fn decode( &self, type_id: WireTypeId, bytes: &[u8], ) -> Result<Box<dyn ErasedWireValue>, CodecError>

Decode bytes into a value of the requested type.

The caller is expected to know what shape it asked for and pass the matching WireTypeId.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§