Skip to main content

Codec

Trait Codec 

Source
pub trait Codec<T>: 'static {
    // Required methods
    fn content_type_suffix() -> &'static str;
    fn encode(value: &T) -> Result<Bytes, Status>;
    fn decode(bytes: &[u8]) -> Result<T, Status>;
}
Expand description

Per-RPC value codec — encodes a request/response message to bytes and back.

The default for codegen is Prost. Other codecs (JSON, custom) implement this trait so the same call-shape dispatch functions can drive them without runtime dispatch.

Required Methods§

Source

fn content_type_suffix() -> &'static str

The fragment after application/grpc+ in the content-type header (e.g. "proto", "json"). For raw application/grpc, return "proto" — the spec says the bare type implies protobuf.

Source

fn encode(value: &T) -> Result<Bytes, Status>

Serialize one message to its wire bytes. The framing layer wraps the result in a length-prefixed gRPC frame; this is just the payload.

Source

fn decode(bytes: &[u8]) -> Result<T, Status>

Deserialize one message from the bytes of a single frame’s payload (already de-framed and decompressed).

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<T> Codec<T> for Prost
where T: Message + Default + 'static,