pub trait Codec: Send + Sync {
const CONTENT_TYPE: &'static str = "application/octet-stream";
// Required methods
fn encode<T: Serialize>(&self, value: &T) -> Result<BytesMut, CodecError>;
fn decode<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T, CodecError>;
// Provided method
fn encode_into<T: Serialize>(
&self,
value: &T,
buf: &mut BytesMut,
) -> Result<(), CodecError> { ... }
}Expand description
A serializer that converts Rust values to and from bytes.
Implementations are stateless and cheap to clone. The trait uses generic methods rather
than associated types so a single codec instance can handle any Serialize /
DeserializeOwned value. This means dyn Codec is not object-safe; use generics or
boxed concrete codecs at the call site.
§Examples
use ruststream::codec::{Codec, JsonCodec};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Order { id: u32, total: f64 }
let codec = JsonCodec;
let bytes = codec.encode(&Order { id: 1, total: 9.99 })?;
let back: Order = codec.decode(&bytes)?;
assert_eq!(back, Order { id: 1, total: 9.99 });Provided Associated Constants§
Sourceconst CONTENT_TYPE: &'static str = "application/octet-stream"
const CONTENT_TYPE: &'static str = "application/octet-stream"
The media type of the bytes this codec produces, reported as the contentType of every
message it encodes or decodes in the generated AsyncAPI document.
The built-in codecs name application/json, application/cbor (RFC 8949) and
application/msgpack. MessagePack has no registered media type; of the two spellings
in use, application/msgpack is the one this crate reports.
The default is application/octet-stream: what a codec whose output carries no media
type of its own should keep.
§Examples
use ruststream::codec::{Codec, JsonCodec};
assert_eq!(JsonCodec::CONTENT_TYPE, "application/json");Required Methods§
Sourcefn encode<T: Serialize>(&self, value: &T) -> Result<BytesMut, CodecError>
fn encode<T: Serialize>(&self, value: &T) -> Result<BytesMut, CodecError>
Encodes value into a mutable byte buffer.
Returning BytesMut lets the encoded buffer move into the publish pipeline (an
Outgoing payload) without a copy, while still allowing
publish middleware to mutate it in place. It is what a publish to a transport that keeps
the payload (Take) calls, once per message.
§Errors
Returns CodecError::Encode when the underlying serializer fails.
Sourcefn decode<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T, CodecError>
fn decode<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T, CodecError>
Decodes bytes into a Rust value of type T.
§Errors
Returns CodecError::Decode when the underlying deserializer fails.
Provided Methods§
Sourcefn encode_into<T: Serialize>(
&self,
value: &T,
buf: &mut BytesMut,
) -> Result<(), CodecError>
fn encode_into<T: Serialize>( &self, value: &T, buf: &mut BytesMut, ) -> Result<(), CodecError>
Encodes value into the end of a buffer the caller owns.
What a publish to a transport that only reads the payload (Lend) calls:
inside a dispatch loop that buffer is the loop’s own, reused for every message, so a
codec that writes into it leaves the publish path with nothing to allocate. Implement it
wherever the serializer can write into a sink; the default encodes into a buffer of its
own and copies, which is correct but pays the allocation this exists to avoid.
§Errors
Returns CodecError::Encode when the underlying serializer fails. The buffer’s
contents are then unspecified: the publish path discards what a failed encode wrote.
§Examples
use ruststream::BytesMut;
use ruststream::codec::{Codec, JsonCodec};
let mut buf = BytesMut::new();
JsonCodec.encode_into(&7u8, &mut buf)?;
assert_eq!(&buf[..], b"7");Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".