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.
When a layout builder derives a region length through
Self::Value<'static>: TryFrom<usize>, the complete conversion and codec round trip
must preserve that length: converting the decoded planned representation back to
usize must produce the original region length.
Required Associated Types§
Sourcetype Value<'wire>
where
Self: 'wire
type Value<'wire> where Self: 'wire
Semantic value represented by the codec, which may borrow decode input.
Sourcetype DecodeError: Debug
type DecodeError: Debug
Error returned while structurally validating a prefix.
Sourcetype EncodeError: Debug
type EncodeError: Debug
Error returned while preparing an encoded value.
Sourcetype Plan<'value>: EncodePlan
where
Self: 'value
type Plan<'value>: EncodePlan where Self: 'value
Prepared canonical encoded bytes, which may borrow the input value.
Required Methods§
Sourcefn validate_prefix(bytes: &[u8]) -> Result<PrefixExtent, Self::DecodeError>
fn validate_prefix(bytes: &[u8]) -> Result<PrefixExtent, Self::DecodeError>
Structurally validates a prefix and reports its exact encoded extent.
Sourcefn decode<'wire>(bytes: &'wire [u8]) -> Self::Value<'wire>
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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".