Skip to main content

CodecFor

Trait CodecFor 

Source
pub trait CodecFor<T>: Codec {
    type Decoded<'buf>: 'buf
       where T: 'buf;

    // Required methods
    fn encode(msg: &T, buf: &mut [u8]) -> Result<usize, Self::Error>;
    fn decode<'buf>(buf: &'buf [u8]) -> Result<Self::Decoded<'buf>, Self::Error>
       where T: 'buf;
}
Expand description

A Codec that can round-trip the message type T.

Both directions work against a caller-supplied slice, so the trait is usable from no_std firmware with a static scratch buffer.

Required Associated Types§

Source

type Decoded<'buf>: 'buf where T: 'buf

What decode yields for a buffer living as long as 'buf.

This is a type constructor, not T, which is what lets one trait cover both owning and zero-copy codecs: PostcardCodec sets it to T, while a zero-copy codec sets it to a borrow such as &'buf T::Archived. Callers must not assume decoding produces an owned value — consortium_ipc::ReceivedMessage exists to abstract over exactly this.

Required Methods§

Source

fn encode(msg: &T, buf: &mut [u8]) -> Result<usize, Self::Error>

Serialize msg into buf, returning the number of bytes written.

Implementations must never truncate: a transport transmits buf[..n] verbatim, so a destination that cannot hold the whole message is an error, not a partial write.

Source

fn decode<'buf>(buf: &'buf [u8]) -> Result<Self::Decoded<'buf>, Self::Error>
where T: 'buf,

Deserialize a T from exactly the bytes in buf.

buf is the received frame trimmed to its true length; trailing slack is not tolerated by every family. Zero-copy codecs borrow from buf, which is why the returned value is tied to 'buf.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§