Skip to main content

PrefixCodec

Trait PrefixCodec 

Source
pub trait PrefixCodec {
    type Value<'wire>
       where Self: 'wire;
    type DecodeError: Debug;
    type EncodeError: Debug;
    type Plan<'value>: EncodePlan
       where Self: 'value;

    // Required methods
    fn validate_prefix(bytes: &[u8]) -> Result<PrefixExtent, Self::DecodeError>;
    fn decode<'wire>(bytes: &'wire [u8]) -> Self::Value<'wire>;
    fn plan<'value>(
        value: Self::Value<'value>,
    ) -> Result<Self::Plan<'value>, Self::EncodeError>;
}
Expand description

A codec whose encoded representation occupies a variable-length prefix.

Self::validate_prefix performs structural validation and discovers the exact nonzero extent from available input. A successful extent must not exceed the supplied input. Callers must enforce that implementor law with PrefixExtent::split_input or an equivalent check before slicing, because custom implementations can violate it.

Self::decode receives exactly the encoded bytes for which validation succeeded and whose length equals the reported extent. It decodes semantically without rediscovering a suffix. Calling it on other bytes is a contract violation and may panic. Legal noncanonical input remains the caller’s exact bytes; canonicality belongs to Self::plan. Every successful plan must report a nonzero encoded length and write a complete canonical representation for which Self::validate_prefix returns that same extent. Decoding those bytes must recover the same semantic value supplied to Self::plan. A codec that violates these requirements is contract-invalid. All fallible planning completes before a caller buffer is mutated.

Required Associated Types§

Source

type Value<'wire> where Self: 'wire

Semantic value represented by the codec, which may borrow decode input.

Source

type DecodeError: Debug

Error returned while structurally validating a prefix.

Source

type EncodeError: Debug

Error returned while preparing an encoded value.

Source

type Plan<'value>: EncodePlan where Self: 'value

Prepared canonical encoded bytes, which may borrow the input value.

Required Methods§

Source

fn validate_prefix(bytes: &[u8]) -> Result<PrefixExtent, Self::DecodeError>

Structurally validates a prefix and reports its exact encoded extent.

Source

fn decode<'wire>(bytes: &'wire [u8]) -> Self::Value<'wire>

Decodes exact bytes which have already been successfully prefix-validated.

bytes must be the encoded span selected by the returned PrefixExtent, not the input that may also contain a suffix. Calling this with other bytes is a contract violation and may panic.

Source

fn plan<'value>( value: Self::Value<'value>, ) -> Result<Self::Plan<'value>, Self::EncodeError>

Prepares the complete canonical encoding without mutating a caller buffer.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§